如何设置nginx max打开文件?

Jia*_*ang 16 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
Run Code Online (Sandbox Code Playgroud)

特定进程的打开文件限制仍为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
Run Code Online (Sandbox Code Playgroud)

我已经通过谷歌搜索尝试了所有可能的解决方案但是徒劳无功.我错过了什么设置?

sic*_*ill 35

在CentOS上(在7.x上测试):

/etc/systemd/system/nginx.service.d/override.conf使用以下内容创建文件:

[Service]
LimitNOFILE=65536
Run Code Online (Sandbox Code Playgroud)

使用以下命令重新加载systemd守护程序:

systemctl daemon-reload
Run Code Online (Sandbox Code Playgroud)

将此添加到Nginx配置文件:

worker_rlimit_nofile 16384; (has to be smaller or equal to LimitNOFILE set above)
Run Code Online (Sandbox Code Playgroud)

最后重启Nginx:

systemctl restart nginx
Run Code Online (Sandbox Code Playgroud)

您可以验证它是否有效cat /proc/<nginx-pid>/limits.

  • 我确认这也适用于**Ubuntu Xenial**.谢谢你的方法. (2认同)
  • 此外,在 CentOS 7 上,您可以执行以下操作:“systemctl edit SERVICE_NAME”,在其中提供变量,保存该文件并重新加载服务。 (2认同)

Jia*_*ang 10

在发布此问题后的几分钟内我找到了答案......

# 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"
Run Code Online (Sandbox Code Playgroud)

/etc/security/limit.conf由PAM使用,因此它与www-data(它是nologin用户)无关.

  • / etc/default/nginx在哪里?我只看到/etc/nginx/nginx.conf,没有运气设置worker_rlimit_nofile (4认同)

adr*_*TNT 5

对于 nginx,只需编辑nginx.conf和设置即可worker_rlimit_nofile更改限制。我最初认为这是 nginx 的自我限制,但它增加了每个进程的限制:

worker_rlimit_nofile 4096;
Run Code Online (Sandbox Code Playgroud)

您可以通过获取 nginx 进程 ID(来自top -u nginx)进行测试,然后运行:

cat /proc/{PID}/limits
Run Code Online (Sandbox Code Playgroud)

查看当前限制

CentOS 7 上的另一种方法是通过systemctl edit SERVICE_NAME,在那里添加变量:

[Service]
LimitNOFILE=65536
Run Code Online (Sandbox Code Playgroud)

保存该文件并重新加载服务。