本文会先由一个问题引入,然后再进行多种情况进行分析。

一、问题和排查步骤

1.1 问题基本信息

​ 我们应用程序从代码层面收到的 Header 中的 Host 的值是 upstream 的 名称。 我们程序是需要获取到实际的值。所以这里存在一个问题。

我们先看看我们的 nginx 配置。

upstream open-hz8443{
server 10.60.6.184:8000 max_fails=1 fail_timeout=3s weight=10;
}

server{
        server_name 192.168.80.132;
        listen 80;
        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_connect_timeout   3s;
        proxy_read_timeout 120s;
        proxy_send_timeout 120s;
        proxy_next_upstream error timeout invalid_header http_404 http_502 http_504 http_500;

        location / {
                proxy_pass http://open-hz8443;
        }
        location ^~ /wss/v1
        {
            proxy_pass  http://open-hz8443;
            proxy_set_header Connection "upgrade";
            proxy_set_header Upgrade $http_upgrade;
            tcp_nodelay on;
        }
}

我们请求 http://192.168.80.132/wss/v1, 我们可以在后端(10.60.6.184:8000)获取到 Host 的值为 open-hz8443

这个是我们做了一个 脚本,用于获取请求的所有的数据的。 脚本具体内容在文末。

Connection: upgrade
Host: open-hz8443    # 获取到的Host 是 open-hz8443
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36 FS
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.9

10.210.20.43 - - [04/Jan/2021 15:54:46] "GET /wss/v1 HTTP/1.0" 200 -

1.2 问题解析

首先这里有知识点:

我们在 Server 配了 

proxy_set_header Host $host; 

, 我们在 location 是只配置了

proxy_set_header Connection "upgrade";
proxy_set_header Upgrade $http_upgrade;

     我们的 Host 最初我认为是会继承 Server 配置的 proxy_set_header Host $host; , 但是明显是没有继承的,而是直接拿的 upstream 的名称。说明没有继承,那么这里是有一个什么规则? 我们往下看。

  1. 查询 Nginx 官方文档。

    Module ngx_http_proxy_module

    Allows redefining or appending fields to the request header passed to the proxied server. 
    The value can contain text, variables, and their combinations.
    These directives are inherited from the previous configuration level if 
    and only if there are no proxy_set_header directives defined on the current level. 
    By default, only two fields are redefined:
    

             大概意思就是当我们没有设置 proxy_set_header 才会从上一层级继承,当我们设置了 proxy_set_header ,也就意味着我们不从上面 server 的 proxy_set_header Host $host; 进行继承。

    那为什么会显示 open-hz8443, 是因为有一个默认值

    proxy_set_header Host       $proxy_host;
    

    这个 proxy_host

    • 当我们设置了 upstream 的话,也就是上面的例子$proxy_host 就是 upstream的名称值.
    • 当我们直接使用的 proxy_pass http://10.60.6.184:8000 的话,那么 $proxy_host 表示的就是 10.60.6.184:8000

1.3、解决办法

在 location ^~ /wss/v1 下面增加配置 proxy_set_header Host $host;

        location ^~ /wss/v1
        {
            proxy_pass  http://open-hz8443;
            proxy_set_header   Host     $host;
            proxy_set_header Connection "upgrade";
            proxy_set_header Upgrade $http_upgrade;
            tcp_nodelay on;
        }

二、扩展-各种情况对比

proxy_set_header  默认两项

proxy_set_header Host       $proxy_host;
proxy_set_header Connection close;

proxy_set_header 其他项

proxy_set_header Connection "upgrade";
proxy_set_header Upgrade $http_upgrade;
Server 设置 proxy_set_header 其他项Server 设置 proxy_set_header 默认两项location 设置 proxy_set_header 默认两项location 设置 proxy_set_header 其他项默认值生效默认值不生效
11111 继承location
11011(不继承Server)
11101 继承location
11001 继承server
10101 继承location
10001 默认
100111 默认
10111 继承location
01001 继承server
01011 默认
01101 继承location
01111 继承location
00011 默认
00101 继承location
00111 继承location
00001 默认

总结

  1. location 设置了 proxy_set_header 就不继承,但继承默认值,默认值优先级低于 location设置。
  2. location 未设置了proxy_set_header ,就往上继承,直到默认值。

只要调用了 proxy_set_header,并没有设置 host 和 connection ,默认重写host、connection两个头。

三、扩展 ->脚本

proxy_set_header $host $proxy_host $http_host 各个变量含义

记录一次 Nginx 配置 proxy_pass 后 返回404问题

python 获取请求所有数据信息脚本

#!/usr/bin/env python

import SimpleHTTPServer
import SocketServer

PORT = 8000

class GetHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
    def do_GET(self):
        print(self.headers)
        self.send_response(200, "")
    def do_POST(self):
        print(self.headers)
        content_length = self.headers.getheaders('content-length')
        length = int(content_length[0]) if content_length else 0
        print(self.rfile.read(length))
        self.send_response(200, "")

Handler = GetHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
httpd.serve_forever()

扩展:add_header指令

官方的介绍:

Adds the specified field to a response header provided that the response code equals 200, 201 (1.3.10), 204, 206, 301, 302, 303, 304, 307 (1.1.16, 1.0.13), or 308 (1.13.0). The value can contain variables.

There could be several add_header directives. These directives are inherited from the previous level if and only if there are no add_header directives defined on the current level.

If the always parameter is specified (1.7.5), the header field will be added regardless of the response code.

意思也就是说话在响应状态码成功时,add_header 指令才生效,并且当前《作用域》下没有 add_header 指令时,会向上层继承。

转自:

一次 Nginx proxy_set_header 故障问题解析和延升 - 自由早晚乱余生 - 博客园

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