高性能 WEB 服务器 Nginx:全局配置
|
admin
2025年6月29日 18:40
本文热度 1054
|
2 Nginx 配置
2.1 配置文件说明
#ubuntu2204 中使用 apt 安装的 nginx 的配置文件[root@ubuntu ~]# ls -l /etc/nginx/total 64drwxr-xr-x 2 root root 4096 May 31 2024 conf.d #子配置文件目录,在主配置文件中被包含,默认为空-rw-r--r-- 1 root root 1125 May 31 2024 fastcgi.conf #FastCGI配置文件,定义相关配置项,被引用-rw-r--r-- 1 root root 1055 May 31 2024 fastcgi_params #FastCGI配置文件,定义相关配置项,被引用-rw-r--r-- 1 root root 2837 May 31 2024 koi-utf #用于转换KOI8-R编码的字符集到UTF-8-rw-r--r-- 1 root root 2223 May 31 2024 koi-win #用于转换KOI8-U编码的字符集到UTF-8-rw-r--r-- 1 root root 3957 May 31 2024 mime.types #包含文件扩展名和相应MIME类型的映射drwxr-xr-x 2 root root 4096 May 31 2024 modules-available #模块配置文件目录drwxr-xr-x 2 root root 4096 Jan 21 00:36 modules-enabled #当前生效的模块配置文件目录,用软链接指向真正的文件-rw-r--r-- 1 root root 1447 May 31 2024 nginx.conf #主配置文件-rw-r--r-- 1 root root 180 May 31 2024 proxy_params #反向代理配置文件-rw-r--r-- 1 root root 636 May 31 2024 scgi_params #SCGI配置文件,SCGI和FastCGI类似,都是一种协议drwxr-xr-x 2 root root 4096 Jan 21 00:36 sites-available #所有域名配置文件drwxr-xr-x 2 root root 4096 Jan 21 00:36 sites-enabled #生效的域名配置文件,用软连接指向site-available中的文件drwxr-xr-x 2 root root 4096 Jan 21 00:36 snippets #通用的配置片段目录,需要时被引用-rw-r--r-- 1 root root 664 May 31 2024 uwsgi_params #配置 Nginx uWSGI 模块的默认参数的文件-rw-r--r-- 1 root root 3071 May 31 2024 win-utf #编码转换映射转化文件
Nginx 配置文件格式说明
https://nginx.org/en/docs/ https://nginx.org/en/docs/dirindex.html https://nginx.org/en/docs/varindex.html
2.2 全局配置
查看默认主配置文件内容
[root@ubuntu ~]# cat /etc/nginx/nginx.conf | grep -Ev "#|^$"user www-data;worker_processes auto;pid /run/nginx.pid;include /etc/nginx/modules-enabled/*.conf;events { worker_connections 768;}http { sendfile on; tcp_nopush on; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; ssl_prefer_server_ciphers on; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; gzip on; include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*;}
#主配置文件有以下几部分组成[root@ubuntu ~]# cat /etc/nginx/nginx.conf#全局配置............#事件驱动相关的配置events { ...... ......}#http/https 协议相关配置段http { ...... ......}#mail 协议相关配置段, 此部份默认被注释mail { ...... ...... server { ...... ...... }}# stream 协议相关配置,默认无此部份stream { ...... ......}
全局配置
全局配置段常见的配置指令分类
user user [group]; worker_processes auto|N; worker_cpu_affinity auto|cpumask; pid /path/file; worker_priority N; worker_rlimit_nofile N; daemon on|off; master_process on|off; include /path/file;
#evnets 配置项events { worker_connections N; #默认值512,单个worker进程所支持的最大并发连接数,总的并发数用此值乘以worker 进程数 multi_accept on|off; #默认值 off,on 表示worker 进程一次可以接收多个新的连接请求,off 表示 worker 进程 一次只能接收一个新连接,建议设置为 on accept_mutex on|off; #默认值 off,表示master 进程获得客户端请求后,会通知所有的 worker 进程,由当前状态最佳的worker 进程处理请求,此过程也被称为 惊群,on 表示 master 进程轮流给 worker 进程分配要处理的连接,此处建议设置为 on use method; #指定事件驱动模型,默认不设置此项,nginx 会根据当前系统自行选择 #可用值select|pool|kqueue|epoll|/dev/poll|eventport}https:
#当前有两颗CPU,每颗CPU 一个核心[root@ubuntu ~]# cat /proc/cpuinfo | grep processorprocessor : 0processor : 1
#此项值为 auto,则应该启动两个 worker 进程[root@ubuntu ~]# cat /etc/nginx/nginx.conf | grep worker_processesworker_processes auto;
#重启服务并查看进程[root@ubuntu ~]# systemctl restart nginx.service[root@ubuntu ~]# ps aux | grep nginx
#修改配置值为 3,重启后再次查看[root@ubuntu ~]# cat /etc/nginx/nginx.conf | grep worker_processesworker_processes 3;[root@ubuntu ~]# systemctl restart nginx[root@ubuntu ~]# ps aux | grep nginx
#将 worker_processes 值改回 auto,查看当前 worker 进程与 CPU 核心的绑定关系[root@ubuntu ~]# ps axo pid,cmd,psr | grep nginx28470 nginx: master process /usr/ 0 #master 进程绑定在 0 号 CPU 上28471 nginx: worker process 1 #28471 绑定在 1 号 CPU 上28472 nginx: worker process 0 #28472 绑定在 2 号 CPU 上
#远程客户端进行压测[root@ubuntu ~]# while true; do ab -c 1000 -n 5000 http://10.0.0.206/;sleep 1;done
#服务端woker 进程会切 CPU,并不是一成不变的对应关系,这样会需要更大的开销[root@ubuntu ~]# ps axo pid,cmd,psr | grep nginx28470 nginx: master process /usr/ 028471 nginx: worker process 028472 nginx: worker process 1[root@ubuntu ~]# ps axo pid,cmd,psr | grep nginx28470 nginx: master process /usr/ 028471 nginx: worker process 028472 nginx: worker process 0
#将woker 进程与 CPU 核心进行绑定[root@ubuntu ~]# cat /etc/nginx/nginx.confworker_processes 2;worker_cpu_affinity 01 10;
#重载服务,开启压测并观察,worker 进程不再切换CPU[root@ubuntu ~]# systemctl reload nginx.service[root@ubuntu ~]# ps axo pid,cmd,psr | grep nginx28470 nginx: master process /usr/ 128646 nginx: worker process 028647 nginx: worker process 1
[root@ubuntu ~][root@ubuntu ~]pid /run/nginx.pid;[root@ubuntu ~]28470
[root@ubuntu ~][root@ubuntu ~]cat: /run/nginx.pid: No such file or directory
[root@ubuntu ~][root@ubuntu ~]28765
[root@ubuntu ~]28765 nginx: master process /usr/ 028766 nginx: worker process 028767 nginx: worker process 0
[root@ubuntu ~]worker_priority -20;[root@ubuntu ~][root@ubuntu ~]28765 nginx: master process /usr/ 028828 nginx: worker process -2028829 nginx: worker process -20
#先将 worker 进程数调整为 1 ,便于观察[root@ubuntu ~]# ps aux | grep nginxroot 29959 0.0 0.0 55224 1680 ? Ss 20:02 0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;www-data 29960 0.0 0.2 55856 5452 ? S 20:02 0:00 nginx: worker process[root@ubuntu ~]# ls -l /proc/29960/fdtotal 0lrwx------ 1 www-data www-data 64 Jan 21 16:03 0 -> /dev/nulllrwx------ 1 www-data www-data 64 Jan 21 16:03 1 -> /dev/nulllrwx------ 1 www-data www-data 64 Jan 21 16:03 10 -> 'socket:[598353]'lrwx------ 1 www-data www-data 64 Jan 21 16:03 11 -> 'anon_inode:[eventpoll]'lrwx------ 1 www-data www-data 64 Jan 21 16:03 12 -> 'anon_inode:[eventfd]'l-wx------ 1 www-data www-data 64 Jan 21 16:03 2 -> /var/log/nginx/error.logl-wx------ 1 www-data www-data 64 Jan 21 16:03 3 -> /var/log/nginx/access.loglrwx------ 1 www-data www-data 64 Jan 21 16:03 6 -> 'socket:[595559]'lrwx------ 1 www-data www-data 64 Jan 21 16:03 7 -> 'socket:[595560]'l-wx------ 1 www-data www-data 64 Jan 21 16:03 8 -> /var/log/nginx/error.log
#客户端开启压测,观察服务端会开启大量的文件描述符,一个描述符对应一个客户端请求[root@ubuntu ~]# while true; do ab -c 1000 -n 5000 http://10.0.0.206/;sleep 1;done
#没有设置,资源限制来自于 systemd 的默认配置[root@ubuntu ~]# cat /proc/29959/limits | grep filesMax open files 1024 524288 files[root@ubuntu ~]# cat /proc/29960/limits | grep filesMax open files 1024 524288 files
#查看systemd的默认限制[root@ubuntu ~]# systemctl show | grep FILEDefaultLimitNOFILE=524288 #默认硬限制DefaultLimitNOFILESoft=1024 #默认软限制,软限制不能超过硬限制
#直接查看文件也可以[root@ubuntu ~]# cat /etc/systemd/system.conf | grep FILE#DefaultLimitNOFILE=1024:524288
#在 service 文件中设置[root@ubuntu ~]# cat /lib/systemd/system/nginx.service............[Unit]......[Service]......LimitNOFILE=10000:50000 #soft 10000, hard 50000
#重载service,并重启服务[root@ubuntu ~]# systemctl daemon-reload[root@ubuntu ~]# systemctl restart nginx.service
#查看进程[root@ubuntu ~]# ps aux | grep nginxroot 30317 0.0 0.0 55224 1676 ? Ss 20:32 0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;www-data 30318 0.0 0.2 55856 5680 ? S 20:32 0:00 nginx: worker process
#查看限制[root@ubuntu ~]# cat /proc/30317/limits | grep filesMax open files 10000 50000 files[root@ubuntu ~]# cat /proc/30318/limits | grep filesMax open files 10000 50000 files
#在nginx 配置文件中设置[root@ubuntu ~]# cat /etc/nginx/nginx.confworker_rlimit_nofile 60000;
#重启服务[root@ubuntu ~]# systemctl reload nginx.service
#生成了新的 worker 进程[root@ubuntu ~]# ps aux | grep nginxroot 30317 0.0 0.3 55356 5984 ? Ss 20:32 0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;www-data 30380 0.0 0.2 55984 5724 ? S 20:34 0:00 nginx: worker process
#master 进程中的限制没有变化[root@ubuntu ~]# cat /proc/30317/limits | grep filesMax open files 10000 50000 files
#worker 进程中的限制发生了变化[root@ubuntu ~]# cat /proc/30380/limits | grep filesMax open files 60000 60000 files
#如果不是用 systemd 来管理 nginx 服务,而是直接用二进制命令启动#那么,全局文件描述符要在 /etc/security/limits.conf 中进行设置
— END —
阅读原文:原文链接
该文章在 2025/7/1 22:59:25 编辑过