一、 nginx根据自身优化:
1、基于编译安装包优化
例如:tar -zxvf nginx.tar.gz
cd nginx/auto/cc
vim gcc
注释:关闭nginx的debug模块,减小安装包大小
#debug
#CFLAGS="$CFLAGS -g"
2、基于nginx性能优化,通过googl-pertools及libunwind库包优化,针对服务器性能以及并发数、内存分配、速度进行优化。
例如:1、安装libunwind
tar -zxvf libunwind.tar.gz
cd libunwind
CFLAGS=-fPIC ./confgure
make CFLAGS=-fPIC
make CFLAGS=-fPIC install
2、安装gperftools
tar -zxvf gperftools.tat.gz
cd gperftools
./configure
make && make install
3、重新编译nginx
tar -zxvf nginx.tar.gz
cd nginx
./configure --with-http_stub_status_module 支持健康监控模块
--with-google_perftools_module 支持gperftllos模块
--prefix=/usr/local/nginx 安装目录
4、创建文件夹,存放gperftools文件
mkdir /tmp/tcmallor
chmod 777 /tmp/tcmallor
5、修改nginx配置文件,配置支持gperftools存放文件
打开nginx.conf
在pid下增加
google_perftooks_profiles /tmp/tcmallor
然后启动nginx
可以查看lsof -n|grep tcmallor 查看是否存在数据
二、通过nginx内核优化
加入到linux内核参数中,/etc/sysctl.conf文件中。生效:/sbin/sysctl -p
例如:
net.ipv4.tcp_max_tw_backets=6000 设定timewait的数量,默认为180000
net.ipv4.ip_local_port_range=1024 65000 设定打开端口的范围
net.ipv4.tcp_tw_recycle=1 启用timewait快速回收
net.ipv4.tcp_tw_reuse=1 允许timewait 的 socket重用于新的链接
net.ipv4.tcp_syncookies=1 启用syncookies,当syn列队溢出时候,启用cookies处理