Nginxselect一个随机的位置块

我想根据path将请求redirect到不同的服务器,因此我在Nginxconfiguration中使用了以下http块:

http { include /etc/nginx/mime.types; default_type application/octet-stream; index index.html index.htm; server { access_log /var/log/nginx/staging.access.log main buffer=32k; error_log /var/log/nginx/staging.error.log error; listen 80; root /dev/null; location / { proxy_pass http://core:80; # returns "Core Service" } location /page/ { rewrite ^/page(/.*)$ $1 break; proxy_pass http://page:80; # returns "Page Service" } location /auth/ { rewrite ^/auth(/.*)$ $1 break; proxy_pass http://auth:80; # returns "Auth Service" } } } 

据我了解Nginx的文档,Nginx应该使用最好的匹配位置块,因此我期望curl http://hostname/应该返回“核心服务”, curl http://hostname/authvalidation“ curl http://hostname/authvalidation服务”和curl http://hostname/ “页面服务”。 然而Nginx使用一个随机的位置块:

 $ curl http://hostname/ Core Service $ curl http://hostname/ Auth Service $ curl http://hostname/ Page Service $ curl -L http://hostname/page Auth Service $ curl -L http://hostname/page Auth Service $ curl -L http://hostname/page Core Service 

我的configuration有什么问题?

您在每场比赛结束后添加了尾部/字符,请按照以下步骤进行编辑:

 location /page { rewrite ^/page(/.*)$ $1 break; proxy_pass http://page:80; # returns "Page Service" } location /auth { rewrite ^/auth(/.*)$ $1 break; proxy_pass http://auth:80; # returns "Auth Service" } 

顺便说一句,你应该简化你的configuration:

 server { access_log /var/log/nginx/staging.access.log main buffer=32k; error_log /var/log/nginx/staging.error.log error; listen 80; location / { proxy_pass http://core:80/; # returns "Core Service" } location /page/ { proxy_pass http://page:80/; # returns "Page Service" } location /auth/ { proxy_pass http://auth:80/; # returns "Auth Service" } }