Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what's wrong with this configuration for nginx as reverse proxy for node.js?

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!

like image 853
Brian Park Avatar asked May 19 '11 02:05

Brian Park


People also ask

Can you use Nginx with node js?

Configuring Nginx For Nginx to route to the Node. js application listening on port 3000, we'll need to first unlink the default configuration of Nginx and then create a new configuration to be used for by our Node. js application. The Nginx configuration is kept in the /etc/nginx/sites-available directory.

Can Nginx be used as reverse proxy?

Nginx is an open source web server that can also serve as a reverse proxy. Apart from being used to host websites, it's also one of the most widely used reverse proxy and load balancing solutions.

How do you check if Nginx reverse proxy is working?

To check the status of Nginx, run systemctl status nginx . This command generates some useful information. As this screenshot shows, Nginx is in active (running) status, and the process ID of the Nginx instance is 8539.


1 Answers

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!

like image 59
Saud Alfadhli Avatar answered Nov 10 '22 19:11

Saud Alfadhli