不懂 http 协议,连 nginx 为什么这么设置都不懂 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
请不要在回答技术问题时复制粘贴 AI 生成的内容
ChristopherWu
V2EX    程序员

不懂 http 协议,连 nginx 为什么这么设置都不懂

  •  
  •   ChristopherWu 2019-01-28 14:07:31 +08:00 6046 次点击
    这是一个创建于 2519 天前的主题,其中的信息可能已经有所发展或是发生改变。

    来自我的公众号 『 YongHao 写东西的 Cache 』 打个小广告,还是希望写的东西有人看

    分享一下见解,权当抛砖引玉


    proxy_pass 的默认头部

    在 nginx 中,proxy_pass 是我们做内部转发时很常见的命令,同时少不了用proxy_set_header设置转发时的头部。细看文档,你会发现 nginx 会默认为你转发时设置两个头部:

    proxy_set_header Host $proxy_host; proxy_set_header Connection close; 

    你能想到为什么这两个头部 nginx 为什么要做默认设置吗?


    1. $proxy_host查查文档很容易理解,就是转发的目标服务器。

    $proxy_host

    name and port of a proxied server as specified in the proxy_pass directive;

    设置 Host 头也很容易理解如果你想了一会很想不到,那你真的该好好看看 http 协议的基础知识了。

    想完了吗?答案就是:通常服务器都会设置并根据虚拟域名来过滤请求,如 nginx 中的server_name ,目的就是一台服务器,可以同时服务作用于多个域名的请求。

    1. Connection 头( header ) 决定当前的事务完成后,是否会关闭网络连接。

      如果该值是 “ keep-alive ”,网络连接就是持久的,不会关闭,使得对同一个服务器的请求可以继续在该连接上完成。

      如果该值是 “ close ”,表明客户端或服务器在完结请求后想要关闭该网络连接,这是 HTTP/1.0 请求的默认值

      而 HTTP 的 RFC 定义了:

      HTTP/1.1 applications that do not support persistent connections MUST include the "close" connection option in every message.

    HTTP 1.1 中,如果不支持持久链接的话,每条信息必须包含 close。因为 nginx 不知道你 proxy pass 过去的应用支持长连接不啊,所以只能给你先设个 close。e

    proxy_pass 支持长连接

    而在 proxy_pass支持长连接,更是家常便饭了。

    nginx 使用upstream做负载均衡时,为了提高性能,需要用 keep-alive 来建造连接池,复用已经创建的连接。

    我记得以前看到 nginx 官方文档中,是提供了做法的:

    For HTTP, the proxy_http_version directive should be set to “1.1” and the “ Connection ” header field should be cleared:

    upstream http_backend { server 127.0.0.1:8080; keepalive 16; } server { ... location /http/ { proxy_pass http://http_backend; proxy_http_version 1.1; proxy_set_header Connection ""; ... } } 

    Alternatively, HTTP/1.0 persistent connections can be used by passing the “ Connection: Keep-Alive ” header field to an upstream server, though this method is not recommended.

    又到我们的思考时间了,为什么proxy_http_version要设为 1.1 呢?为什么这次Connection头部设置为空字符串呢?

    第一个问题很容易解答,只要想一想 http 1.11.0的区别即可: http 1.1 开始,支持 keep-alive 长连接。

    第二个问题,其实设置为空字符串,只是为了避免别人传了 Connection为 close 的请求过来,然后我们 转发到 upstream 时 把此头部带过去,导致了全部 upstream中的长连接都被关掉也就是keepalive设置来一点用都没有了。

    也可以设置为“ Connection: Keep-Alive ”,但官方说不推荐,至于为什么,我还没有想明白。

    11 条回复    2019-01-29 12:05:35 +08:00
    msg7086
        1
    msg7086  
       2019-01-28 14:28:16 +08:00
    > Alternatively, HTTP/1.0 persistent connections can be used by passing the “ Connection: Keep-Alive ” header field to an upstream server, though this method is not recommended.

    官方说不推荐的是「在 HTTP/1.0 下强制打开 Keep-Alive 」的做法。句子要读完整。
    ChristopherWu
        2
    ChristopherWu  
    OP
       2019-01-28 14:36:09 +08:00
    @msg7086
    那意思是, 如果是 HTTP 1.1,直接设`Connection: ""` 就可以,upsteam 那里设置了 keepalive 就可以了。
    如果是 HTTP 1.0, 就需要在 Connection 设置 keepalive 强制开启了,否则在 upsteam 那里设置了 keepalive 也没有用?
    msg7086
        3
    msg7086  
       2019-01-28 15:21:12 +08:00   1
    @ChristopherWu
    意思是:

    1. 推荐使用「 HTTP/1.1 」并设置 Connection 为空。HTTP/1.1 下,空与 Keep-Alive 是等价的,没有区别。

    2. 但是如果“你”坚持要用 HTTP/1.0 来做持久连接,那么可以写 Connection: Keep-Alive,但是这种方式(指的是完整的这句话的做法)是不推荐的。

    这里的 this method 指的是 HTTP/1.0 persistent connections,而不是单指 Connection: Keep-Alive。

    至于后面那句话,又是另一回事了。upstream 里的 keepalive 是 TCP 持久连接,这里是 HTTP 持久连接,两个协议工作层数不一样。当然了,两边需要同时开启才能达到持久连接的效果,任何一层关闭了,连接就断了。
    ChristopherWu
        4
    ChristopherWu  
    OP
       2019-01-28 15:32:41 +08:00
    @msg7086 明白了,谢谢大佬指教。『 HTTP/1.1 下,空与 Keep-Alive 是等价的,没有区别』这句我是不知道的。。
    msg7086
        5
    msg7086  
       2019-01-28 16:24:11 +08:00
    @ChristopherWu
    A significant difference between HTTP/1.1 and earlier versions of HTTP is that persistent connections are the default behavior of any HTTP connection. That is, unless otherwise indicated, the client SHOULD assume that the server will maintain a persistent connection, even after error responses from the server.
    ChristopherWu
        6
    ChristopherWu  
    OP
       2019-01-28 16:36:10 +08:00
    @msg7086 谢谢,在 RFC 看到过的。只是 没有意识到 - = -
    ChristopherWu
        7
    ChristopherWu  
    OP
       2019-01-28 16:49:34 +08:00
    @msg7086 大佬为什么这么厉害呢。。啥都知道,能不能分享一下学习经验
    msg7086
        8
    msg7086  
       2019-01-28 17:02:55 +08:00
    @ChristopherWu 过奖了。多看多思考多折腾。
    像是多逛问与答区域本身也能学到很多东西。
    比如看到我不懂的问题,我可以去先学习再回来回答啊。这样就等于一个人学了好多人在学的东西了。
    ChristopherWu
        9
    ChristopherWu  
    OP
       2019-01-28 17:16:12 +08:00
    @msg7086 但其实我觉得 v 站有价值的问题,没有很多- = -
    msg7086
        10
    msg7086  
       2019-01-28 17:17:01 +08:00
    @ChristopherWu 有价值的问题,本来也就不多,这是没办法的啦……
    LinShen
        11
    LinShen  
       2019-01-29 12:05:35 +08:00
    最近发现前端服务是 node 启动的话,用 http-proxy-middleware 模块的话可以替代 nginx 的作用
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2626 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 27ms UTC 11:49 PVG 19:49 LAX 03:49 JFK 06:49
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86