Search code examples
apacheroutesnode.jsaliasports

Set up apache to alias a nodejs application?


I have a PHP application being served through apache on port 80. I have a nodejs application running standalone on port 3000. I want to make ajax requests from the client side code generated by PHP to the nodejs application. The problem is the same origin policy won't allow a different port, and I can't run both nodejs and apache on port 80.

What I would ideally like to do is have them both appear to run on port 80 from the client's perspective. How can I set up apache to reroute/alias/whatever certain requests to the nodejs application?

Hope that makes sense. Note: Not sure if this is possible, or if I am going about it in the right way - open to suggestions.


Solution

  • You can do that with reverse proxying. Add mod_proxy and setup a location under your main domain in the vhost file to proxy to port 3000 on localhost. Basically something like:

    <VirtualHost *:80>
     ServerName example.com
     <Location /api>
       ProxyPass /api http://localhost:3000/
       ProxyPassReverse /api http://localhost:3000/
     </Location>
    </VirtualHost>