【问题标题】:Sending many record on client startup. Nginx 502 bad gateway too many open files在客户端启动时发送许多记录。 Nginx 502 bad gateway 打开的文件太多
【发布时间】:2016-03-17 22:10:05
【问题描述】:

我的 nodejs 应用在客户端启动时发送 > 800 个 mongodb 文档(仅在客户端第一次访问我的应用时执行)。

Nginx 作为节点服务器前的反向代理。

应用服务器规范

  • 数字海洋
  • CentOS 7.2
  • 2GB 内存
  • 2CPU

MongoDB 服务器规范

  • 数字海洋
  • Ubuntu 14.04
  • 512 内存
  • 1 个 CPU

nginx -v // nginx版本:nginx/1.8.1

nginx 配置

user  nginx;
worker_processes  2;
worker_rlimit_nofile 100480;

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

pid        /run/nginx.pid;

events {
  worker_connections  1024;
}

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;

  index   index.html index.htm;

  server {
    server_name 128.199.139.xxx;

    root /var/www/myapp/bundle/public;

    module_app_type node;

    module_startup_file main.js;

    module_env_var MONGO_URL mongodb://{username}:{password}@128.199.139.xxx:27017/;

    module_env_var ROOT_URL http://128.199.139.xxx;

    location / {
      proxy_pass http://128.199.139.xxx;
      proxy_http_version 1.1;

      #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      #proxy_set_header X-Real-IP $remote_addr;

      # pass the host header - http://wiki.nginx.org/HttpProxyModule#proxy_pass
      #proxy_set_header Host $host;

      # WebSocket proxying - from http://nginx.org/en/docs/http/websocket.html
      proxy_set_header Upgrade "upgrade";
      proxy_set_header Connection $http_upgrade;

      #add_header 'Access-Control-Allow-Origin' '*';
    }
  }

  server {
    listen       80 default_server;
    server_name  localhost;
    root         /usr/share/nginx/html;

    #charset koi8-r;

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

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    location / {
    }

    # redirect server error pages to the static page /40x.html
    #
    error_page  404              /404.html;
    location = /40x.html {
    }

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
    }
  }
}

错误日志

以下错误日志:

2016/03/17 09:46:00 [crit] 10295#0: accept4() failed (24: Too many open files)
2016/03/17 09:46:01 [crit] 10295#0: accept4() failed (24: Too many open files)
2016/03/17 09:46:01 [crit] 10295#0: accept4() failed (24: Too many open files)
.....many duplicate error as above
2016/03/17 09:47:35 [error] 10295#0: *4064 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 128.199.139.160, server: 128.199.139.1$
2016/03/17 09:47:35 [alert] 10295#0: accept4() failed (9: Bad file descriptor)
[ 2016-03-17 09:53:47.8144 10403/7f2833c9a700 age/Ust/UstRouterMain.cpp:422 ]: Signal received. Gracefully shutting down... (send signal 2 more time(s) to force shutdown)
[ 2016-03-17 09:53:47.8145 10403/7f2839500880 age/Ust/UstRouterMain.cpp:492 ]: Received command to shutdown gracefully. Waiting until all clients have disconnected...
[ 2016-03-17 09:53:47.8146 10403/7f2833c9a700 Ser/Server.h:464 ]: [UstRouter] Shutdown finished
2016/03/17 09:54:11 [alert] 10549#0: 1024 worker_connections are not enough
2016/03/17 09:54:11 [error] 10549#0: *1021 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 128.199.139.160, server: 128.199.139.1$
2016/03/17 09:54:12 [alert] 10549#0: 1024 worker_connections are not enough
2016/03/17 09:54:12 [error] 10549#0: *2043 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 128.199.139.160, server: 128.199.139.1$
2016/03/17 11:43:20 [alert] 10549#0: 1024 worker_connections are not enough
2016/03/17 11:43:20 [error] 10549#0: *3069 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 128.199.139.160, server: 128.199.139.1$
2016/03/17 13:49:54 [error] 10549#0: *3071 open() "/usr/share/nginx/html/robots.txt" failed (2: No such file or directory), client: 180.97.106.xx, server: localhost, request: "GET

感谢任何帮助或线索

【问题讨论】:

  • 投反对票并逃跑。真是一头驴

标签: node.js mongodb meteor nginx digital-ocean


【解决方案1】:

您应该更改工作进程的打开文件限制。 100480 对您的服务器来说太高了。

我总是使用以下规则:

worker_rlimit_nofile = 2 * worker_connections

所以在这种情况下,试试这个:

worker_rlimit_nofile = 2048

【讨论】:

    猜你喜欢
    • 2014-07-08
    • 2017-07-11
    • 1970-01-01
    • 2023-03-20
    • 2019-05-04
    • 2021-04-20
    • 2017-05-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多