用于 POST 的 NGINX 代理通行证

发布于 2025-01-09 08:23:54 字数 744 浏览 1 评论 0原文

我有两个上游 A 和 B 分别在端口 8301 和 8303 上运行。我的目的是将反向代理放在它们前面,将 A 上失败的请求传递给 B。

我像这样配置了 nginx

daemon off;
events {}

http {
    upstream api {
        server host.docker.internal:8301;
        server host.docker.internal:8303 backup;
    }

    server {
        listen 8300;
        location / {
            proxy_pass http://api;
            proxy_redirect off;
            proxy_intercept_errors on;
            proxy_next_upstream error http_403;
            proxy_next_upstream error http_502;
        }

    }
}

当我运行 curl localhost:8300/xyz 时,A 响应 403 然后 nginx 将请求传递给 B。但这不适用于 POST。 curl -X POST localhost:8300/xyz 返回 403,并且没有尝试上游 B。

如何配置 nginx 来代理传递所有请求方法?

I have two upstreams A and B running on ports 8301 and 8303 respectively. My intention is to put reverse proxy in front of them which passes request that failed on A to B.

I configured nginx like this

daemon off;
events {}

http {
    upstream api {
        server host.docker.internal:8301;
        server host.docker.internal:8303 backup;
    }

    server {
        listen 8300;
        location / {
            proxy_pass http://api;
            proxy_redirect off;
            proxy_intercept_errors on;
            proxy_next_upstream error http_403;
            proxy_next_upstream error http_502;
        }

    }
}

When I run curl localhost:8300/xyz and A responds with 403 then nginx passes request to B. But this does not work with POST. curl -X POST localhost:8300/xyz returns 403 and there is no attempt for upstream B.

How do I configure nginx to proxy pass all request methods?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

佼人 2025-01-16 08:23:54

当您使用 proxy_next_upstream 并需要重试 POST、LOCK、PATCH 等请求时,您将需要这样配置 proxy_next_upsteam。

        proxy_next_upstream error http_403 non_idempotent;
        proxy_next_upstream error http_502 non_idempotent;

来自 Nginx 文档

non_idempotent
normally, requests with a non-idempotent method (POST, LOCK, PATCH) are not passed to the next server if a request has been sent to an upstream server (1.9.13); enabling this option explicitly allows retrying such requests;

When you utilise proxy_next_upstream and require request like POST, LOCK, PATCH to be retried you will need to configure your proxy_next_upsteam as such.

        proxy_next_upstream error http_403 non_idempotent;
        proxy_next_upstream error http_502 non_idempotent;

From the Nginx Docs

non_idempotent
normally, requests with a non-idempotent method (POST, LOCK, PATCH) are not passed to the next server if a request has been sent to an upstream server (1.9.13); enabling this option explicitly allows retrying such requests;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文