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.

Finbarr
  • 31,350
  • 13
  • 63
  • 94
  • 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 '11 at 13:02
  • 1
    [nodejs and nginx](http://stackoverflow.com/questions/5009324/node-js-nginx-and-now) – Raynos Jun 25 '11 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 '11 at 14:00

1 Answers1

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>
dvbportal
  • 134
  • 1
  • 3
  • I went with Raynos solution but this would have worked too, albeit not as efficiently. – Finbarr Jun 26 '11 at 16:45
  • 1
    i would like to add this http://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 '12 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 '14 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 07 '15 at 01:52
  • This works, just like Synexis says. I didnt add the paths inside the location block, no need. – Dan Zuzevich Jan 23 '18 at 16:09
  • this configuration make an error, please fix your cnfiguration – cyril Nov 05 '18 at 08:52