Linux 如何设置nginx max打开文件?
虽然我已经完成了以下设置,甚至重新启动了服务器:Linux 如何设置nginx max打开文件?,linux,ubuntu,nginx,limit,Linux,Ubuntu,Nginx,Limit,虽然我已经完成了以下设置,甚至重新启动了服务器: # head /etc/security/limits.conf -n2 www-data soft nofile -1 www-data hard nofile -1 # /sbin/sysctl fs.file-max fs.file-max = 201558 特定进程的打开文件限制仍为1024/4096: # ps aux | grep nginx root 983 0.0 0.0 85872 1348 ?
# head /etc/security/limits.conf -n2
www-data soft nofile -1
www-data hard nofile -1
# /sbin/sysctl fs.file-max
fs.file-max = 201558
特定进程的打开文件限制仍为1024/4096:
# ps aux | grep nginx
root 983 0.0 0.0 85872 1348 ? Ss 15:42 0:00 nginx: master process /usr/sbin/nginx
www-data 984 0.0 0.2 89780 6000 ? S 15:42 0:00 nginx: worker process
www-data 985 0.0 0.2 89780 5472 ? S 15:42 0:00 nginx: worker process
root 1247 0.0 0.0 11744 916 pts/0 S+ 15:47 0:00 grep --color=auto nginx
# cat /proc/984/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 15845 15845 processes
Max open files 1024 4096 files
Max locked memory 65536 65536 bytes
Max address space unlimited unlimited bytes
Max file locks unlimited unlimited locks
Max pending signals 15845 15845 signals
Max msgqueue size 819200 819200 bytes
Max nice priority 0 0
Max realtime priority 0 0
Max realtime timeout unlimited unlimited us
我尝试过谷歌搜索所有可能的解决方案,但都没有成功。我错过了什么设置?我在发布这个问题后几分钟就找到了答案
# cat /etc/default/nginx
# Note: You may want to look at the following page before setting the ULIMIT.
# http://wiki.nginx.org/CoreModule#worker_rlimit_nofile
# Set the ulimit variable if you need defaults to change.
# Example: ULIMIT="-n 4096"
ULIMIT="-n 15000"
PAM使用了/etc/security/limit.conf
,因此它与www-data
(它是nologin用户)无关。在CentOS上(在7.x上测试):
创建包含以下内容的文件/etc/systemd/system/nginx.service.d/override.conf
:
[Service]
LimitNOFILE=65536
使用以下命令重新加载systemd守护程序:
systemctl daemon-reload
将此添加到Nginx配置文件:
worker_rlimit_nofile 16384; (has to be smaller or equal to LimitNOFILE set above)
最后重新启动Nginx:
systemctl restart nginx
只需编辑
nginx.conf
并设置worker\rlimit\u nofile
应更改限制,就可以验证它是否与nginx的cat/proc//limits
一起工作。
我最初认为这是nginx的自我限制,但它增加了每个进程的限制:
worker_rlimit_nofile 4096;
您可以通过获取nginx进程ID(从top-u nginx
)进行测试,然后运行:
cat /proc/{PID}/limits
查看当前的限制
在CentOS 7上的另一种方法是通过systemctl edit SERVICE_NAME
,在那里添加变量:
[Service]
LimitNOFILE=65536
保存该文件并重新加载该服务。对于那些寻找预systemd Debian机器答案的人,nginx init脚本执行
/etc/default/nginx
。所以,添加行
ulimit -n 9999
将更改nginx守护程序的限制,而不会干扰init脚本
在前面的回答中添加
ULIMIT=“-n 15000”
与我的nginx版本不兼容。重复:?哪里是/etc/default/nginx?我只看到/etc/nginx/nginx.conf,没有运气设置worker\u rlimit\u nofile您真的尝试过自己的答案吗?我发现nginx在重新启动时返回无效选项:n
。我确认这也适用于Ubuntu Xenial。谢谢你的方法。也许有一天我们会为nginx的主进程设置一个“worker\rlimit\u nofile”:也许master\u rlimit\u nofile根据我的经验,你只需更改worker\r\u nofile
并重新加载nginx
。除非你达到了极限。我能够通过worker_rlimit_nofile
将限制提高到主进程设置的硬限制之上。此外,在CentOS 7上,您可以执行以下操作:systemctl edit SERVICE_NAME
,在那里提供变量,保存该文件并重新加载服务。