POST request body being lost with nginx reverse proxy

Viewed 10979

I'm using nginx as a reverse proxy for a http service using a configuration like this:

location /jobexecutor/ {
        proxy_pass      http://jobexecutor:8080/jobexecutor/;
        proxy_set_header    Host $host;
        proxy_set_header    X-Real-IP $remote_addr;
        proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header    X-Forwarded-Proto $scheme;
        proxy_redirect      off;
        proxy_connect_timeout   75s;
}

GET requests are being proxied to the service fine, but when I use POST the request is proxied to the service OK, but the body is empty. When POSTing to the service directly it works fine. Any ideas what's wrong?

2 Answers

You have found a workaround, but I suspect not the root cause.

As per RFC7231 it's a known issue that 301 and 302 server responses often result in the conversion of request methods which are not safe to GET requests when following the redirect.

A normal proxy_pass should be transparent to the client, so it sounds like some other part of your Nginx configuration is doing some client redirection first, before the request gets proxied.

Once you determine where this is happening you can either reconfigure your Nginx conf to eliminate the redirect, or change the 301/302 response codes to 307/308 respectively, which redirect while maintaining the original request method.

I finally found the answer to this. The problem was with curl, in that when following a redirect it wants to convert the POST into a GET, but the -X arg seems to force it to keep it as a GET, but the body get's lost. To get the expected behaviour you need to specify the --post301 or similar argument (as well as the -L argument). See https://curl.haxx.se/docs/manpage.html#--post301

Related