nginx 500(24:打开的文件太多)

DDI*_*DIT 8 nginx 500-error

[我在 Nginx 论坛上发布了这个,但一周后没有回复,所以在这里尝试]

我是 Linux 和 Nginx 新手,但已经学会了足够的知识来安装和运行它,并作为两个内部网络服务器的简单反向代理工作。这几个月来一直运行良好,但我最近开始收到 500 个错误。

这是/var/log/nginx/error.log (我已将我们的公司名称替换​​为“companyname.com”并将我们的公共 WAN IP 地址替换为

2020/02/10 15:17:49 [alert] 1069#1069: *1011 socket() failed (24: Too many open files) while connecting to upstream, client: 10.10.10.1, server: web1.companyname.com, request: "GET / HTTP/1.0", upstream: "https://<WANIP>:443/", host: "web1.companyname.com"

2020/02/10 15:21:41 [alert] 1069#1069: *2022 socket() failed (24: Too many open files) while connecting to upstream, client: 10.10.10.1, server: web2.companyname.com, request: "GET / HTTP/1.0", upstream: "https://<WANIP>:443/", host: "web2.companyname.com"

2020/02/10 15:33:28 [alert] 1084#1084: *19987 socket() failed (24: Too many open files) while connecting to upstream, client: 10.10.10.1, server: web2.companyname.com, request: "GET / HTTP/1.0", upstream: "https://<WANIP>:443/", host: "web2.companyname.com"

2020/02/10 15:34:16 [alert] 1084#1084: *39974 socket() failed (24: Too many open files) while connecting to upstream, client: 10.10.10.1, server: web1.companyname.com, request: "GET / HTTP/1.0", upstream: "https://<WANIP>:443/", host: "web1.companyname.com"

2020/02/10 15:50:30 [error] 1086#1086: *1 client intended to send too large body: 4294967295 bytes, client: 176.58.124.134, server: london.companyname.com, request: "GET /msdn.cpp HTTP/1.1", host: "<WANIP>"

2020/02/10 16:32:56 [alert] 1086#1086: *19989 socket() failed (24: Too many open files) while connecting to upstream, client: 10.10.10.1, server: web1.companyname.com, request: "GET / HTTP/1.0", upstream: "https://<WANIP>:443/", host: "web1.companyname.com"
Run Code Online (Sandbox Code Playgroud)

我在末尾添加了以下内容 /etc/security/limits.conf

nginx soft nofile 10000
nginx hard nofile 30000
Run Code Online (Sandbox Code Playgroud)

我已将以下内容添加到 /etc/sysctl.conf

fs.file-max=70000
Run Code Online (Sandbox Code Playgroud)

...并重新启动。但是,我在重新启动后立即遇到同样的问题。

Interestingly the IP address that appears in the log "176.58.124.134" I don't recognise and a quick google search suggests this is an abusive IP address. I can block at the firewall, but I'm not sure that's the problem.

Any tips, suggestions are grealy appreciated. Thanks.

Pio*_*asz 8

您的nginx服务器跟不上到达的请求数量。更准确地说,它缺乏可用的文件描述符来打开与上游的连接。

这些由三个参数调节:

  1. /proc/sys/fs/file-max 中的每个系统限制,它限制了整个系统的fd的最大数量。不要更改它,默认值足够高(800000在我的小型服务器上)。
  2. 每个进程的硬限制(RLIMIT_NOFILE),只能由root(或具有 的进程CAP_SYS_RESOURCES)设置。这通常相当高(ulimit -Hn, around 1000000),因此无需增加它。如果你想增加它的pam_limit.so配置/etc/security/limits.conf不会帮助你,因为nginx的是开始systemd(我猜,因为你不提你的发行),不使用PAM。您需要改为编辑nginx.service文件:

    systemctl edit --full nginx.service
    
    Run Code Online (Sandbox Code Playgroud)

    并将以下行添加到该[Service]部分:

    LimitNOFILE=your_limit
    
    Run Code Online (Sandbox Code Playgroud)
  3. 每个进程的软限制。nginx可以使用 Romeo 提到的指令自行增加它:

    worker_rlimit_nofile = your_limit;
    
    Run Code Online (Sandbox Code Playgroud)

每个限制不能高于前几点。

但是,除非您的网站在一夜之间变得非常流行,否则它更有可能遭受 DDOS 攻击。您可以通过使用http_limit_conn模块限制每个客户端的连接数来缓解它。文档中的示例配置应直接适用于您的案例:

http {
    limit_conn_zone $binary_remote_addr zone=addr:10m;
    limit_conn addr 10;
    ...
Run Code Online (Sandbox Code Playgroud)

这会将每个 IP 地址的连接数限制为 10。在大多数发行版中,您可以将这两个limit_*指令放在单独的文件中(例如/etc/nginx/conf.d/limit.conf),而无需修改 main nginx.conf.

  • LimitNOFILE=70000 修复问题 (2认同)