使用 nginx 反向代理丢失 POST 请求正文 [英] POST request body being lost with nginx reverse proxy

查看:620
本文介绍了使用 nginx 反向代理丢失 POST 请求正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 nginx 作为 http 服务的反向代理,使用如下配置:

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 请求被代理到服务很好,但是当我使用 POST 时,请求被代理到服务 OK,但主体是空的.当直接发布到服务时,它工作正常.有什么想法吗?

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?

推荐答案

您已找到解决方法,但我怀疑不是根本原因.

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

根据 RFC7231,301 和 302 服务器是一个已知问题响应通常会导致将不安全的请求方法转换为 GET跟随重定向时的请求.

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.

一个普通的 proxy_pass 应该对客户端透明,所以听起来你的 Nginx 配置的其他部分在请求被代理之前先做一些客户端重定向.

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.

一旦您确定发生这种情况的位置,您可以重新配置您的 Nginx conf 以消除重定向,或者将 301/302 响应代码分别更改为 307/308,从而在保持原始请求方法的同时进行重定向.

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.

这篇关于使用 nginx 反向代理丢失 POST 请求正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