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

后端 未结 2 1866
生来不讨喜
生来不讨喜 2020-12-01 00:02

Problem: I need to host a Node-application and a php-application on the same server on different domains.

example.com should use port 80 as normal, but node-example

2条回答
  •  星月不相逢
    2020-12-01 00:32

    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,

    create a file in /etc/apache2/sites-available/www.example1.com.conf

    
            ServerName  www.example1.com
            ServerAdmin webmaster@example1.com
    
            
                    Options -Indexes +FollowSymLinks
                    AllowOverride All
                    Require all granted
                    DirectoryIndex index.html
            
    
            
                    Options -Indexes +FollowSymLinks
                    AllowOverride All
                    Require all granted
                    DirectoryIndex index.html
            
    
            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
    
    
    

    Create another file www.example2.com.conf in sites-available and copy the above configuration replacing example1 with example2.

    For subdomains, replace www in filename and inside configuration with your subdomain, eg: api.

    Once you have the conf files created, you have to enable them with command

    a2ensite www.example1.com.conf

    and then reload apache2 with command

    sudo systemctl reload apache2

    Make sure you have the directories example1 and example2 created in APACHE_LOG_DIR created before you reload the apache.

    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.

提交回复
热议问题