32

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.com should route to port 3000.

Routing ALL traffic from port 80 to 3000 works fine using mod_proxy, thusly:

<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.

Is there a way to keep example.com to point to port 80?

It would also be okay for example.com/old-admin to point to port 80.

olke
  • 321
  • 1
  • 3
  • 4

2 Answers2

39

Just make two <VirtualHost *:80> tags

<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

drinchev
  • 19,201
  • 4
  • 67
  • 93
  • I tried using two virtualhost-tags before, but it wasn't working... Turns out I forgot about setting "NameVirtualHost *". Thanks for the answer though! – olke Jan 10 '13 at 14:44
  • 1
    Wow! you saved me from iptables and its invisible rules :) Works great. tnx mate. – Maziyar May 26 '14 at 02:34
  • 2
    If you still not getting it worked, make sure you have enabled `mod_proxy` and `mod_proxy_http` in apache configuration and restart the server. – Bilal Apr 11 '15 at 09:42
  • @drinchev Do I need to add "ProxyRequests off" to all of my apache site files? – tani-rokk Jul 07 '16 at 14:28
  • from the [docs](https://httpd.apache.org/docs/current/mod/mod_proxy.html#proxypass) : Strictly limiting access is essential if you are using a forward proxy (using the ProxyRequests directive). Otherwise, your server can be used by any client to access arbitrary hosts while hiding his or her true identity. This is dangerous both for your network and for the Internet at large. When using a reverse proxy (using the ProxyPass directive with ProxyRequests Off), access control is less critical because clients can only contact the hosts that you have specifically configured. – drinchev Jul 08 '16 at 07:56
5

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

<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.

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.

Hanu
  • 1,087
  • 11
  • 21