⑯.nginx 优化
调整打开文件数
vim /etc/security/limits.conf
//针对root用户
root soft nofile 65535
root hard nofile 65535
//所有用户, 全局
* soft nofile 25535
* hard nofile 25535
//对于Nginx进程
worker_rlimit_nofile 45535;
//root用户
//soft提醒
//hard限制
//nofile文件数配置项
//65535最大大小
Nginx性能优化
CPU亲和, 减少进程之间不断频繁迁移, 减少性能损耗
1.查看当前CPU物理状态
[root@nginx ~]# lscpu |grep "CPU(s)"
CPU(s): 24
On-line CPU(s) list: 0-23
NUMA node0 CPU(s): 0,2,4,6,8,10,12,14,16,18,20,22
NUMA node1 CPU(s): 1,3,5,7,9,11,13,15,17,19,21,23
//2颗物理cpu,没颗cpu12核心, 总共24核心
2.将Nginx worker进程绑到不同的核心上
//启动多少worker进程, 官方建议和cpu核心一致, 第一种绑定组合方式
#worker_processes 24;
#worker_cpu_affinity 000000000001 000000000010 000000000100 000000001000 000000010000 000000100000 000001000000 000010000000 000100000000 001000000000 010000000000 10000000000;
//第二种方式
#worker_processes 2;
#worker_cpu_affinity 101010101010 010101010101;
//最佳方式绑定方式
worker_processes auto;
worker_cpu_affinity auto;
3.查看nginx worker进程绑定至对应cpu
ps -eo pid,args,psr|grep [n]ginx
4.Nginx通用优化配置文件
[root@nginx ~]# cat nginx.conf
user nginx;
worker_processes auto;
worker_cpu_affinity auto;
error_log /var/log/nginx/error.log warn;
pid /run/nginx.pid;
#调整至1w以上,负荷较高建议2-3w以上
worker_rlimit_nofile 35535;
events {
use epoll;
#限制每个进程能处理多少个连接请求,10240x16
worker_connections 10240;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
# 统一使用utf-8字符集
charset utf-8;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
# Core module
sendfile on;
# 静态资源服务器建议打开
tcp_nopush on;
# 动态资源服务建议打开,需要打开keepalived
tcp_nodelay on;
keepalive_timeout 65;
# Gzip module
gzip on;
gzip_disable "MSIE [1-6]\.";
gzip_http_version 1.1;
# Virtal Server
include /etc/nginx/conf.d/*.conf;
}