HTTP 2 下 NGINX 反向代理的一些探索 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
请不要在回答技术问题时复制粘贴 AI 生成的内容
byfar
V2EX    程序员

HTTP 2 下 NGINX 反向代理的一些探索

  •  2
     
  •   byfar 2017-04-18 14:18:28 +08:00 10745 次点击
    这是一个创建于 3103 天前的主题,其中的信息可能已经有所发展或是发生改变。

    早上 腾讯云发了个关于 http2 的帖子[HTTP 2 的新特性你 get 了吗?]( t/355553

    看完之后很激动啊,自己的测试小站可以先搞起来啊。

    之后就开始寻思,当前的情况是这样的:阿里一台服务器当作代理服务器,把所有请求代理到本地服务器。查了下 http2.0 在 nginx 下只要升级下 openssl ,装一个模块就可以了,然后就有个疑问:本地的服务器需要也支持 http2.0 吗?代理的原理是怎样的?

    不懂就先百度,百度一圈好气啊,根本找不到答案,刚想发个帖请教下 V 友。忽然想到如果下面评论都是教我如何 google 的那多尴尬。随意 google 了下瞬间柳暗花明。

    [The HTTP/2 Module in NGINX]( https://www.nginx.com/blog/http2-module-nginx/

    里面写得很清楚啊, PPT 也写得很赞, NGINX 靠谱!

    在最后的 Q&A 找到了答案

    Q: Will you support HTTP/2 on the upstream side as well, or only support HTTP/2 on the client side? A: At the moment, we only support HTTP/2 on the client side. You can ’ t configure HTTP/2 with proxy_pass. [Editor In the original version of this post, this sentence was incorrectly transcribed as “ You can configure HTTP/2 with proxy_pass. ” We apologize for any confusion this may have caused.] But what is the point of HTTP/2 on the backend side? Because as you can see from the benchmarks, there ’ s not much benefit in HTTP/2 for low latency networks such as upstream connections. Also, in NGINX you have the keepalive module, and you can configure a keepalive cache. The main performance benefit of HTTP/2 is to eliminate additional handshakes, but if you do that already with a keepalive cache, you don ’ t need HTTP/2 on the upstream side. 

    刚看的时候也好气啊,为什么 http2 这么叼(看了腾讯云之后的感想)你却说他是包装 http1 的运输层?看到后来才感觉腾讯云里的这篇文章( https://www.qcloud.com/community/article/541321?fromSource=gwzcw.93353.93353 )可能更加的片面,官方博客的更加中肯。为这风格点赞,差点就跟了风。

    前面 PPT 中有张图来展示 first painting http1.1>http2>https (有些企业确实为了访问速度放弃了 https ), 那这样一来,确实是不需要本地服务器 http2.0 ,明显用 http 比较快。

    结论:这样一来,我的代理服务器改 http2 就变得简单了,只需要代理服务器 nginx 的 docker 镜像升级一下,其它的都不变就 OK 。暗爽

    不知以上想法有没错误,还没有具体实践,如有纰漏请指正万分感谢!

    17 条回复    2017-04-19 19:01:05 +08:00
    alect
        1
    alect  
       2017-04-18 14:54:22 +08:00
    反代从源服务器取数据确实不支持 http/2
    jayong
        2
    jayong  
       2017-04-18 15:54:09 +08:00
    刚对比看了下腾讯和官方博客的,腾讯的太片面,稍不注意就误导人了啊。
    ohshift
        3
    ohshift  
       2017-04-18 20:39:58 +08:00
    感谢分享
    components
        4
    components  
       2017-04-18 21:20:46 +08:00
    感谢分享
    zealot0630
        5
    zealot0630  
       2017-04-18 21:52:59 +08:00
    我觉得只是 NIGNX 开发者懒,既然 HTTP/2 全面碾压 HTTP/1.1 ,只不过在 upstream 方面提升比较小,他们就懒得做了而已
    zealot0630
        6
    zealot0630  
       2017-04-18 21:53:55 +08:00
    而且 不想用 SSL ,可以用 H2C 阿
    pubby
        7
    pubby  
       2017-04-18 23:58:26 +08:00
    反代使用 1.1 就够了吧

    proxy_http_version 1.1;
    proxy_set_header Connection "";
    laike9m
        8
    laike9m  
       2017-04-18 23:59:56 +08:00   2
    你要想深度了解 HTTP/2 ,网上大部分文章(包括英文的)都太浅。
    之前写过一些协议的分析,可以看看: https://laike9m.com/blog/archive/
    mritd
        9
    mritd  
       2017-04-19 00:15:15 +08:00 via iPhone   1
    偷偷告诉你一个好消息,也是被坑过的地方, nginx 官方继续 alpine 的 docker 镜像自带 http2 支持(openssl 有布丁) 而基于 debian 的则不行
    choury
        10
    choury  
       2017-04-19 01:18:31 +08:00 via Android
    @laike9m 论深入的话来看我对 http2 的实现 https://github.com/choury/sproxy/tree/master/prot
    ryd994
        11
    ryd994  
       2017-04-19 07:08:44 +08:00 via Android
    @zealot0630 开源软件最宝贵的是开发者的精力啊……
    人家又没拿工资嘛………
    真要很有用的话估计会变成商业版专用……
    byfar
        12
    byfar  
    OP
       2017-04-19 08:55:27 +08:00
    @zealot0630 官方说首屏访问速度 http>http2>https 只要代理用 http2 upstream 还是用 http1 ,所以说不考虑在 upsream 中支持 http2

    原文:
    But what is the point of HTTP/2 on the backend side? Because as you can see from
    the benchmarks, there ’ s not much benefit in HTTP/2 for low latency networks such
    as upstream connections.

    难道我理解错了?
    byfar
        13
    byfar  
    OP
       2017-04-19 09:01:59 +08:00
    @mritd 赞,我一直用官方的 latest 镜像,这个镜像 openssl 是 1.0.1 的,没有 http2 。刚看了下 alpine 的 dockerfile 确实有 http_v2_module 。

    确实是个好消息呀,哈哈。
    bbao
        14
    bbao  
       2017-04-19 11:30:18 +08:00   2
    开启 http2 倒是挺简单的,安装 nginx 时候, openssl 使用 1.0.2 ;然后直接 ssl http2 就行;

    关键是你们的客户端需要支持 http2 ,手机 okhttp 版本需要注意, android 和 ios 都有要注意的地方; android 高版本的手机才支持 http2 ;如果 2 年前的手机, okhttp 或者代码里支持 http2 ,发出的请求也可能是 http1 的;所以测试这块,需要特别的注意;
    http2 目前的抓包工具不好抓,常用的抓包工具,能代理手机上的,是没办反走 http2 的;如果已经支持了 http2 ,用 charles 或者其他的抓包工具,它也是自动转换成 http1.1 的;

    所以要看的话,只能在 nginx 打印 protocol 的日志,然后用 wireshark ,抓在浏览器中接口的包;或者使用浏览器的 http2 判断的插件;

    开启 http2 已经有几个月了,感觉还是挺好的;

    开启 http2 之后,常规的 https 优化还是要做的。
    ohshift
        15
    ohshift  
       2017-04-19 18:49:05 +08:00
    @bbao https 要做可以做哪些优化,可不可以提供点思路?
    bbao
        16
    bbao  
       2017-04-19 18:57:42 +08:00   2
    @ohshift

    1. 如果全站 HTTPS ,开启 STS ;避免每次 http 跳转到 https 多余的 302 跳转。
    2. 设置 OCSP ,由服务器验证证书,避免客户端验证证书,客户端验证会多一步 ocsp 的域名解析、 tcp 握手、以及验证交互;
    3. 会话复用, session_cache 、 session_ticket ;避免了再次建立连时的 服务器证书、公钥传递、如果是双向认证的话,也避免了客户端的;
    4. 分布式的话 session_ticket 保存在每个服务器上,可以定时更新;
    5. session_cache 也可以分布式保存;

    6. 上面做完了,对性能要求更高的话,可以把公钥交换时计算的部分,在其他服务器上计算。这个需要修改源代码吧,要求可能更高一些;前 5 点是常规优化,了解完了,可以进行配置;

    关于如何验证 ocsp 的过程, https://segmentfault.com/q/1010000007560751 我在这里自问自答了。
    bbao
        17
    bbao  
       2017-04-19 19:01:05 +08:00
    @ohshift 还有证书大小,三级证书的话,包括站点证书,中级证书,不要根证书;四级证书的话;就站点证书+2 个中级证书;

    这样能减少证书大小的传输。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     3846 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 29ms UTC 10:16 PVG 18:16 LAX 03:16 JFK 06:16
    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