关于 nginx 长连接的问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
simple2025
0D
V2EX    Python

关于 nginx 长连接的问题

  •  
  •   simple2025 2021 年 4 月 7 日 3153 次点击
    这是一个创建于 1747 天前的主题,其中的信息可能已经有所发展或是发生改变。

    如果 client -> nginx 配置成长连接, keep-alive

    nginx->server 不配置成长链接,

    那么 整个请求时长链接的吗?

    因为 tornado 好像是不支持长连接的

    感觉我 nginx 好多不会呀

    第 1 条附言    2021 年 4 月 7 日
    我现在的问题是 client-> nginx 感觉也不是长连接了


    ```
    (tppay) [vagrant@localhost tppay]$ ss -o state time-wait | grep ":8008" | wc -l
    691
    ```

    8008 是 nginx 监听的端口好
    第 2 条附言    2021 年 4 月 7 日
    nginx 配置

    ```

    http
    {
    include mime.types;
    #include luawaf.conf;

    include proxy.conf;

    default_type application/octet-stream;

    server_names_hash_bucket_size 512;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    client_max_body_size 50m;

    sendfile on;
    tcp_nopush on;

    keepalive_timeout 60;

    tcp_nodelay on;

    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_siz 256k;
    fastcgi_intercept_errors on;

    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 2;
    gzip_types text/plain application/Javascript text/html application/x-Javascript text/Javascript text/css application/xml application/json;
    gzip_vary on;
    gzip_proxied expired no-cache no-store private auth;
    gzip_disable "MSIE [1-6]\.";

    limit_conn_zone $binary_remote_addr zOne=perip:10m;
    limit_conn_zone $server_name zOne=perserver:10m;

    server_tokens off;
    access_log off;


    }

    ```
    第 3 条附言    2021 年 4 月 7 日
    好像是我自己的问题,结了吧
    11 条回复    2021-04-08 09:43:24 +08:00
    ericls
        1
    ericls  
       2021 年 4 月 7 日 via iPhone   1
    在 nginx 那一层应该是复用的 你试试就知道了

    实际上一个 tcp 经过那么多个 hop 实际情况很难控制
    opengps
        2
    opengps  
       2021 年 4 月 7 日   1
    应该仅仅是外层 client -> nginx 是长连接复用,内部 nginx->server 依然是有请求就单独转发一次
    iConnect
        3
    iConnect  
       2021 年 4 月 7 日 via Android
    客户端你是用 websocket 吗?
    iyaozhen
        4
    iyaozhen  
       2021 年 4 月 7 日   1
    那么 整个请求时长链接的吗?
    显然不是啊

    tornado 我看默认就支持呀
    https://www.tornadoweb.org/en/stable/httpserver.html?highlight=keep%20alive#http-server

    nginx 需要这样配置:
    http://nginx.org/en/docs/http/ngx_http_upstream_module.html#keepalive
    ChoateYao
        5
    ChoateYao  
       2021 年 4 月 7 日   2
    HTTP keepalive 还是 TCP keepalive 这是两种不同的概念,TCP keepalive 没认真了解过。

    但是 HTTP keepalive 是指复用 TCP 连接,当客户端跟服务端建立起 TCP 通道之后,请求出第一个 HTTP,如果 HTTP 没有指明 keepalive,则得到响应之后服务端会马上把 TCP 断开;否则可以复用 TCP 通道,无需创建多个 TCP 通道,这样子就能避免在一定时间内发起 HTTP 请求,因为创建多个 TCP 通道导致性能性能损耗。
    wakzz
        6
    wakzz  
       2021 年 4 月 7 日   1
    nginx -> server 默认的 http 负载均衡是 TCP 短连接,需要一些额外的配置才能设置成 HTTP 长连接。
    simple2025
        7
    simple2025  
    OP
       2021 年 4 月 7 日
    @iyaozhen 记错了,是 tornado 自带的 simple_httpclient 不支持长连接
    simple2025
        8
    simple2025  
    OP
       2021 年 4 月 7 日
    @wakzz 但是我现在 client->nginx 都是短链接了,我现在想要 client->nginx 是长连接
    wakzz
        9
    wakzz  
       2021 年 4 月 7 日   2
    @chenqh 四楼的第二个链接就是文档,修改方式就是 upstream 添加 keepalive,以及 location 添加 proxy_http_version 1.1 和 proxy_set_header Connection ""就行。注意是这几个同时配置后才起效,缺一个都不行。
    holinhot
        10
    holinhot  
       2021 年 4 月 7 日
    题主说的回源长连接
    abccccabc
        11
    abccccabc  
       2021 年 4 月 8 日
    楼主,9 楼的方法可行。
    a href="/about" class="dark" target="_self">关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2846 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 32ms UTC 12:48 PVG 20:48 LAX 04:48 JFK 07:48
    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