Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

POST request body being lost with nginx reverse proxy

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?

like image 834
tdudgeon Avatar asked Sep 18 '18 09:09

tdudgeon


People also ask

How do you check if Nginx reverse proxy is working?

To check the status of Nginx, run systemctl status nginx . This command generates some useful information. As this screenshot shows, Nginx is in active (running) status, and the process ID of the Nginx instance is 8539.

Can Nginx be used as reverse proxy?

Nginx is an open source web server that can also serve as a reverse proxy. Apart from being used to host websites, it's also one of the most widely used reverse proxy and load balancing solutions.

How does Nginx reverse proxy work?

A reverse proxy server is a type of proxy server that typically sits behind the firewall in a private network and directs client requests to the appropriate backend server. A reverse proxy provides an additional level of abstraction and control to ensure the smooth flow of network traffic between clients and servers.

Is Nginx a load balancer or reverse proxy?

NGINX Plus and NGINX are the best-in-class reverse proxy and load balancing solutions used by high-traffic websites such as Dropbox, Netflix, and Zynga. More than 350 million websites worldwide rely on NGINX Plus and NGINX Open Source to deliver their content quickly, reliably, and securely.


Video Answer


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.

like image 133
miknik Avatar answered Nov 14 '22 17:11

miknik


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

like image 20
tdudgeon Avatar answered Nov 14 '22 17:11

tdudgeon