【问题标题】:Nginx facing problem when proxy_pass contains undersocres当 proxy_pass 包含下划线时 Nginx 面临问题
【发布时间】:2021-02-19 02:49:12
【问题描述】:

我有一个 Nginx 服务器,它将请求重定向到另一个外部站点。 位置配置如下:

location ~* ^\/h\/([a-zA-Z0-9.-]+)\/(.*) {

        proxy_connect_timeout 20s;
        set $remote_host $1;
        proxy_set_header Host $remote_host;
        proxy_pass http://$remote_host/$2;

所以,服务器收到如下请求:

http://localhost/h/test.com/testinguri.txt

代理通行证将是http://test.com/testinguri.txt

一切正常,直到我们使用两个下划线并且服务器将响应 502 :

curl -I http://localhost/h/test.com/testing_ur_i.txt

HTTP/1.1 502 错误网关 服务器:nginx/1.10.3 内容类型:文本/html 内容长度:173 连接:保持活动

调试模式的错误日志:

2020/11/05 00:00:11 [错误] 40859#40859: *66328156 上游从上游读取响应标头时过早关闭连接,客户端:****,服务器:_,请求:“GET /h /test.com/testing_ur_i.txt HTTP/1.1",上游:

请求在至少一秒后失败,因此不是超时问题。并且远程站点正常工作。 (出于安全原因,我通过test.com更改了真实域名)

谢谢

【问题讨论】:

标签: nginx


【解决方案1】:

鉴于错误实际上表明连接已被您的后端关闭(在您的示例中为 test.com)。

请尝试将其添加到您的位置部分:

proxy_read_timeout 300s;
proxy_connect_timeout 75s;

https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_read_timeout

【讨论】:

    【解决方案2】:

    所以,这与使用下划线无关。 问题是远程站点支持的http版本!

    将代理传递的 http 更改为 1.1 解决了问题:

    proxy_http_version 1.1;

    谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-06
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多