By Brian Park


2011-05-19 02:57:16 8 Comments

I have a personal domain running on a VPS. I'd like to setup nginx as a reverse proxy to node.js application, but it's not working. Could anyone look at my configuration and tell me what I'm doing wrong?

Let's assume my domain name is example.com. Basically, I'd like to make it so that when I go to node.example.com, it proxies to the node.js app. I also have blog.example.com and www.example.com setup in nginx.

Here's my nginx configuration for the reverse proxy (blog.example.com, www.example.com setup is omitted):

server {
  listen 80;
  server_name node.example.com;

  access_log /srv/www/example.com/logs/node-access.log;
  error_log /srv/www/example.com/logs/node-error.log;

  location / {
      proxy_pass              http://example.com:3000/;
      proxy_redirect          off;
      proxy_set_header        Host            $host;
      proxy_set_header        X-Real-IP       $remote_addr;
      proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
      client_max_body_size    10m;
      client_body_buffer_size 128k;
      proxy_connect_timeout   90;
      proxy_send_timeout      90;
      proxy_read_timeout      90;
      proxy_buffers           32 4k;
  }
}

And here's my node.js application:

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(3000, "example.com");

I restarted the nginx server and ran the node.js application. But if I go to node.example.com, it says "node.example.com does not exist or unavailable."

I'm not sure what's wrong with my configuration. I tried various combinations, too.

These are the configurations I have tried:

proxy_pass in nginx           |   hostname in node.js app
http:// localhost:3000/        |   ---.listen(3000, "localhost")
http:// 127.0.0.1:3000/        |   ---.listen(3000, "127.0.0.1")
http:// node.example.com:3000/ |   ---.listen(3000, "node.example.com")

I also tried the following nginx configuration:

upstream nodeapp {
  server 127.0.0.1:3000;
}

server {
   ...
   location / {
     proxy_pass     http:// nodeapp;
     ...
   }
   ...
}

And it doesn't work either. What am I doing wrong? I've searched on the web for a few hours and tried various approaches but they all don't seem to work.

I'd really appreciate if someone can help me out.

Thanks!

2 comments

@Maxim Shubin 2018-12-07 16:02:58

NodeJS code .listen(3000, "example.com"); waiting for example.com in host header so try to change proxy_set_header Host $host;

to new one

proxy_set_header        Host            example.com;

@Saud Alfadhli 2011-05-19 06:53:45

in nginx configuration ( proxy_pass ) you have to remove spaces in URL between (http://) and (your hostname) :

you wrote:

proxy_pass http:// nodeapp;

you have to write:

proxy_pass http://nodeapp;

I try on my server and add space after http:// .. then restart nginx but the nginx is faild! so, I think this is maybe your nginx problem! try to remove this space and I hope working with you!

Good luck!

@Brian Park 2011-05-20 15:32:59

The reason I put space in http:// nodeapp; is because Stackoverflow complained that I have too many links (possibly spam) in my question post. In my actual configuration, I had it as nodeapp; rather than http:// nodeapp.

@Robert Martin 2012-07-19 21:53:02

Fixed space (resurrected).

Related Questions

Sponsored Content

21 Answered Questions

[SOLVED] Difference between proxy server and reverse proxy server

11 Answered Questions

[SOLVED] Node.js + Nginx - What now?

  • 2011-02-15 20:49:02
  • Van Coding
  • 325744 View
  • 922 Score
  • 11 Answer
  • Tags:   node.js nginx concept

0 Answered Questions

2 Answered Questions

[SOLVED] Express - req.ip returns 127.0.0.1

2 Answered Questions

1 Answered Questions

[SOLVED] nginx proxy pass Node, SSL?

2 Answered Questions

How to point many paths to proxy server in nginx

1 Answered Questions

[SOLVED] Nginx Reverse Proxy with Multiple Backend Domains

1 Answered Questions

[SOLVED] nginx proxy pass with php and relative path

  • 2012-06-06 22:04:08
  • Gilles Grandguillaume
  • 2289 View
  • 0 Score
  • 1 Answer
  • Tags:   php proxy nginx

0 Answered Questions

nginx multiple ports for reverse proxies

Sponsored Content