Search code examples
node.jswordpressapacheexpressmod-proxy

Apache Proxy for Node.js and Wordpress on same "Domain" but different paths


Here is the case. I have a Wordpress website running on my main domain say http://www.example.com.

.htaccess is enabled on server and I am using Wordpress permalinks to generate pretty URLs.

Now I have another application in Node.js (Express) which I have deployed on same server and is running on port number 3000. So if I put http://www.example.com:3000 it triggers my Expressjs app. and it is running flawlessly.

What I want is to run the Node.js application on a specific path from the same domain. For example, if I hit, http://www.example.com/node it should take user to my Node.js app which it is taking quite fine if I use Apache mod_proxy. But this works only for the / route of my Expressjs router. But I also have several other routes e.g. /subscribe, /confirm but none of them work. Following is my Apache config which I tried to make work by giving multiple ProxyPass directives but it doesn't work.

ProxyPass /node http://127.0.0.1:3000/
ProxyPassReverse /node http://127.0.0.1:3000/
ProxyPass /node/subscribe http://127.0.0.1:3000/subscribe
ProxyPassReverse /node/subscribe http://127.0.0.1:3000/subscribe

The Express router is pretty basic and uses following app configuration:

var router  = express.Router();
app.use("/",router);

router.get("/",function(req,res){
   res.sendFile(templatepath + "index.html");
});

router.get("/subscribe",function(req,res){
   res.sendFile(templatepath + "subscribe.html");
});

router.get("*",function(req,res){
   res.sendFile(templatepath + "404.html");
});

When I try to load http://www.example.com/node/subscribe it simply loads the * route and displays the 404.html template.


Solution

  • Finally got it working by modifying the Apache configuration file as follows:

    ProxyPreserveHost On
    RewriteEngine On
    ProxyPass ^/node/(.*) http://127.0.0.1:3000/$1 [P,L]
    ProxyPassReverse /node/ http://127.0.0.1:3000/