启用reuse_port参数让Nginx性能提升3倍
为什么启用 reuse_port
- 记得 2008年做性能测试的时候,新进7台lenovo4核4G 服务器用于性能测试。
- 当时资源紧张,这7台服务器都装了双系统(Win2003/CentOS5)空闲时用于做肉鸡(压测的Agent)。
- 当时给Nginx做了一系列测试,印象很深的是:在这批机器上,Nginx状态页面的压测。
短连接的话最佳QPS约4万,长连接的话最高QPS约13万。
- 大概3年后,那批 lenovo服务器已经没人瞧得上了,只能做肉鸡。
- 然而,一次不经意的测试,发现再牛的服务器,短连接最佳QPS也高不了多少。
- 而且,肉鸡的资源没用完,被测试服务器的资源也用不完,网络也没瓶颈。
服务器资源使用率很低,然而响应就是不够快。
- 最后,我们发现了 SO_REUSEPORT。对,瓶颈在这(监听的入口)!
引入 LVS fullnat
- 当时用的 CentOS 5、6,内核版本都还在 2.6。
- 尝试过新内核,遇到一些奇怪的现象无力维护,最终放弃。
- 也可以用Nginx监控多个IP来突破瓶颈(多虚拟几个IP),维护成本略高。
- 也可以用LVS的NAT方式来做转发,然而NAT对网络的要求你懂的。
- 最终我们决定使用 淘宝LVS,依赖 淘宝内核。
- 然后使用 fullnat ,将对外端口映射到每个节点的多个端口(而非多IP)。
- 曾在线上做过极端测试,效果非常显著。然后我们就上线了!
原本10多台服务器的压力,全压到1台服务器的10多个端口上。居然抗住了
压测 reuse_port
- 如果我用 Debian 最新 Jessie 发行版,默认内核 3.16.0-4。
- 然而 Tengine 早已支持 reuse_port (原生Nginx要到1.9.1才支持)。
- 开启 reuse_port后,你会发现有很多进程同时监听80端口:
 
- 加压后你会发现,服务器性能可被你榨干:
  
- 对比一下测试 reuse_port的效果,小伙伴们惊呆了(短连接QPS过了24万)!
  
- 真相大白后,你还等什么?
探个究竟
- 测试过程中由于压大 - TCP: Possible SYN flooding on port 80.,出大量错误 。
- 于是将并发量降到了6万 - net.core.somaxconn = 65535。
- 再关闭 reuse_port 后,我们看下 - perf top的情况:
 
- 然后再打开 reuse_port ,对比 - perf top的情况:
  
- 此时再放大 Nginx 监听的 - back_log,看下资源使用情况:
  
- 我们来看看些时的队列情况(有入队过万了): 
 
- 然后我们再来挑战30万并发(MTT是平均响应时间(ms)):  
- 经过一系列调优,相同环境相同并发量,没有再出现 - TCP: Possible SYN flooding on port 80.。但出现了少量连接超时的情况:
2015-11-29 01:31:54,779 ERROR Connection timed out
java.net.ConnectException: Connection timed out
				at HTTPClient.HTTPConnection$EstablishConnection.run(HTTPConnection.java:4082) ~[grinder-httpclient-3.9.1.jar:na]
