下面是我的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>。我如何实现这一点?
1条答案
按热度按时间tnkciper1#
如果没有可选的URL,
proxy_pass
将把请求的URL传递给上游代理,而不修改它。例如,发送到服务器的
/auth/foo
请求将作为/auth/foo
传递给http://auth-service:8080
。有两种方法可以在URL被传递到上游之前修改它:使用
proxy_pass
的内置函数或使用rewrite...break
。举例来说:
字符串
请注意,
location
值和proxy_pass
值都必须以/
结束,才能正确Map。这意味着URLlocalhost:80/auth
将不再有效。如果你需要
localhost:80/auth
和localhost:80/auth/foo
都工作,你应该使用rewrite...break
。举例来说:
型
上面的代码无论在
/auth
后面是否有尾随的/
都可以工作。