简体   繁体   English

Apache + Node.js + mod_proxy。 如何将一个域路由到:3000,将另一个域路由到:80

[英]Apache + Node.js + mod_proxy. How to route one domain to :3000 and another to :80

Problem: I need to host a Node-application and a php-application on the same server on different domains. 问题:我需要在不同域的同一台服务器上托管Node-application和php-application。

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 info@node-example.com
    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也可以。

Just make two <VirtualHost *:80> tags 只需制作两个<VirtualHost *:80>标签

<VirtualHost *:80>
    ServerAdmin info@node-example.com
    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 info@node-example.com
    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:80 app 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的静态路径

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. 除了在缩放比例不同时将它们移动到其他服务器之外,这还使您可以独立配置它们。

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 webmaster@example1.com

        <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.conf in sites-available and copy the above configuration replacing example1 with example2. sites-available创建另一个文件www.example2.com.conf ,然后复制上述配置,将example1替换为example2。

For subdomains, replace www in 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

and then reload apache2 with command 然后用命令重新加载apache2

sudo systemctl reload apache2

Make sure you have the directories example1 and example2 created in APACHE_LOG_DIR created before you reload the apache. 重新加载apache之前,请确保在APACHE_LOG_DIR中创建的目录example1example2创建。

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记录,无论您使用什么方式,都应该很方便。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM