【问题标题】:Nginx gateway timeout without much load on serverNginx 网关超时,服务器上没有太多负载
【发布时间】:2018-03-02 11:08:32
【问题描述】:

我正在使用 NGINX 和 UWSGI 为基于 python/Django 的 API 后端提供动力,负载均衡器连接到 AWS 自动缩放组,我的服务器在日常工作中运行良好,但有时在一两个月内开始从服务器获取 504 或 502持续一天或更长时间。我的服务器上的负载低于常规,内存使用情况很好,但仍然得到 502 或 504

使用 ubuntu 14.0.4

这是我的 nginx 配置的样子

user www-data;
worker_processes 2;
pid /run/nginx.pid;
events {
    worker_connections 2048;
    multi_accept on;
    use epoll;
}
worker_rlimit_nofile 40000;
http {
    ##
    # Basic Settings
    ##
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 50s;
keepalive_requests 1024;
    types_hash_max_size 2048;
    # server_tokens off;
    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;
    client_body_timeout 12;
    client_header_timeout 12;
    send_timeout 10;
    limit_conn_zone $binary_remote_addr zone=addr:10m;
    limit_conn addr 20;
    open_file_cache max=5000 inactive=20s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 2;
    open_file_cache_errors on;
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
 ##
    # Logging Settings
    ##
    log_format json '{"method": "$request_method", "uri": 
 "$request_uri", "status": $status, "request_time": $request_time, 
 "upstream_response_time": $upstream_response_time, "body_bytes_sent": 
 $body_bytes_sent, "http_user_agent": "$http_user_agent", "remote": 
"$remote_addr", "upstream": "$upstream_addr", "proxy": "$proxy_host"}';
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;
    ##
    # Gzip Settings
    ##
    gzip on;
    gzip_disable "msie6";
    gzip_vary on;
gzip_proxied any;
    gzip_comp_level 9;
    #gzip_buffers 16 8k;
    #gzip_http_version 1.1;
    #gzip_types text/plain text/css application/json;
    gzip_types text/plain text/css application/json application/x-
javascript text/xml application/xml application/xml+rss text/javascript;
    ##
    # nginx-naxsi config
    ##
    # Uncomment it if you installed nginx-naxsi
    ##
    #include /etc/nginx/naxsi_core.rules;
    ##
    # nginx-passenger config
    ##
    # Uncomment it if you installed nginx-passenger
    ##
 include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

【问题讨论】:

  • 您使用的是 ELB 还是您自己的负载均衡器? 502/504 错误是由负载均衡器还是由您的 nginx 服务器返回的?您是否有权访问 nginx 和 uwsgi 日志?
  • 我们正在使用 ELB,Nginx 返回 520/504,有时也在 ELB 上。而不是ELB。我们确实可以访问 Nginx 和 UWSGI 日志
  • 因此,如果您查看产生 502 错误的请求,您会在 nginx 和 uwsgi 日志中看到这些请求的什么内容?一定有错误记录在某处。

标签: python django amazon-web-services nginx uwsgi


【解决方案1】:

首先,确定哪个服务器返回超时错误。 负载均衡器或 Nginx Worker

【讨论】:

    【解决方案2】:

    希望这可以帮助一些具有类似设置的人。

    设置: 在我的案例中,在 GCP 上,一个 NGINX 实例被放置在一个子网中,对于这个子网, 为 NGINX 连接了一个 NAT 网关以访问公共互联网。

    由于这个 NGINX 使用 proxy_pass 到与我们的后端 API 关联的域,它需要访问互联网。

    遇到的错误: 同时多次请求后,我在 NGINX 错误日志上收到 504 超时。 综上所述:110 timeout while connecting to upstream

    解决方案: 增加 NAT 网关上每个 VM 实例的最小端口数。

    无需更改 NGINX 配置。

    每个虚拟机实例的最小端口数最初设置为 64,当请求数量增加时,这还不够。

    GCP Cloud NAT Overview

    【讨论】:

      猜你喜欢
      • 2022-01-10
      • 2016-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-05
      • 1970-01-01
      • 2011-01-17
      相关资源
      最近更新 更多