【问题标题】:An HTTP POST redirect to HTTPS leads to a data lose?HTTP POST 重定向到 HTTPS 会导致数据丢失?
【发布时间】:2019-11-23 21:43:40
【问题描述】:

当重定向的 POST 请求没有 Content-Length 时遇到问题。

首先遇到 AWS ALB 及其 301 for HTTP => HTTPS 重定向的问题,并认为这是 ALB 本身的问题。

然后只配置一个普通的 NGINX 与 HTTP 80 服务器和 HTTP 443 服务器:

server {

    listen 80;
    ...

    location / {
        return 302 https://example.com$request_uri;
    }
}

server {

    listen       443 ssl;
...
    location / {

        proxy_pass http://localhost:8081;        

        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-SSL on;
        proxy_set_header    X-Forwarded-Proto $scheme;
   }
}

在上游,我有一个简单的 Python 网络服务器来监听 8081 端口。

然后,如果直接向 443 端口运行请求 - 一切正常。 当我向端口 HTTP 80 发出请求时,会导致 302 重定向 - Python 应用程序说:

    content_length = int(self.headers['Content-Length']) # <--- Gets the size of data TypeError: int() argument must be a string, a
bytes-like object or a number, not 'NoneType' ```

self.headers['Content-Length'] == NoneType.

curl 显示数据时:

$ curl -vL -X POST http://example.com/ -d "param1=value1&param2=value2"
...
< HTTP/1.1 502 Bad Gateway
< Server: nginx/1.10.3
...
< Content-Type: text/html
< Content-Length: 173

我不确定这是否是与 NGINX 相关的问题(再次 - 最初在 AWS Application Load Balancer 上遇到),但无论如何......

【问题讨论】:

  • 找到解决方案,待全面测试后添加到这里。

标签: http nginx redirect post


【解决方案1】:

简而言之:解决方案是使用 307 代码重定向来避免将请求的类型从 POST 更改为 GET

相关的 RFC 文档是 here。 乐于助人的 Mozilla 的 doc。 而关于这个“侦探”的长期阅读是here

【讨论】:

    猜你喜欢
    • 2019-01-08
    • 1970-01-01
    • 2017-01-09
    • 2016-11-23
    • 2018-11-07
    • 1970-01-01
    • 2015-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多