10

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.

3
  • load balancing node with apache seems like an easy way to create a bottleneck. Just use nginx and forward some of your requests to node.js (Alternatively re-write your PHP app in node \o/)
    – Raynos
    Jun 25, 2011 at 13:02
  • 1
    nodejs and nginx
    – Raynos
    Jun 25, 2011 at 13:06
  • Yea I think you are both right. nginx seems like the way to go, and would love to rewrite the app in node but time is precious!
    – Finbarr
    Jun 25, 2011 at 14:00

1 Answer 1

11

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>
6
  • I went with Raynos solution but this would have worked too, albeit not as efficiently.
    – Finbarr
    Jun 26, 2011 at 16:45
  • 1
    i would like to add this blog.ngarua.com/?p=112 the configuration in the link enables apache to serve the static files and the nodejs server to handle the actual app requests.
    – memical
    Apr 14, 2012 at 13:50
  • 2
    I am getting the error: ProxyPass|ProxyPassMatch can not have a path when defined in a location. When applying the above!
    – moderns
    May 10, 2014 at 14:36
  • 1
    Re: @moderns comment, to the best of my knowledge you can't have a path for ProxyPass or ProxyPassReverse in a Location block (it's redundant). I had success just removing the path, i.e.: ProxyPass http://localhost:3000.
    – Synexis
    Sep 7, 2015 at 1:52
  • This works, just like Synexis says. I didnt add the paths inside the location block, no need. Jan 23, 2018 at 16:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.