Nginx网关超时没有太多负载在服务器上

时间:2017-09-21 07:40:18

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

我正在使用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/*;
}

3 个答案:

答案 0 :(得分:0)

也许您正在尝试访问从您的服务提供商处禁用的服务。

在我的情况下,我在digitalocean上托管了我的项目。我曾经在访问邮件类型时遇到相同的错误网关超时,因为digitalocean人员在一段时间后停止了我的Droplet上的smtp服务。

因此,您最好联系您的服务提供商并告诉他们。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

希望这对具有类似设置的人有所帮助。

设置: 在我的情况下,在GCP上,NGINX实例放置在子网内,对于该子网, 已为NGINX连接了NAT网关,以访问公共互联网。

由于此NGINX使用proxy_pass到与我们的后端API关联的域,因此它需要访问Internet。

遇到的错误: 同时发生多个请求后,NGINX错误日志上出现504超时。 总结:110 timeout while connecting to upstream

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

不需要更改NGINX配置。

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

GCP Cloud NAT Overview