问题:我需要在不同域上的同一台服务器上托管节点应用程序和php应用程序。
example.com应该正常使用端口80,但node-example.com应该路由到端口3000。
路由从端口80到3000的所有流量使用mod_proxy正常工作,因此:
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName node-example.com
ServerAlias www.node-example.com
ProxyRequests off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
<Location />
ProxyPass http://localhost:3000/
ProxyPassReverse http://localhost:3000/
</Location>
</VirtualHost>
但是,这会使example.com和node-example.com指向localhost:3000并运行Node-app。
有没有办法让example.com指向端口80?
example.com/old-admin指向端口80也没关系。
只需制作两个<VirtualHost *:80>
标签
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName www.node-example.com
ProxyRequests off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
<Location />
ProxyPass http://localhost:3000/
ProxyPassReverse http://localhost:3000/
</Location>
</VirtualHost>
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName node-example.com
ProxyRequests off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
<Location />
ProxyPass http://localhost:80/
ProxyPassReverse http://localhost:80/
</Location>
</VirtualHost>
它应该这样工作;)
或者,如果您的localhost:80
应用程序不是节点,您可以删除该目标的<Proxy *>
和<Location />
标签,并将其替换为DocumentRoot /var/www/node-example.com
- 您的index.html的静态路径
我建议你为两个域创建两个不同的虚拟主机conf文件。这将使您能够独立配置它们,除了在缩放不同时将它们移动到不同的服务器。
对于具有默认安装位置的apache2,
在/etc/apache2/sites-available/www.example1.com.conf中创建一个文件
<VirtualHost *:80>
ServerName www.example1.com
ServerAdmin [email protected]
<Directory /home/example1/api/admin/docs>
Options -Indexes +FollowSymLinks
AllowOverride All
Require all granted
DirectoryIndex index.html
</Directory>
<Directory /home/example1/api/mobile/docs>
Options -Indexes +FollowSymLinks
AllowOverride All
Require all granted
DirectoryIndex index.html
</Directory>
ProxyRequests Off
ProxyPreserveHost On
ProxyPass /api/ "http://localhost:30007/"
ProxyPassReverse / "http://localhost:30007/"
ErrorLog ${APACHE_LOG_DIR}/example1/example1.log
CustomLog ${APACHE_LOG_DIR}/example1/example1.log combined
</VirtualHost>
在www.example2.com.conf
中创建另一个文件sites-available
并复制上面的配置,用example2替换example1。
对于子域,将filename和内部配置中的www
替换为您的子域,例如:api
。
创建conf文件后,必须使用命令启用它们
a2ensite www.example1.com.conf
然后使用命令重新加载apache2
sudo systemctl reload apache2
确保在重新加载apache之前创建了在APACHE_LOG_DIR中创建的目录example1
和example2
。
而已。使用您的域名注册商或CDN中的服务器IP地址配置您的域名A记录,无论您使用什么,您都应该好好去。