【问题标题】:Space in URL path ("/path/subpath name") leads to "Invalid HTTP Version: 'name" errorURL 路径中的空格 ("/path/subpath name") 导致 "Invalid HTTP Version: 'name" 错误
【发布时间】:2021-02-06 13:04:19
【问题描述】:

我有以下 nginx 配置:

  location ~* ^/path/(.*) {
    set $query $1;
    proxy_pass http://backend_app/$query;
    proxy_set_header Host $http_host;
    proxy_set_header X-Forwarded-Proto $scheme;
  }

我希望 nginx 将请求从 http://frontend/path/subpath name 代理到 http://backend_app/subpath name。但我看到了一些奇怪的行为:

http://frontend/path/subpath --> http://backend_app/path/subpath 工作正常。

但是

http://frontend/path/subpath name --> 返回 400 错误请求,错误消息如下:

< HTTP/1.1 400 Bad Request
< Server: nginx/1.16.1
< Date: Sat, 06 Feb 2021 01:13:15 GMT
< Content-Type: text/html
< Content-Length: 198
< Connection: keep-alive
< Strict-Transport-Security: max-age=31536000; includeSubDomains
<
<html>
  <head>
    <title>Bad Request</title>
  </head>
  <body>
    <h1><p>Bad Request</p></h1>
    Invalid HTTP Version &#x27;Invalid HTTP Version: &#x27;name HTTP/1.0&#x27;&#x27;
  </body>
</html>
* Connection #0 to host frontend left intact
* Closing connection 0

请注意响应中的错误消息为Invalid HTTP Version &amp;#x27;Invalid HTTP Version: &amp;#x27;name HTTP/1.0&amp;#x27;&amp;#x27;

所以 nginx 将路径中空格后面的单词解释为 http 版本名称的一部分。

我也尝试过使用 URL 编码,但仍然没有成功。

【问题讨论】:

    标签: nginx nginx-reverse-proxy nginx-location


    【解决方案1】:

    刚刚了解了 URI 规范化,我可以通过以下配置解决此问题:

      location /path {
        rewrite /path/(.*) /$1 break;
        proxy_pass http://backend_app;
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-Proto $scheme;
      }
    

    一切都按预期进行。

    【讨论】:

      猜你喜欢
      • 2017-09-21
      • 2012-09-25
      • 2022-01-08
      • 1970-01-01
      • 2021-08-20
      • 1970-01-01
      • 1970-01-01
      • 2021-01-03
      • 2017-02-03
      相关资源
      最近更新 更多