寻觅生命中的那一片浅草......

文章带标签 TCP

zz:TCP和Nginx性能调优

绝对干货,沿用文中所说,当别人贴出优化配置的时候,并没有告诉你为什么要这么优化,也没有贴出具体的场景,而这2篇,除了告诉你优化了什么,还说明了为什么要这么做:

Part 1: Lessons learned tuning TCP and Nginx in EC2

Part 2: Lessons learned tuning TCP and Nginx in EC2

 

设置fastcgi_pass和proxy_pass使用长连接

默认情况下,无论是fastcgi_pass还是proxy_pass,每个请求都会建立后端服务器建立一个连接,然后关掉,这样带来的问题是

  • 每个连接都需要新建、关闭连接
  • tcp产生大量time_wait

建立upstream

这个是fastcgi的例子,如果是http的则把端口改下就可以了

upstream fastcgi_backend {
    server 127.0.0.1:9000;
    keepalive 60;
}

fastcgi_pass

加入fastcgi_keep_conn on;

location ~ .*\.php$
{
    fastcgi_pass fastcgi_backend;
    fastcgi_keep_conn on;
}

proxy_pass

location ~ .*\.php$
{
    # 清除"Connection"头部
    proxy_set_header   Connection "";
    # 将http版本由1.0修改为1.1
    proxy_http_version 1.1;
    proxy_pass  http://backend.server;
}

经过上面的优化,fastcgi和proxy的time_wait都可以由1000+下降到20+

主要参考

2024年三月
« 5月    
 123
45678910
11121314151617
18192021222324
25262728293031