使用 nginx 反向代理丢失 POST 请求正文

编程入门 行业动态 更新时间:2024-10-25 00:27:51
本文介绍了使用 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 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 请求正文

本文发布于:2023-10-10 01:26:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1477312.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正文   nginx   POST

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!