I’m currently trying out nginx and nodejs with connect running nodejs proxied in nginx. The problem I have is that I currently don’t run nodejs under the root (/) but under /data as nginx should handle the static requests as normal. nodejs should not have to know that it’s under /data but it seems to be required.
In other words. I want nodejs to “think” it runs at /. Is that possible?
nginx config:
upstream app_node {
server 127.0.0.1:3000;
}
server {
...
location /data {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://app_node/data;
proxy_redirect off;
}
}
nodejs code:
exports.routes = function(app) {
// I don't want "data" here. My nodejs app should be able to run under
// any folder
app.get('/data', function(req, res, params) {
res.writeHead(200, { 'Content-type': 'text/plain' });
res.end('app.get /data');
});
// I don't want "data" here either
app.get('/data/test', function(req, res, params) {
res.writeHead(200, { 'Content-type': 'text/plain' });
res.end('app.get /data/test');
});
};
I think this solution may be better (if you are using something like Express OR something similar that uses the “middleware” logic):
Add a middleware function to change the url like so
rewriter.js
In your Express app do like so:
app.config