V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
unboy
V2EX  ›  NGINX

网页 TTFB 间歇超高延迟, nginx 如何优化

  •  
  •   unboy · 2019-08-17 16:17:30 +08:00 · 6029 次点击
    这是一个创建于 1685 天前的主题,其中的信息可能已经有所发展或是发生改变。
    静态页面,带宽、cpu、内存占用都在 20%以下,服务器 ping 值 30ms 以下。但网页加载时快时慢,Chrome 排查发现当加载缓慢时,waiting(TTFB)最高时达 15s。以下为 nginx 配置,请问该如何优化?

    worker_processes 2;
    worker_cpu_affinity 0101 1010;
    #pid logs/nginx.pid;
    worker_rlimit_nofile 200134;

    events {
    worker_connections 200134;
    multi_accept on;
    use epoll;
    }

    http {
    include mime.types;
    default_type application/octet-stream;
    access_log off;
    error_log /dev/null;
    fastcgi on;
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 20;
    client_header_timeout 10;
    client_body_timeout 10;
    reset_timedout_connection on;
    send_timeout 10;
    open_file_cache max=200134 inactive=20s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 1;
    open_file_cache_errors on;
    server_tokens off;
    gzip on;
    13 条回复    2019-08-18 11:40:26 +08:00
    misaka19000
        1
    misaka19000  
       2019-08-17 16:22:44 +08:00 via Android
    打 log 定为耗时原因
    misaka19000
        2
    misaka19000  
       2019-08-17 16:22:53 +08:00 via Android
    定位
    arrow8899
        3
    arrow8899  
       2019-08-17 17:17:16 +08:00
    静态页面不至于这么慢吧,在客户端和服务器分别压测一下,看下是不是客户端网络问题;
    aru
        4
    aru  
       2019-08-17 17:22:34 +08:00
    先排除是不是带宽太低,网页内容太大
    alexkkaa
        5
    alexkkaa  
       2019-08-17 19:38:00 +08:00 via Android
    ttfb 十有八九是线路问题。先放个静态 html 测下
    lansquenet
        6
    lansquenet  
       2019-08-17 21:04:17 +08:00
    歪个楼请教一下,worker_connections worker_rlimit_nofile 这两个参数的值不是最大 65535 吗?谢谢!
    lansquenet
        7
    lansquenet  
       2019-08-17 21:07:24 +08:00
    还有一个问题,你们的 CPU 是几核的?我看你的配置,worker_cpu_affinity 这里的配置好像对不上 worker_processes。
    salmon5
        8
    salmon5  
       2019-08-17 21:14:49 +08:00   ❤️ 1
    @lansquenet 都是乱几把配的,worker_rlimit_nofile 最大 1048576
    lansquenet
        9
    lansquenet  
       2019-08-17 21:25:36 +08:00
    @salmon5 worker_rlimit_nofile 貌似受 /etc/security/limits.conf 的句柄数限制,如果 worker_rlimit_nofile 能够设置超过 65535,是不是 /etc/security/limits.conf 的句柄数也能设置超过 65535 ?谢谢!
    hawhaw
        10
    hawhaw  
       2019-08-17 22:29:12 +08:00 via Android   ❤️ 1
    我记得 limits.conf 句柄数能超 65535,只是需要调整下 kernel 参数
    salmon5
        11
    salmon5  
       2019-08-17 23:35:00 +08:00   ❤️ 1
    @lansquenet worker_rlimit_nofile 和 /etc/security/limits.conf 最大都是系统限制的最大值 1048576,这 2 个值没有关系,worker_rlimit_nofile 配了只约束 nginx worker 进程,最大 1048576,大于 1048576 等于没配。系统这个 1048576 可以改,一般没啥意义,不详表。
    bclerdx
        12
    bclerdx  
       2019-08-18 08:12:17 +08:00 via Android   ❤️ 1
    @aru 带宽太低,网页内容太大,会咋样和表现呢?
    realpg
        13
    realpg  
       2019-08-18 11:40:26 +08:00
    nginx purge 了 重新装一个用默认配置也比你这瞎改瞎搭配的号
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   964 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 20:39 · PVG 04:39 · LAX 13:39 · JFK 16:39
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.