Search code examples
nginxproxyreverse-proxy

NGINX proxy pass for POST


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?


Solution

  • 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;