nginx的这种配置作为node.js的反向代理有什么问题?

时间:2022-03-06 02:54:31

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?

我有一个在VPS上运行的个人域名。我想将nginx设置为node.js应用程序的反向代理,但它不起作用。任何人都可以看看我的配置并告诉我我做错了什么?

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.

我们假设我的域名是example.com。基本上,我想这样做,以便当我去node.example.com时,它代理node.js应用程序。我还在nginx中设置了blog.example.com和www.example.com。

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

这是我的反向代理的nginx配置(blog.example.com,省略了www.example.com设置):

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:

这是我的node.js应用程序:

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."

我重新启动了nginx服务器并运行了node.js应用程序。但是如果我转到node.example.com,它会说“node.example.com不存在或不可用”。

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:

我也尝试了以下nginx配置:

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!

1 个解决方案

#1


4  

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

在nginx配置(proxy_pass)中,您必须删除(http://)和(您的主机名)之间的URL中的空格:

you wrote:

proxy_pass http:// nodeapp;

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!

我在我的服务器上尝试并在http:// ..之后添加空格然后重新启动nginx但是nginx是faild!所以,我认为这可能是你的nginx问题!尝试删除此空间,我希望与您合作!

Good luck!

#1


4  

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

在nginx配置(proxy_pass)中,您必须删除(http://)和(您的主机名)之间的URL中的空格:

you wrote:

proxy_pass http:// nodeapp;

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!

我在我的服务器上尝试并在http:// ..之后添加空格然后重新启动nginx但是nginx是faild!所以,我认为这可能是你的nginx问题!尝试删除此空间,我希望与您合作!

Good luck!