1.减少Nginx编译后的文件大小 在编译时,Nginx默认以debug模式进行,此模式下会插入很多跟踪和ASSERT之类的信息,在源码文件中找到auto/cc/gcc文件,在如下行: # debug # CFLAGS="$CFLAGS -g" 注释掉 2.为特定的CPU指定CPU类型编译优化 cat /proc/cpuinfo | grep "model name" 查看CPU类型 --with-cc-opt='-O3' 默认的GCC编译参数是“-O”,使用自定义 --with-cpu-opt=amd64 #有效值包括:pentium pentiumpro pentium3 pentium4 athlon opteron amd64 sparc32 sparc64 # ppc64 利用TCMalloc优化Nginx
是谷歌开发的开源工具google-perftools中的一个成员,与标准的glibc库的Malloc相比,内存分配效率和速度上更高,可以提高服务器并发性能,从而降低了系统的负载。 #安装libunwind库libunwind-0.99-alpha.tar.gz tar xvf libunwind-0.99-alpha.tar.gz cd libunwind-0.99-alpha CFLAGS=-fPIC ./configure make CFLAGS=-fPIC make CFLAGS=-fPIC install #安装google-pertoolsgperftools-2.0.tar.gz cd .. tar xvf gperftools-2.0.tar.gz cd gperftools-2.0 ./configure make && make install echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf ldconfig #重新编译Nginx cd .. cd nginx-0.7.65 ./configure --prefix=/opt/nginx --with-http_stub_status_module --with-google_perftools_module --with-http_stub_status_module --with-cc-opt='-O3' --with-cpu-opt=amd64 make && make install mkdir /tmp/tcmalloc为google-perftools 添加线程目录 chmod 0777 /tmp/tcmalloc vi /opt/nginx/conf/nginx.conf #pid logs/nginx.pid; google_perftools_profiles /tmp/tcmalloc; Nginx完成google-perftools的加载 save&&quit /opt/nginx/sbin/nginx lsof -n | grep tcmalloc 验证google-perftools是否正常加载
Nginx内核参数优化
#在linux系统中针对Nginx应用进行的系统内核参数优化 vi /etc/sysctl.conf net.ipv4.tcp_max_tw_buckets = 6000 设定timewait的数量,默认是180000 net.ipv4.ip_local_port_range = 1024 65000 允许系统打开的端口范围 net.ipv4.tcp_tw_recycle = 1 启动timewait快速回收 net.ipv4.tcp_tw_reuse = 1 开启重用,允许将TIME-WAIT sockets重新用于新的TCP连接 net.ipv4.tcp_syncookies = 1 开启SYN Cookies,当出现SYN等待队列溢出是,启用cookies进行处理 net.core.somaxconn = 262144 默认值128,调节系统并发tcp连接数,默认值可能会导致连接超时或重传,根据实际并发请求数调节此值。 net.core.netdev_max_backlog = 262114 当网络接口接收数据包的速率比内核处理这些包的速率快时,允许发送队列数据包的最大的数据。 net.ipv4.tcp_max_orphans = 262144 系统中最大TCP套接字不被关联到任何一个用户句柄上,如果超过这个数字,孤立连接将立刻复位并打印出警告信息。 net.ipv4.tcp_max_syn_backlog = 262144 记录尚未接收到客户端确认信息的连接请求的最大值。 net.ipv4.tcp_synack_retries = 1 决定了内核放弃连接之前发送SYN+ACK包的数量 net.ipv4.tcp_syn_retries = 1 决定了内核放弃建立连接之前发送SYN包的数量 net.ipv4.tcp_fin_timeout = 1 决定了套接字保持在FIN-WAIT-2状态的时间。默认60秒,大量的套接字可能产生内存溢出的风险。 net.ipv4.tcp_keepalive_time = 30 表示当keepalive启动的时候,TCP发送keepalive消息的频度,默认值是2(单位是小时) save&&quit /sbin/sysctl -p
|