linux - nginx负载均衡器打开文件太多

标签 linux nginx load-balancing

我有一个负载均衡器,但遇到了此类错误:

2017/09/12 11:18:38 [crit] 22348#22348: accept4() failed (24: Too many open files)
2017/09/12 11:18:38 [alert] 22348#22348: *4288962 socket() failed (24: Too many open files) while connecting to upstream, client: x.x.x.x, server: example.com, request: "GET /xxx.jpg HTTP/1.1", upstream: "http://y.y.y.y:80/xxx.jpg", host: "example.com", referrer: "https://example.com/some-page"
2017/09/12 11:18:38 [crit] 22348#22348: *4288962 open() "/usr/local/nginx/html/50x.html" failed (24: Too many open files), client: x.x.x.x, server: example.com, request: "GET /xxx.jpg HTTP/1.1", upstream: "http://y.y.y.y:80/xxx.jpg", host: "example.com", referrer: "https://example.com/some-page"

nginx版本:nginx/1.10.1

操作系统:Debian GNU/Linux 8 (jessie)

有趣的是并不总是出错。大多数情况下,我会遇到 30-50 行错误,然后在 5-10 分钟内就没有任何错误。然后一旦错误再次出现......

这是我的 nginx.conf:

user                    www-data;
pid                     /usr/local/nginx/nginx.pid;
worker_processes        auto;

error_log               /var/log/nginx/error.log;

events {
    worker_connections  30000;
}

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

    fastcgi_buffers 16 16k;
    fastcgi_buffer_size 32k;

    proxy_buffer_size   128k;
    proxy_buffers   4 256k;
    proxy_busy_buffers_size   256k;
    client_max_body_size 500m;
    rewrite_log on;

    log_format          main    '$remote_addr - "$proxy_add_x_forwarded_for" -  [$time_local] "$request" '
                                '$status $body_bytes_sent "$http_referer" '
                                '"$http_user_agent" "$backend" '
                                'rt=$request_time uct="$upstream_connect_time" '
                                'uht="$upstream_header_time" urt="$upstream_response_time"';

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

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;

    geoip_country    /etc/nginx/geodb/GeoIP.dat;
    geoip_city       /etc/nginx/geodb/GeoLiteCity.dat;

    include             /etc/nginx/loadbalancer/loadbalancer.conf;

}

还有一些信息:

$ ulimit -Hn
65536
$ ulimit -Sn
65536
$ sysctl fs.file-nr
fs.file-nr = 2848   0   70000

我不知道这是否值得,但这个负载均衡器位于 cloudflare 后面。

最佳答案

我已将以下行添加到 nginx.conf 中:

worker_rlimit_nofile    20000;

现在可以了,修改后我没有收到任何错误。

希望对遇到同样问题的人有所帮助。

关于linux - nginx负载均衡器打开文件太多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56507072/

相关文章:

mysql - 如何在linux中找出当前使用的MySQL配置文件的位置

python + 如何验证变量是否包含字符

linux - 使用 docker 启动集线器后出现 "This site can’ t be returned

django - 使用 init.d 和自定义配置文件在 Ubuntu 12.04 上启动 nginx

macos - 在 macOS 上意外使用了端口 80,结果为 "It works"

nginx - nginx服务器配置:文件夹的子域

php - 错误: "500 internal server error" in virtualhost ubuntu 16. 04

azure - 基于 Azure 的服务的地理分布

image - Nginx 负载均衡

apache - HTTP 负载平衡 - 仅当第一个工作人员使用 mod_jk 失败时才 rdirect