【问题标题】:Nginx proxy_pass with variables带有变量的 Nginx proxy_pass
【发布时间】:2021-10-11 10:56:18
【问题描述】:

我有 2 个上游,并在 nginx 配置中描述了 cookie 检查的逻辑:

upstream back {
    server backend-release.mynamespace.svc.cluster.local;
 }
upstream back-preprod {
    server backend-preprod.mynamespace.svc.cluster.local;
}

map $http_cookie $back_upstream {
    default back;
    ~*is-preprod=true back-preprod;
}

location ~ ^/(api|graphql) {
    proxy_pass http://$back_upstream;
}
location /rest/ {
    proxy_pass http://$back_upstream/rest/;
}

当我调用 GET http://my-url/api/... 时,它可以工作,但我总是得到 404 以获得 GET http://my-url/rest/..

如何解决?

【问题讨论】:

  • 试试不带/rest/proxy_pass http://$back_upstream;
  • 相同结果:404

标签: nginx nginx-reverse-proxy nginx-config nginx-location nginx-upstreams


【解决方案1】:

我找到了解决方案:

upstream back {
    server backend-release.mynamespace.svc.cluster.local;
}
upstream back-preprod {
    server backend-preprod.mynamespace.svc.cluster.local;
}

map $http_cookie $back_upstream {
    default back;
    ~*is-preprod=true back-preprod;
}

location ~ ^/(api|graphql|rest) {
    proxy_pass http://$back_upstream;
}

location /rest/ 在这种情况下是不必要的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-08
    • 2013-06-04
    • 1970-01-01
    • 1970-01-01
    • 2022-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多