12

I'm trying to configure nginx so it proxy_pass requests to my node apps. Question on StackOverflow got many upvotes: https://stackoverflow.com/questions/5009324/node-js-nginx-and-now and I'm using config from there.

(but since question is about server configuration it is supposed to be on ServerFault)

Here is the nginx configuration:

server {
  listen 80;
  listen [::]:80;

  root /var/www/services.stefanow.net/public_html;
  index index.html index.htm;
  server_name services.stefanow.net;

  location / {
    try_files $uri $uri/ =404;
  }

  location /test-express {
    proxy_pass    http://127.0.0.1:3002;
  }    

  location /test-http {
    proxy_pass    http://127.0.0.1:3003;
  }
}

Using plain node:

var http = require('http');

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(3003, '127.0.0.1');

console.log('Server running at http://127.0.0.1:3003/');

It works! Check: http://services.stefanow.net/test-http

Using express:

var express = require('express');
var app = express(); //

app.get('/', function(req, res) {
  res.redirect('/index.html');
});

app.get('/index.html', function(req, res) {
  res.send("blah blah index.html");
});

app.listen(3002, "127.0.0.1");
console.log('Server running at http://127.0.0.1:3002/');

It doesn't work :( See: http://services.stefanow.net/test-express


I know that something is going on.

a) test-express is NOT running enter image description here

b) text-express is running

enter image description here

(and I can confirm it is running via command line while ssh on the server)

root@stefanow:~# service nginx restart
 * Restarting nginx nginx                                                                                  [ OK ]

root@stefanow:~# curl localhost:3002
Moved Temporarily. Redirecting to /index.html

root@stefanow:~# curl localhost:3002/index.html
blah blah index.html

I tried setting headers as described here: http://www.nginxtips.com/how-to-setup-nginx-as-proxy-for-nodejs/ (still doesn't work)

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;

I also tried replacing '127.0.0.1' with 'localhost' and vice versa


Please advise. I'm pretty sure I miss some obvious detail and I would like to learn more. Thank you.

  • Any log in nginx error log? – masegaloeh Jun 3 '14 at 2:45
  • In this setup - how are you running your express application? Do you need to have a separate process like forever or pm2 that runs it, then nginx just proxies to it? – grammar Jan 15 '15 at 23:00
  • I cannot recall exactly... I remember the accepted answer did work for me. – Michal Stefanow Jan 15 '15 at 23:32
20

You express configured to serve path /index.html, but you requires /test-express/index.html. Either configure express to serve /test-express/index.html or make nginx to strip /test-exress from proxied request. Latter is as simple as add trailing slashes to location and proxy_pass.

location /test-express/ {
  proxy_pass    http://127.0.0.1:3002/;
}

See http://nginx.org/r/proxy_pass for details.

  • 2
    Q: "I'm pretty sure I miss some obvious detail" A: "as simple as add trailing slashes" (thank you, I was literally stuck) – Michal Stefanow Jun 3 '14 at 7:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.