测试环境
Dell PowerEdge M620
Intel(R) Xeon(R) CPU E5-2620 v2 @ 2.10GHz
Linux 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt11-1+deb8u3 (2015-08-04) x86_64 GNU/Linux
Ethernet controller: Broadcom Corporation NetXtreme II BCM57810 10 Gigabit Ethernet (rev 10)
Tengine version: Tengine/2.1.1 (nginx/1.6.2)
built by gcc 4.9.2 (Debian 4.9.2-10)
TLS SNI support enabled
configure arguments: --user=www-data --group=www-data --with-pcre-jit --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --dso-tool-path=/usr/sbin/dso_tool --dso-path=/usr/share/nginx/modules --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-syslog --with-jemalloc --with-mail --with-file-aio --with-mail_ssl_module --with-http_ssl_module --with-google_perftools_module --with-http_auth_request_module --with-http_dav_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_spdy_module --with-http_stub_status_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_flv_module --with-http_slice_module --with-http_mp4_module --with-http_concat_module --with-http_random_index_module --with-http_secure_link_module --with-http_sysguard_module --with-http_split_clients_module=shared --with-http_referer_module=shared --with-http_uwsgi_module=shared --with-http_scgi_module=shared --with-http_memcached_module=shared --with-http_limit_conn_module=shared --with-http_limit_req_module=shared --with-http_empty_gif_module=shared --with-http_browser_module=shared --with-http_user_agent_module=shared --with-http_upstream_ip_hash_module=shared --with-http_upstream_least_conn_module=shared --with-http_upstream_session_sticky_module=shared --with-http_reqstat_module=shared --with-http_lua_module --with-luajit-inc=/usr/include/luajit-2.0 --with-luajit-lib=/usr/lib/x86_64-linux-gnu --with-cpu-opt=amd64 --with-ld-opt=-Wl,-z,relro --with-cc-opt='-g -O2 -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2' --add-module=../echo-nginx-module-0.58 --add-module=../headers-more-nginx-module-0.261 --add-module=../redis2-nginx-module-0.12 --add-module=../ngx_cache_purge-2.3 --add-module=../ngx_cache_path_status-1.0
loaded modules:
    ngx_core_module (static)
    ngx_errlog_module (static)
    ngx_conf_module (static)
    ngx_dso_module (static)
    ngx_syslog_module (static)
    ngx_events_module (static)
    ngx_event_core_module (static)
    ngx_epoll_module (static)
    ngx_procs_module (static)
    ngx_proc_core_module (static)
    ngx_openssl_module (static)
    ngx_regex_module (static)
    ngx_http_module (static)
    ngx_http_core_module (static)
    ngx_http_log_module (static)
    ngx_http_upstream_module (static)
    ngx_http_spdy_module (static)
    ngx_http_static_module (static)
    ngx_http_gzip_static_module (static)
    ngx_http_dav_module (static)
    ngx_http_autoindex_module (static)
    ngx_http_index_module (static)
    ngx_http_random_index_module (static)
    ngx_http_concat_module (static)
    ngx_http_upstream_ip_hash_module (shared, 3.1)
    ngx_http_upstream_least_conn_module (shared, 3.1)
    ngx_http_auth_request_module (static)
    ngx_http_auth_basic_module (static)
    ngx_http_limit_conn_module (shared, 3.1)
    ngx_http_access_module (static)
    ngx_http_limit_req_module (shared, 3.1)
    ngx_http_realip_module (static)
    ngx_http_geo_module (static)
    ngx_http_geoip_module (static)
    ngx_http_split_clients_module (shared, 3.1)
    ngx_http_map_module (static)
    ngx_http_reqstat_module (shared, 3.1)
    ngx_http_user_agent_module (shared, 3.1)
    ngx_http_referer_module (shared, 3.1)
    ngx_http_rewrite_module (static)
    ngx_http_ssl_module (static)
    ngx_http_proxy_module (static)
    ngx_http_uwsgi_module (shared, 3.1)
    ngx_http_fastcgi_module (static)
    ngx_http_scgi_module (shared, 3.1)
    ngx_http_empty_gif_module (shared, 3.1)
    ngx_http_memcached_module (shared, 3.1)
    ngx_http_browser_module (shared, 3.1)
    ngx_http_secure_link_module (static)
    ngx_http_sysguard_module (static)
    ngx_http_flv_module (static)
    ngx_http_slice_module (static)
    ngx_http_mp4_module (static)
    ngx_http_upstream_consistent_hash_module (static)
    ngx_http_upstream_check_module (static)
    ngx_http_upstream_keepalive_module (static)
    ngx_http_upstream_dynamic_module (static)
    ngx_http_stub_status_module (static)
    ngx_http_redis2_module (static)
    ngx_http_cache_purge_module (static)
    ngx_http_slowfs_module (static)
    ngx_cache_path_status_module (static)
    ngx_http_write_filter_module (static)
    ngx_http_header_filter_module (static)
    ngx_http_chunked_filter_module (static)
    ngx_http_spdy_filter_module (static)
    ngx_http_range_header_filter_module (static)
    ngx_http_gzip_filter_module (static)
    ngx_http_postpone_filter_module (static)
    ngx_http_ssi_filter_module (static)
    ngx_http_charset_filter_module (static)
    ngx_http_xslt_filter_module (static)
    ngx_http_image_filter_module (static)
    ngx_http_sub_filter_module (static)
    ngx_http_addition_filter_module (static)
    ngx_http_gunzip_filter_module (static)
    ngx_http_userid_filter_module (static)
    ngx_http_footer_filter_module (static)
    ngx_http_upstream_session_sticky_module (shared, 3.1)
    ngx_http_trim_filter_module (static)
    ngx_http_headers_filter_module (static)
    ngx_http_echo_module (static)
    ngx_http_headers_more_filter_module (static)
    ngx_http_lua_module (static)
    ngx_http_copy_filter_module (static)
    ngx_http_range_body_filter_module (static)
    ngx_http_not_modified_filter_module (static)
    ngx_mail_module (static)
    ngx_mail_core_module (static)
    ngx_mail_ssl_module (static)
    ngx_mail_pop3_module (static)
    ngx_mail_imap_module (static)
    ngx_mail_smtp_module (static)
    ngx_mail_auth_http_module (static)
    ngx_mail_proxy_module (static)
    ngx_google_perftools_module (static)
