Question

Apache: How to create a sub-directory which would serve node.js app running on a port?

Posted April 17, 2019 3.4k views
ApacheUbuntu 18.04

I am trying setup Apache to have multiple routes/links/sub-directory for multiple apps (node.js app, react app)

For example,

mydomain.net/projects/app1

mydomain.net/projects/app2

And these apps would start on its individual ports like app1 runs on port 81, app2 runs on port 82,.. and so on.

I had already try to create a virtualhost in /etc/apache2/sites-available/newRoute.conf

<VirtualHost *:80>

        ServerName mydomainname.net
        ServerAlias www.mydomainname.net
        ServerAdmin name@hotmail.com

        RewriteEngine On
        RewriteCond %{HTTP:Upgrade} =websocket [NC]
        RewriteRule /(.*)                       ws://localhost:3000/$1 [P,L]
        RewriteCond %{HTTP:Upgrade} !=websocket [NC]
        RewriteRule /(.*)                       http://localhost:3000/$1 [P,L]

        ProxyPassReverse / http://localhost:3000/

</VirtualHost>

<VirtualHost *:80>

        ServerName mydomainname.net/test
        ServerAlias www.mydomainname.net/test
        ServerAdmin name@hotmail.com

        ProxyPass /test http://localhost:81/
        ProxyPassReverse /test http://localhost:81/

</VirtualHost>

but it didn’t work out. I started the node.js app on port 81 and browse to mydomainname.net/test, and it just display my mydomainname.net content.

In addition, localhost:3000 is just a react app which has homepage content

Also, the node.js app which running on localhost:81 would print “hello world” on the screen.

Thank

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
1 answer

@chiKaRauT
Use Location tag as follows

<VirtualHost *:80>

        ServerName mydomainname.net
        ServerAlias www.mydomainname.net
        ServerAdmin name@hotmail.com

       <Location /projects/app1>
             ProxyPass http://localhost:81/
      </Location>

       <Location /projects/app2>
             ProxyPass http://localhost:82/
      </Location>

</VirtualHost>

Hope this helps.

Submit an Answer