How to set nginx max open files?

2020-05-13 20:20发布

Though I have done the following setting, and even restarted the server:

# 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

The open files limitation of specific process is still 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

I've tried all possible solutions from googling but in vain. What setting did I miss?

3条回答
Anthone
2楼-- · 2020-05-13 20:44

For nginx simply editing nginx.conf and setting worker_rlimit_nofile should change the limitation. I initially thought it is a self-imposed limit of nginx, but it increases limit per process:

worker_rlimit_nofile 4096;

You can test by getting a nginx process ID (from top -u nginx), then run:

cat /proc/{PID}/limits

To see the current limits

Another way on CentOS 7 is by systemctl edit SERVICE_NAME, add the variables there:

[Service]
LimitNOFILE=65536

save that file and reload the service.

查看更多
Luminary・发光体
3楼-- · 2020-05-13 20:49

On CentOS (tested on 7.x):

Create file /etc/systemd/system/nginx.service.d/override.conf with the following contents:

[Service]
LimitNOFILE=65536

Reload systemd daemon with:

systemctl daemon-reload

Add this to Nginx config file:

worker_rlimit_nofile 16384; (has to be smaller or equal to LimitNOFILE set above)

And finally restart Nginx:

systemctl restart nginx

You can verify that it works with cat /proc/<nginx-pid>/limits.

查看更多
男人必须洒脱
4楼-- · 2020-05-13 20:54

I found the answer in few minutes after posting this question...

# 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"

/etc/security/limit.conf is used by PAM, so it shoud be nothing to do with www-data (it's nologin user).

查看更多
登录 后发表回答