7
votes

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!

3
That error message sounds more like a dns issue than an nginx/node issue. Does it work if you use the ip address instead of node.example.com?kolbyjack
I tried using the IP address as well, but that didn't work either.Brian Park
@kolbyjack, actually you were right. I misconfigured DNS. Now it's working =) Thanks!Brian Park
@BrianPark I think that the second parameter of the .listen method is what is messing up. There you need to specify which interface to listen (hostname). I recommend you to not pass that parameter. Just .listen(3000), and by default the second parameter will be 0.0.0.0, that will work too.Robert

3 Answers

6
votes

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!

0
votes

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;
0
votes

If you want too run your node.js app on node.example.com then in proxy configuration you need to define node on 127.0.0.1:3000 or localhost:3000 and not as example.com:3000

If you need to run on example.com with 3000 as port then initially you need to define nginx rule for port 80 as well. By default a domain is mapped against port 80 and 443. Rest you can change configuration in your node.js startup file.