LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

高性能 WEB 服务器 Nginx:全局配置

admin
2025年6月29日 18:40 本文热度 28

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 配置文件格式说明

  • 配置文件由指令与指令块构成

  • 每条指令以 ; 分号结尾,指令与值之间以空格符号分隔

  • 可以将多条指令放在同一行,用分号分隔即可,但可读性差,不推荐

  • 指令块以{ }大括号将多条指令组织在一起,且可以嵌套指令块

  • include 语句允许组合多个配置文件以提升可维护性

  • 使用 # 号添加注释,提高可读性

  • 使用 $ 符号使用变量

  • 变量分为 Nginx 内置变量和自定义变量,自定义变量用 set 指令定义

  • 部分指令的参数支持正则表达式

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 进程运行时的用户和组,默认值为 nobody nobodyworker_processes auto|N; #指定启动的 worker 进程数量,N 表示具体数字,auto 表示有几个CPU核心就开启几个进程worker_cpu_affinity auto|cpumask; #将worker进程绑定到指定的CPU核心,提升性能,默认不绑定                        # auto 表示自动绑定,nginx 1.9.10 版本开始支持                        # cpumask 写法                        #有几颗CPU核就写几个数字,默认是0,表示占位符,从右往左数,如果占位符值为1,则表示绑定                        # worker_processes 4;                        # worker_cpu_affinity 0001 0010 0100 1000;                        # 上述写法表示4个工作进程分别绑定到 0,1,2,3 这4个CPU核心上                        # worker_processes 2;                        # worker_cpu_affinity 0101 1010;                        # 上述写法表示2个工作进程分别绑定到 0/2,1/3 CPU核心上pid /path/file; #pid 文件路径,该文件中保存的是 master 进程号,服务启动时自动生成该文件worker_priority N; #worker 进程优先级,默认值为0,此处设定的是 nice 值,取值为 -20 到 19worker_rlimit_nofile N; #所有worker进程可以打开的文件描述符数量之和,默认不限制                        #此值并不仅仅是指nginx 与客户端的连接数,也包括与后端服务的连接数                        #比如一个客户端请求一个PHP资源,Nginx要维持与客户端连接,也要维持后端FastCGI连接daemon on|off#默认值为on,表示以后台守护进程方式运行,off 表示前台运行master_process on|off#默认值为on,表示以 master-worker 模式运行,off 只有master 进程,用于开发调试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://nginx.org/en/docs/events.html #事件驱动模型说明文档
设置 worker 进程数量
#当前有两颗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 进程与 cpu 核心绑定
#将 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
设置 pid 文件
#查看 pid 文件[root@ubuntu ~]# ps aux | grep nginx[root@ubuntu ~]# cat /etc/nginx/nginx.conf | grep pidpid /run/nginx.pid;[root@ubuntu ~]# cat /run/nginx.pid28470
#停止服务,pid 文件消失[root@ubuntu ~]# systemctl stop nginx.service[root@ubuntu ~]# cat /run/nginx.pidcat: /run/nginx.pid: No such file or directory
#启动服务,重新生成[root@ubuntu ~]# systemctl start nginx.service[root@ubuntu ~]# cat /run/nginx.pid28765
设置 worker 进程优先级
[root@ubuntu ~]# ps axo pid,cmd,ni | grep nginx28765 nginx: master process /usr/ 028766 nginx: worker process 028767 nginx: worker process 0
#调整 worker 进程优先级,值越小,优先级越高[root@ubuntu ~]# cat /etc/nginx/nginx.conf | grep worker_priorityworker_priority -20;[root@ubuntu ~]# systemctl reload nginx.service[root@ubuntu ~]# ps axo pid,cmd,ni | grep nginx28765 nginx: master process /usr/ 028828 nginx: worker process -2028829 nginx: worker process -20
设置 worker 进程文件描述符数量
#先将 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 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2025 ClickSun All Rights Reserved