- 具体Nginx配置如下:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
events {
	use epoll;
  multi_accept off;
  reuse_port on;
  worker_connections  1048576;
}
dso {
# 动态加载功能模块 /usr/share/nginx/modules
				load ngx_http_memcached_module.so;
        load ngx_http_limit_conn_module.so;
        load ngx_http_empty_gif_module.so;
        load ngx_http_scgi_module.so;
        load ngx_http_upstream_session_sticky_module.so;
        load ngx_http_user_agent_module.so;
        load ngx_http_referer_module.so;
        load ngx_http_upstream_least_conn_module.so;
        load ngx_http_uwsgi_module.so;
        load ngx_http_reqstat_module.so;
        load ngx_http_browser_module.so;
        load ngx_http_limit_req_module.so;
        load ngx_http_split_clients_module.so;
        load ngx_http_upstream_ip_hash_module.so;
}
http {
	include       /etc/nginx/mime.types;
	default_type  text/plain;
  access_log  off;
  sendfile        on;
  tcp_nopush	on;
  tcp_nodelay	on;
  server_tokens off;
  keepalive_timeout  120;
  server_names_hash_bucket_size 512;
  server_name_in_redirect off;
  fastcgi_connect_timeout 3s;
  fastcgi_send_timeout 3s;
  fastcgi_read_timeout 3s;
  fastcgi_buffer_size 128k;
  fastcgi_buffers 8 128k;
  fastcgi_busy_buffers_size 256k;
  fastcgi_temp_file_write_size 256k;
  variables_hash_max_size  1024;
  set_real_ip_from 10.0.0.0/8;
  set_real_ip_from 172.28.0.0/16;
  set_real_ip_from 192.168.0.0/16;
  real_ip_header X-Forwarded-For;
  gzip off;
  gzip_disable "msie6";
  gzip_min_length 1k;
  gzip_buffers 16 64k;
  gzip_http_version 1.1;
  gzip_comp_level 6;
  gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
  gzip_vary on;
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
  ssl_prefer_server_ciphers on;
  access_log /var/log/nginx/access.log;
  error_log /var/log/nginx/error.log;
  server {
		listen       80 backlog=65535;
    charset utf-8;
    location / { # 打印Tengine状态页
				stub_status on; # 开启状态页,依赖 http_stub_status_module 模块
        access_log  off; #访问过程不记日志
		}
		location ~ ^(.*)\/\.(svn|git|hg|bzr|cvs)\/ { # 屏蔽这些目录
				deny all;
        access_log off;
        log_not_found off;
    }
		location ~ /\. { # 屏蔽.开头的目录或文件,比如  .htaccess .bash_history
			deny all;
			access_log off;
			log_not_found off;
		}
		location /do_not_delete.html {
			access_log off;
			empty_gif;
		}
	}
}
启用reuse_port参数让Nginx性能提升3倍的更多相关文章
- Web 应用性能提升 10 倍的 10 个建议
		转载自http://blog.jobbole.com/94962/ 提升 Web 应用的性能变得越来越重要.线上经济活动的份额持续增长,当前发达世界中 5 % 的经济发生在互联网上(查看下面资源的统计 ... 
- 重构、插件化、性能提升 20 倍,Apache DolphinScheduler 2.0 alpha 发布亮点太多!
		点击上方 蓝字关注我们 社区的小伙伴们,好消息!经过 100 多位社区贡献者近 10 个月的共同努力,我们很高兴地宣布 Apache DolphinScheduler 2.0 alpha 发布.这是 ... 
