NGINX在使用上行时没有响应

我试图通过nginx来平衡一个web应用程序,它可以正常工作,我的web应用程序会调用带有子path的服务。

例如它的工作

http://example.com/luna/ 

但不是

  http://example.com/luna/sales 

我的nginx.conf

 user nobody; worker_processes auto; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; map $http_upgrade $connection_upgrade { default upgrade; '' close; } upstream lunaups { server myhostserver1.com:8080; server myhostserver2.com:8080; } server { listen 80; server_name example.com; proxy_pass_header Server; location = / { rewrite ^ http://example.com/luna redirect; } location /luna { rewrite ^$/luna/(.*)/^ /$1 redirect; proxy_pass http://lunaups; #add_header X-Upstream $upstream_addr; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } 

我的web应用程序调用一个像/ luna / sales这样的额外子path的服务,无法返回响应。 我在这里错过了什么?

它工作,如果我从上游移除我的主机服务器之一,但是当我在上游添加第二个主机它无法返回响应。

我的重写规则是错的还是我的configuration是错的?

您的重写规则将Web浏览器从/luna/salesredirect到/sales 。 这意味着Web浏览器会为/sales一个新的HTTP请求,但是您没有匹配/sales location块,因此您将收到404错误。

我相信你真正想要做的是改变代理到上游的URI。 如果是这样,你可以尝试改变你的location块:

 location ~ /luna(?<upstream_uri>(/.*)?) { proxy_pass http://lunaups/$upstream_uri; } 

这将匹配/luna/luna/whatever ,将匹配的子expression式绑定为$upstream_uri ,然后仅将该子expression式发送到上游。