Apache + Node.js + mod_proxy。如何将一个域路由到 :3000 并将另一个域路由到 :80
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14259321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Apache + Node.js + mod_proxy. How to route one domain to :3000 and another to :80
提问by olke
Problem: I need to host a Node-application and a php-application on the same server on different domains.
问题:我需要在不同域的同一台服务器上托管一个 Node 应用程序和一个 php 应用程序。
example.com should use port 80 as normal, but node-example.com should route to port 3000.
example.com 应该正常使用端口 80,但 node-example.com 应该路由到端口 3000。
Routing ALL traffic from port 80 to 3000 works fine using mod_proxy, thusly:
使用 mod_proxy 将所有流量从端口 80 路由到 3000 可以正常工作,因此:
<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>
This however makes both example.com and node-example.com to point to localhost:3000 and run the Node-app.
然而,这使得 example.com 和 node-example.com 都指向 localhost:3000 并运行 Node-app。
Is there a way to keep example.com to point to port 80?
有没有办法让 example.com 指向端口 80?
It would also be okay for example.com/old-admin to point to port 80.
example.com/old-admin 也可以指向端口 80。
回答by drinchev
Just make two <VirtualHost *:80>tags
只做两个<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>
It should work that way ;)
它应该这样工作;)
Or if your localhost:80app isn't node you can remove <Proxy *>& <Location />tags for that target and replace it with DocumentRoot /var/www/node-example.com- your static path to index.html
或者,如果您的localhost:80应用程序不是节点,您可以删除该目标的<Proxy *>&<Location />标签并将其替换为DocumentRoot /var/www/node-example.com- index.html 的静态路径
回答by Amruta-Pani
I suggest you create two different virtual host conf files for two domains. This will enable you to configure them independently besides moving them to different servers when the scaling is different.
我建议您为两个域创建两个不同的虚拟主机 conf 文件。除了在缩放不同时将它们移动到不同的服务器之外,这将使您能够独立配置它们。
For apache2 with default installation location,
对于具有默认安装位置的 apache2,
create a file in /etc/apache2/sites-available/www.example1.com.conf
在 /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>
Create another file www.example2.com.confin sites-availableand copy the above configuration replacing example1 with example2.
创建另一个文件www.example2.com.conf中sites-available,并复制上面的配置与例题更换例1。
For subdomains, replace wwwin filename and inside configuration with your subdomain, eg: api.
对于子域,将www文件名和内部配置替换为您的子域,例如:api.
Once you have the conf files created, you have to enable them with command
创建 conf 文件后,您必须使用命令启用它们
a2ensite www.example1.com.conf
a2ensite www.example1.com.conf
and then reload apache2 with command
然后用命令重新加载apache2
sudo systemctl reload apache2
sudo systemctl reload apache2
Make sure you have the directories example1and example2created in APACHE_LOG_DIR created before you reload the apache.
在重新加载 apache 之前,请确保您在 APACHE_LOG_DIR 中创建了目录example1和example2创建的目录。
Thats it. configure your domain's A record with server IP address in your domain registrar or CDN, whatever you are using and you should be good to go.
就是这样。使用您的域注册商或 CDN 中的服务器 IP 地址配置您域的 A 记录,无论您使用什么,您都应该很高兴。

