多个 Node JS Express 应用程序的 Nginx 多个位置 [英] Nginx multiple locations for multiple Node JS Express Apps

查看:69
本文介绍了多个 Node JS Express 应用程序的 Nginx 多个位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下配置:

location / {
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header X-NginX-Proxy true;
  proxy_pass http://localhost:3000; # this is where our node js app runs at
  proxy_set_header Host $http_host;
  proxy_cache_bypass $http_upgrade;
  proxy_redirect off;
}

哪些代理 [SERVER_IP]/localhost:3000 以便它基本上将所有内容路由到 node js 应用程序.

Which proxies [SERVER_IP]/ to localhost:3000 so that it basically routes everything to the node js app.

然后我继续编写了另一个 nodejs 应用程序,它在端口 5000 上运行,而不是 3000:

I then went ahead and wrote another nodejs app, which runs on port 5000, instead of 3000:

location / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-NginX-Proxy true;
      proxy_pass http://localhost:3000; # this is where our node js app runs at
      proxy_set_header Host $http_host;
      proxy_cache_bypass $http_upgrade;
      proxy_redirect off;
    }

location /testing {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-NginX-Proxy true;
      proxy_pass http://localhost:5000; # this is where our node js app runs at
      proxy_set_header Host $http_host;
      proxy_cache_bypass $http_upgrade;
      proxy_redirect off;
    }

但是,如果我转到 [SERVER_IP]/testing 它会将其作为/testing 路由到我的第一个应用程序,然后生成 Express JS 消息:无法获取/testing".

However if I go to [SERVER_IP]/testing it routes it as /testing to my first app which then generates the Express JS message: "Cannot GET /testing".

我更改了顺序,重新启动了 nginx 没有问题.知道为什么它不起作用吗?我假设 nginx 是路由到 Node JS 之前的第一个实例.如果我更改 location/{ ... } 的端口,我可以获得第二个应用程序,但我希望它们彼此并行运行.

I changed the order, restarted nginx without problems. Any idea why it would not work? I assume nginx is the first instance before it gets routed to Node JS. If I change the port of the location / { ... } I can get the second app, but I would want to run them parallel to each other.

谢谢

推荐答案

您可能会发现它实际上是您的第二个应用程序,它生成了 Express JS 消息:Cannot GET/testing".

You'll probably find it's actually your second app which is generating the Express JS message: "Cannot GET /testing".

Nginx proxy_pass 指令的行为因您定义它们的方式可能看起来非常微小的差异而有所不同.如果您指定一个位置块并将其 proxy_pass 到一个没有定义路径的服务器,那么整个客户端请求 uri 将被传递到上游服务器.

Nginx proxy_pass directives behave differently based upon what can appear to be very minor differences in how you define them. If you specify a location block and proxy_pass it to a server with no path defined then the entire client request uri will be passed to the upstream server.

http://localhost/testing 将代理到 http://localhost:5000/testing

但是,如果您指定了一个 proxy_pass 指令并附加了任何内容,Nginx 会将与位置块匹配的客户端请求部分替换为您附加到您的 proxy_pass 指令的路径.

However, if you specify a proxy_pass directive with anything appended, Nginx will replace the part of the client request which matches the location block with the path you appended to your proxy_pass directive.

这样,最后只加一个斜杠:

So this, with just an extra slash at the end:

location /testing/ {
    proxy_pass http://localhost:5000/;

现在 Nginx 会这样做:

Now results in Nginx doing this:

http://localhost/testing -> http://localhost:5000/

还有这个:

location /testing/ {
    proxy_pass http://localhost:5000/foo/;

会这样做:

http://localhost/testing/bar/ -> http://localhost:5000/foo/bar/

总而言之,proxy_pass 到裸服务器:ip 以传递整个客户端请求 uri,添加一个斜杠以删除部分 uri,或添加其他内容来替代.

In summary, proxy_pass to a naked server:ip to pass entire client request uri, add a single slash to remove part of the uri, or add something else to substitute.

另一个答案表明它是由您的位置块的顺序引起的,这是不正确的,对回答的人表示尊重我建议您阅读他们发布的链接中的信息,但不要遵循他们的建议,因为它们看起来有点对 Nginx 自己选择位置块的方式感到困惑.

The other answer suggesting it's caused by the order of your location blocks is incorrect, with all due respect to the person who answered I would recommend you read the information from the link they posted but not follow their advice, as they appear slightly confused about the way Nginx selects location blocks themselves.

这篇关于多个 Node JS Express 应用程序的 Nginx 多个位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