Nginx优化
来源:互联网 发布:c语言写网络爬虫 编辑:程序博客网 时间:2024/05/29 11:28
Nginx优化
1.编译优化
1)取消debug模式,取消插入跟踪和ASSERT信息,有利于系统运行流畅。
vim auto/cc/gcc
#注释以下内容
# debug
#CFLAGS="$CFLAGS -g"
2)特定CPU指定CPU类型
--with-cc-opt='-O3'
--with-cpu-opt=[CPU] #有效值:
[pentium|pentiumpro|pentium3|pentium4|athlon|opteron|amd64|sparc32|sparc64|ppc64]#cat /proc/cpuinfo | grep "model name" 查看CPU类型
2.利用TCMalloc优化Nginx性能
TCPmalloc是谷歌开发的开源工具"google-perftools"中。在内存分配效率和速度上提升,提高服务器在高并发性的性能,降低系统负载。
1.安装libunwind
wget http://ftp.twaren.net/Unix/NonGNU//libunwind/libunwind-1.1.tar.gz
tar -zxf libunwind-1.1.tar.gz
cd libunwind-1.1
./configure CFLAGS=-fPIC
make CFLAGS=-fPIC
make CFLAGS=-fPIC
install
2.安装gperftools
cd ..
wget https://github.com/gperftools/gperftools/releases/download/gperftools-2.1.90/gperftools-2.1.90.tar.gz
- tar -zxf gperftools-2.1.90.tar.gz
- cd gperftools-2.1.90
./configure #提前安装gcc+(yum install gcc-c++ -y)
make && make install
echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf
ldconfig
3.编译nginx
./configure \
--prefix=/usr/local/nginx \
--
with
-google_perftools_module
make && make install
4.为google_perftools创建线程目录
mkdir /tmp/tcmalloc
chmod 777 /tmp/tcmalloc/
5.nginx配置文件加入后启动
google_perftools_profiles /tmp/tcmalloc;
6.查看安装是否成功(yum install lsof -y)
lsof -n | grep tcmalloc
3.内核参数优化
vim /etc/sysctl.conf
net.ipv4.tcp_max_tw_buckets = 6000
- net.ipv4.ip_local_port_range = 1024 65000
- net.ipv4.tcp_tw_recycle = 1
- net.ipv4.tcp_tw_reuse = 1
- net.ipv4.tcp_syncookies = 1
- net.core.somaxconn = 262144
- net.core.netdev_max_backlog = 262144
- net.ipv4.tcp_max_orphans = 262144
- net.ipv4.tcp_max_syn_backlog = 262144
- net.ipv4.tcp_synack_retries = 1
- net.ipv4.tcp_syn_retries = 1
- net.ipv4.tcp_fin_timeout = 1
- net.ipv4.tcp_keepalive_time = 30
- sysctl -p #立即生效
net.ipv4.tcp_max_tw_buckets #用来设定timewait的数量 默认是180000,这里改为6000
net.ipv4.ip_local_port_range #用来设定允许系统打开的端口范围最小值1024
net.ipv4.tcp_tw_recycle #用来设置启动timewait快速回收。
net.ipv4.tcp_tw_reuse #用来设置开启重用,允许将time-wait sockets重新用于新的tcp连接
net.ipv4.tcp_syncookies #用来开启syn cookies,当出现syn等待队列一处时,启用cookies处理
net.core.somaxconn #默认是128,参数用于调节系统同时发起的tcp连接数,在高并发的请求中,默认的值可能会导致连接超时或者重传,因此,需要结合并发请求数来调节此值。
net.core.netdev_max_backlog #表示当每个网络接口接受数据包的速率比内核处理这些包的速率快时,允许发送到队列的数据包的最大数目。
net.ipv4.tcp_max_orphans #用于设定系统中最多有多少个tcp套接字不被关联到任何一个用户文件句柄上。如果超过这个数字,孤立连接将立即被复位并打印出警告信息。这个限制值是为了防止简单的DOS攻击。不能过分依靠这个限制甚至人为减小这个值,更多的情况下应该增加这个值。
net.ipv4.tcp_max_syn_backlog #用于记录那些尚未收到客户端确认信息的连接请求的最大值。对于有128MB内存的系统而言,次参数默认值是1024,对小内存的系统则是128
net.ipv4.tcp_synack_retries #参数的值决定了内核放弃连接之前发送SYN+ACK包的数量
net.ipv4.tcp_syn_retries #表示在内核放弃简历连接之前发送SYN包的数量
net.ipv4.tcp_fin_timeout #决定了套接字保持在FIN-WAIT-2 状态的时间。默认值是60秒。正确设置这个值非常重要,有时即使一个负载很小的web服务器,也会出现大量的死套接字而产生内存溢出的风险。
net.ipv4.tcp_keepalive_time #表示当keepalive启动的时候,tcp发送keepalive消息的频度。默认值是2(单位是小时)
4.关于nginx连接限制(一个进程最大打开文件数)
worker_rlimit_nofile 65535; (nginx.conf)
#nginx配置改后,但系统默认最大1024,所以需要改变
#立即生效
ulimit -n 65535
ulimit -u 65535
#永久生效
#一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(ulimit -n)与nginx进程数相除,但是nginx分配请求并不是那么均匀,所以最好与ulimit -n的值保持一致。
vim /etc/security/limits.conf
* soft nproc 65535
* hard nproc 65535
* soft nofile 65535
* hard nofile 65535
0 0
- nginx 优化
- Nginx 优化
- nginx 优化
- nginx 优化
- nginx优化
- nginx 优化
- nginx优化
- nginx 优化
- nginx优化
- Nginx优化
- 优化 Nginx
- nginx优化
- Nginx优化
- Nginx优化
- Nginx优化
- Nginx优化
- nginx优化
- nginx 优化
- 三月英语——偷学小技巧
- c3p0数据库连接池老是报错,怒换dpcp连接池
- SQL经典教程 第二篇:高级(二)--约束
- C
- DL学习笔记【20】nn包中的各位Simple layers
- Nginx优化
- 开心的金明-动态规划-洛谷
- 客官留步!从Visual Studio看微软20年技术变迁
- BZOJ P3156: 防御准备
- 【Android实战】----基于Retrofit实现多图片/文件、图文上传
- Linux小知识
- java中的udp
- java中的Iterator和Iterable 区别
- linux 内核笔记--中断子系统之softirq