修改nginx 进程最大文件句柄数
1. 修改 cat /etc/sysctl.conf 中 fs.file-max参数
fs.file-max=65535
执行 sysctl -p 让修改的内核参数生效
2.修改linux系统参数。追加下面内容到 /etc/security/limits.conf 文件
* soft nofile 65535
* hard nofile 65535
命令如下:
echo "* soft nofile 65535" >> /etc/security/limits.conf
echo "* hard nofile 65535" >> /etc/security/limits.conf
3. 修改 nginx systemd 启动参数
cat /usr/lib/systemd/system/nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
LimitNOFILE=65535 // 添加这一行
[Install]
WantedBy=multi-user.target
4. 重新启动 nginx 进程
systemctl daemon-reload
systemctl restart nginx
5. 查看nginx 进程 pid
ps aux|grep nginx
root 16257 0.0 0.0 123140 2488 ? Ss 16:48 0:00 nginx: master process /usr/sbin/nginx
nginx 16258 0.2 0.0 123588 4060 ? S 16:48 0:02 nginx: worker process
nginx 16259 0.3 0.0 123588 4052 ? S 16:48 0:03 nginx: worker process
nginx 16260 1.8 0.0 123984 4552 ? S 16:48 0:16 nginx: worker process
nginx 16261 0.6 0.0 123588 4056 ? S 16:48 0:06 nginx: worker process
6. 查看进程的 limits 数值, 其中 max open files 已经调整为 65535
cat /proc/16257/limits
Limit Soft Limit Hard Limit Units
Max cpu time unlimited unlimited seconds
Max file size unlimited unlimited bytes
Max data size unlimited unlimited bytes
Max stack size 8388608 unlimited bytes
Max core file size 0 unlimited bytes
Max resident set unlimited unlimited bytes
Max processes 31217 31217 processes
Max open files 65535 65535 files
Max locked memory 65536 65536 bytes
Max address space unlimited unlimited bytes
Max file locks unlimited unlimited locks
Max pending signals 31217 31217 signals
Max msgqueue size 819200 819200 bytes
Max nice priority 0 0
Max realtime priority 0 0
Max realtime timeout unlimited unlimited us