nginx中的路径Map

csga3l58  于 4个月前  发布在  Nginx
关注(0)|答案(1)|浏览(51)

下面是我的nginx conf文件events {}

http {
    server {
        listen 80;

        location / {
            proxy_pass http://frontend-app:4200;
        }

        location /auth {
            proxy_pass http://auth-service:8080;
        }
    }
}

字符串
当我输入localhost:80时,它被转发到frontend-app:4200没有问题。同样,当输入localhost:80/auth时,它被转发到auth-service:8080没有问题。但是,当我输入localhost:80/auth/时<some_path>,它没有被转发到auth-service:8080/<some_path>。我如何实现这一点?

tnkciper

tnkciper1#

如果没有可选的URL,proxy_pass将把请求的URL传递给上游代理,而不修改它。
例如,发送到服务器的/auth/foo请求将作为/auth/foo传递给http://auth-service:8080
有两种方法可以在URL被传递到上游之前修改它:使用proxy_pass的内置函数或使用rewrite...break
举例来说:

location /auth/ {
    proxy_pass http://auth-service:8080/;
}

字符串
请注意,location值和proxy_pass值都必须以/结束,才能正确Map。这意味着URL localhost:80/auth将不再有效。
如果你需要localhost:80/authlocalhost:80/auth/foo都工作,你应该使用rewrite...break
举例来说:

location /auth {
    rewrite ^/auth/?(.*)$ /$1 break;
    proxy_pass http://auth-service:8080;
}


上面的代码无论在/auth后面是否有尾随的/都可以工作。

相关问题