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
chenqh
V2EX  ›  Python

关于 nginx 长连接的问题

  •  
  •   chenqh · 2021-04-07 12:15:16 +08:00 · 2554 次点击
    这是一个创建于 1087 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

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

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

    感觉我 nginx 好多不会呀

    第 1 条附言  ·  2021-04-07 13:03:19 +08:00
    我现在的问题是 client-> nginx 感觉也不是长连接了


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

    8008 是 nginx 监听的端口好
    第 2 条附言  ·  2021-04-07 14:15:12 +08:00
    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_size 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-04-07 14:34:18 +08:00
    好像是我自己的问题,结了吧
    11 条回复    2021-04-08 09:43:24 +08:00
    ericls
        1
    ericls  
       2021-04-07 12:33:27 +08:00 via iPhone   ❤️ 1
    在 nginx 那一层应该是复用的 你试试就知道了

    实际上一个 tcp 经过那么多个 hop 实际情况很难控制
    opengps
        2
    opengps  
       2021-04-07 12:46:11 +08:00   ❤️ 1
    应该仅仅是外层 client -> nginx 是长连接复用,内部 nginx->server 依然是有请求就单独转发一次
    iConnect
        3
    iConnect  
       2021-04-07 13:11:32 +08:00 via Android
    客户端你是用 websocket 吗?
    iyaozhen
        4
    iyaozhen  
       2021-04-07 13:47:56 +08:00   ❤️ 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-04-07 13:58:50 +08:00   ❤️ 2
    HTTP keepalive 还是 TCP keepalive 这是两种不同的概念,TCP keepalive 没认真了解过。

    但是 HTTP keepalive 是指复用 TCP 连接,当客户端跟服务端建立起 TCP 通道之后,请求出第一个 HTTP,如果 HTTP 没有指明 keepalive,则得到响应之后服务端会马上把 TCP 断开;否则可以复用 TCP 通道,无需创建多个 TCP 通道,这样子就能避免在一定时间内发起 HTTP 请求,因为创建多个 TCP 通道导致性能性能损耗。
    wakzz
        6
    wakzz  
       2021-04-07 14:00:36 +08:00   ❤️ 1
    nginx -> server 默认的 http 负载均衡是 TCP 短连接,需要一些额外的配置才能设置成 HTTP 长连接。
    chenqh
        7
    chenqh  
    OP
       2021-04-07 14:13:33 +08:00
    @iyaozhen 记错了,是 tornado 自带的 simple_httpclient 不支持长连接
    chenqh
        8
    chenqh  
    OP
       2021-04-07 14:14:11 +08:00
    @wakzz 但是我现在 client->nginx 都是短链接了,我现在想要 client->nginx 是长连接
    wakzz
        9
    wakzz  
       2021-04-07 15:18:12 +08:00   ❤️ 2
    @chenqh 四楼的第二个链接就是文档,修改方式就是 upstream 添加 keepalive,以及 location 添加 proxy_http_version 1.1 和 proxy_set_header Connection ""就行。注意是这几个同时配置后才起效,缺一个都不行。
    holinhot
        10
    holinhot  
       2021-04-07 19:41:54 +08:00
    题主说的回源长连接
    abccccabc
        11
    abccccabc  
       2021-04-08 09:43:24 +08:00
    楼主,9 楼的方法可行。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2855 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 11:41 · PVG 19:41 · LAX 04:41 · JFK 07:41
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.