I have in the same server NodeJS and Nginx running at a time. Nginx takes the requests from "www.example.com" and i want NodeJS to take requests from "api.example.com". I almost got it, i configured Nginx to forward the requests from "api.example.com" to NodeJS (localhost:3000) and certainly it works, except for the requests arrive to NodeJS without any information.
For example, I'm using PassportJS to develop all the logic behind NodeJS. I have something like this:
app.post('/room/create', function(req, res) { console.log(req); } );
And "req", the var who is supposed to store all the information from the request is empty when i access by "api.ismuser.com". But, if i use the public IP:PORT to get NodeJS, it works.
I'm just guessing this is due to Nginx, who is working like a reverse proxy for NodeJS.
I don't know if putting Nginx in front of NodeJS is well done, but it was the only way i found to get working www.ismuser.com and api.ismuser.com with the same public IP.
What can i do?
UPDATED
Nginx.conf:
http {
passenger_root /home/ubuntu/.rvm/gems/ruby-2.0.0-p195@ism_r3/gems/passenger-4.0.5;
passenger_ruby /home/ubuntu/.rvm/wrappers/ruby-2.0.0-p195@ism_r3/ruby;
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream backend {
server 127.0.0.1:3000;
}
server {
listen 80;
server_name api.example.com;
location / {
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://backend/;
proxy_redirect off;
}
}
server {
listen 80;
server_name www.example.com;
root /home/ubuntu/ismapi/ism/public; # <--- be sure to point to 'public'!
passenger_enabled on;
}
}
You might want to try to set up the proxy_pass directive without a trailing URI part '/', like this:
proxy_pass http://backend;
If you specify the URI, it'll be used in the request sent to your backend node.js app. Instead of a request for /room/create, node.js will get a request for / as defined by your proxy_pass setting.
For more information, please see the nginx proxy_pass documentation: http://wiki.nginx.org/HttpProxyModule#proxy_pass