Linux

打開的文件太多 (CentOS7) - 已經嘗試設置更高的限制

  • June 29, 2021

第一次設置 VPS——注意不要在這裡問,除非我做了盡職調查並提供了背景資訊。

在我的遠端 VPS 上,通過終端,我執行的幾乎所有命令都以一條Error: Too many open files消息結尾,我需要你的幫助才能繼續前進。

我執行:CentOS Linux release 7.6.1810 (Core)在具有 1 個 CPU 核心和 2048Mb RAM 的機器上。它已經設置了一個 LEMP 堆棧Nginx 1.16.1, PHP-FPM 7.3.9, MariaDb 10.4.8,用於一個簡單的 wordpress 站點。

我試過了:

  1. Google和論壇搜尋。
  2. 應用這些設置(每次通過控制面板手動重啟 VPS):

系統範圍的設置/etc/security/limits.conf

nginx       soft    nofile      1024
nginx       hard    nofile      65536
root        hard    nofile      65536
root        soft    nofile      1024

調整記憶體限制和上傳/etc/php.ini

memory_limit = 256M
file_uploads = On
upload_max_filesize = 128M
max_execution_time = 600
max_input_time = 600
max_input_vars = 3000

PHP rlimit 設置在/etc/php-fpm.d/www.conf

rlimit_files = 65535

在以下位置設置 NGINX 限制(和其他設置)nginx.conf

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
   worker_connections  10000;
}

worker_rlimit_nofile 100000;


http {
   include       /etc/nginx/mime.types;
   default_type  application/octet-stream;

   log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                     '$status $body_bytes_sent "$http_referer" '
                     '"$http_user_agent" "$http_x_forwarded_for"';

   access_log  /var/log/nginx/access.log  main;

   sendfile        on;
   #tcp_nopush     on;

   keepalive_timeout  65;
   client_body_buffer_size 128k;
   client_header_buffer_size 10k;
   client_max_body_size 100m;
   large_client_header_buffers 4 256k;

   #gzip  on;

   include /etc/nginx/conf.d/*.conf;
   include /etc/nginx/sites-enabled/*.conf;
   server_names_hash_bucket_size 64;
}

這是輸出cat /proc/sys/fs/file-nr

45216   0   6520154

這是輸出ps aux|grep nginx|grep -v grep

root       928  0.0  0.0  46440  1192 ?        Ss   00:25   0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx      929  0.0  0.2  50880  6028 ?        S    00:25   0:00 nginx: worker process
nginx     9973  0.0  0.1 171576  4048 ?        S    04:28   0:00 php-fpm: pool www
nginx     9974  0.0  0.1 171576  4048 ?        S    04:28   0:00 php-fpm: pool www
nginx     9975  0.0  0.1 171576  4048 ?        S    04:28   0:00 php-fpm: pool www
nginx     9976  0.0  0.1 171576  4048 ?        S    04:28   0:00 php-fpm: pool www
nginx     9977  0.0  0.1 171576  4052 ?        S    04:28   0:00 php-fpm: pool www

將使用者切換到nginxwithsu - nginx並檢查限制 with: ulimit -Sn返回1024 ulimit -Hn返回65536

lsof | wc -l命令返回:4776

希望您能幫助引導我朝著正確的方向解決文件過多的問題!

編輯

  • 以下命令顯示更多資訊:

service nginx restart

Redirecting to /bin/systemctl restart nginx.service
Error: Too many open files
Job for nginx.service failed because a configured resource limit was exceeded. See "systemctl status nginx.service" and "journalctl -xe" for details.
[root@pars ~]# systemctl status nginx.service
● nginx.service - nginx - high performance web server
  Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
 Drop-In: /usr/lib/systemd/system/nginx.service.d
          └─worker_files_limit.conf
  Active: failed (Result: resources) since Fri 2019-09-13 05:32:23 CEST; 14s ago
    Docs: http://nginx.org/en/docs/
 Process: 1113 ExecStop=/bin/kill -s TERM $MAINPID (code=exited, status=0/SUCCESS)
 Process: 1125 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Main PID: 870 (code=exited, status=0/SUCCESS)
  CGroup: /system.slice/virtualizor.service/system.slice/nginx.service

Sep 13 05:32:22 pars.work systemd[1]: Starting nginx - high performance web server...
Sep 13 05:32:22 pars.work systemd[1]: PID file /var/run/nginx.pid not readable (yet?) after start.
Sep 13 05:32:22 pars.work systemd[1]: Failed to set a watch for nginx.service's PID file /var/run/nginx.pid: Too many open files
Sep 13 05:32:23 pars.work systemd[1]: Failed to kill control group: Input/output error
Sep 13 05:32:23 pars.work systemd[1]: Failed to kill control group: Input/output error
Sep 13 05:32:23 pars.work systemd[1]: Failed to start nginx - high performance web server.
Sep 13 05:32:23 pars.work systemd[1]: Unit nginx.service entered failed state.
Sep 13 05:32:23 pars.work systemd[1]: nginx.service failed.

實際上,用完的不是打開的文件句柄,而是 inotify 手錶。

您可以在錯誤消息中看到這一點:

Sep 13 05:32:22 pars.work systemd[1]: Failed to set a watch for nginx.service's PID file /var/run/nginx.pid: Too many open files

要解決此問題,您需要提高系統可用的 inotify 監視數量。如果你真的檢查,你會發現它有一些低得離譜的值,比如 8192。

$ sysctl fs.inotify.max_user_watches
fs.inotify.max_user_watches = 8192

您可以通過在目錄中編輯或創建文件來持久地將 sysctl 設置fs.inotify.max_user_watches為更高的值。例如,我的系統有:/etc/sysctl.conf``/etc/sysctl.d

$ cat /etc/sysctl.d/10-user-watches.conf 
fs.inotify.max_user_watches = 1048576

然後用sysctl -p.

您可能不想直接使用該數字並導致核心分配記憶體來跟踪一百萬個使用者文件監視槽;相反,只需將目前值加倍,直到問題停止發生。

引用自:https://serverfault.com/questions/984066