1. 程式人生 > 其它 >修改nginx 程序最大檔案控制代碼數

修改nginx 程序最大檔案控制代碼數

修改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