- Elasticsearch Reindex性能提升10倍+实战
		文章转载自: https://mp.weixin.qq.com/s?__biz=MzI2NDY1MTA3OQ==&mid=2247484134&idx=1&sn=750249a ... 
- 线程池机制使nginx性能提高9倍
		原文标题:Thread Pools in NGINX Boost Performance 9x! 原文官方地址:https://www.nginx.com/blog/thread-pools-boos ... 
- 如何利用缓存机制实现JAVA类反射性能提升30倍
		一次性能提高30倍的JAVA类反射性能优化实践 文章来源:宜信技术学院 & 宜信支付结算团队技术分享第4期-支付结算部支付研发团队高级工程师陶红<JAVA类反射技术&优化> ... 
- 查询性能提升3倍!Apache Hudi 查询优化了解下?
		从 Hudi 0.10.0版本开始,我们很高兴推出在数据库领域中称为 Z-Order 和 Hilbert 空间填充曲线的高级数据布局优化技术的支持. 1. 背景 Amazon EMR 团队最近发表了一 ... 
- 在MongoDB中创建一个索引而性能提升1000倍的小例子
		在https://www.cnblogs.com/xuliuzai/p/9965229.html的博文中我们介绍了MongoDB的常见索引的创建语法.部分同学还想看看MongoDB的威力到底有多大,所 ... 
- 有史以来性价比最高最让人感动的一次数据库&SQL优化(DB & SQL TUNING)——半小时性能提升千倍
		昨天,一个客户现场人员急急忙忙打电话找我,说需要帮忙调优系统,因为经常给他们干活,所以,也就没多说什么,先了解情况,据他们说,就是他们的系统最近才出现了明显的反应迟钝问题,他们的那个系统我很了解,软硬 ... 
- 存算分离下写性能提升10倍以上,EMR Spark引擎是如何做到的?
		引言 随着大数据技术架构的演进,存储与计算分离的架构能更好的满足用户对降低数据存储成本,按需调度计算资源的诉求,正在成为越来越多人的选择.相较 HDFS,数据存储在对象存储上可以节约存储成本,但与此 ... 
随机推荐
- iPadOS 14 memoji 无法使用 bug
			iPadOS 14 memoji 无法使用 bug iPadOS 14 bug refs 如何在 iPhone 和 iPad Pro 上使用动话表情 https://support.apple.com ... 
- W3C & 弹幕
			W3C & 弹幕 弹幕用例规范 Draft Community Group Report 21 August 2020 refs https://w3c.github.io/danmaku/u ... 
- AIoT & IoT
			AIoT & IoT Artificial Intelligence of Things Internet of Things AIoT === AI + IoT 人工智能物联网 === 人工 ... 
- 根据对象,返回'&键名=值&键名=值‘形式
			Object.prototype.srcCode=function () { var str=''; for(key in this){ if (this.hasOwnProperty(key) == ... 
- Spring系列.Resource接口
			接口简介 JDK中提供了java.net.URL这个类来用于获取不同种类的资源(根据不同前缀的url可以获取不同种类的资源).但是URL这个类没有获取classpath和ServletContext下 ... 
- Linux 网络分析必备技能:tcpdump 实战详解
			大家好,我是肖邦,这是我的第 11 篇原创文章. 今天要分享的是 tcpdump,它是 Linux 系统中特别有用的网络工具,通常用于故障诊断.网络分析,功能非常的强大. 相对于其它 Linux 工具 ... 
- Vue学习笔记-chrome84版本浏览器跨域设置
			一 使用环境: windows 7 64位操作系统 二 chrome84版本浏览器跨域设置 报错问题:Indicate whether to send a cookie in a cross- ... 
- 处理XML数据应用实践
			摘要:GaussDB(DWS)支持XML数据类型及丰富的XML解析函数,可实现关系数据和XML数据的映射管理功能. XML概述 XML是可扩展的标识语言(eXtensible Markup Langu ... 
- win10 查看已保存的wifi密码
			netsh wlan show profile name="WIFINAME-Test" key=clear C:\windows\system32> C:\window ... 
- Docker-compose封装mysql并初始化数据以及redis
			一.概述 现有一台服务器,需要部署mysql和redis.其中mysql容器,需要在第一次启动时,执行sql文件. redis保持空数据即可. 关于Docker-compose的安装,请参考连接: h ... 
