Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 524
Finbarr Avatar asked Jun 25 '11 12:06

Finbarr


People also ask

Can I use Apache with node js?

The benefits Apache brings to Node. js applications. How to configure Apache for a Node.

Does Nodejs replace Apache?

If you're prepared to re-write your PHP in JavaScript, then yes, Node. js can replace your Apache. If you place an Apache or NGINX instance running in reverse-proxy mode between your servers and your clients, you could handle some requests in JavaScript on Node.

Is node faster than Apache?

The response time is almost the same with the lowest concurrency level, but with 250 concurrent requests, NodeJS was five times faster than Apache.


1 Answers

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>
like image 113
dvbportal Avatar answered Oct 03 '22 04:10

dvbportal