ami linux nginx 502坏网关

时间:2017-07-17 06:41:51

标签: php nginx

我正在使用AWS m4 large(2vCPU,8Gib内存)AMI Linux操作系统和nginx&像这样的php-fpm配置

nginx.conf

user nginx;
    worker_processes 2;
    error_log /var/log/nginx/error.log;
    pid /var/run/nginx.pid;

    # Load dynamic modules. See /usr/share/nginx/README.dynamic.
    include /usr/share/nginx/modules/*.conf;

    events {
        worker_connections 1024;
    }

    http {
        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;
            access_log off;
            log_not_found off;
            error_log /var/log/nginx/error.log warn;

        #keepalive_timeout   65;
        types_hash_max_size 2048;

       # server_names_hash_bucket_size 1024;
        server_names_hash_max_size 1024;

    gzip on;
    gzip_disable "msie6";
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_min_length 1100;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript
server_names_hash_bucket_size 100;

#limit_req_zone $binary_remote_addr zone=one:10m rate=5r/s;
#limit_req zone=one burst=10;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    include /etc/nginx/servers/*.conf;
}

www.conf

[www]

user = apache
group = apache

listen= /var/run/php-fpm/php-fpm-7.0.sock

listen.owner = nginx
listen.group = nginx
listen.mode = 0660

listen.allowed_clients = 127.0.0.1

pm = dynamic
pm.max_children = 140
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500

;pm = ondemand
;pm.max_children = 500
;pm.max_requests = 10000

pm.status_path = /status
ping.path = /ping

;slowlog = /var/log/php-fpm/www-slow.log
;request_slowlog_timeout = 5s
request_terminate_timeout = 1m
rlimit_core = unlimited

php_admin_value[error_log] = /var/log/php-fpm/7.0/www-error.log
php_admin_flag[log_errors] = on

php_value[session.save_handler] = files
php_value[session.save_path]    = /var/lib/php/7.0/session
php_value[soap.wsdl_cache_dir]  = /var/lib/php/7.0/wsdlcache

有时它会在高流量负载时显示错误502错误网关和504网关超时, 如何处理和优化高流量的php-fpm,以便它保持稳定

感谢。

1 个答案:

答案 0 :(得分:1)

您得到502,这是因为您的服务器太忙而无法响应任何请求。具体来说,请求客户端无法在其最大读取时间限制内获得读取结果。

您得到了504,来自nginx。具体来说,您的nginx无法获得其最大读取时间限制的读取结果。其配置如下:

fastcgi_connect_timeout 600;

fastcgi_send_timeout 600;

fastcgi_read_timeout 600;

在高并发情况下,大流量可能会带来诸如高CPU使用率和平均负载以及平均负载的症状。如何处理高并发很复杂。

就您而言,

1)您可以合理地计划资源。如果需要,添加一些服务器,并使您的群集可以处理三倍的流量。

2)您的nginx.conf中的限制请求:

http {
    limit_req_zone $binary_remote_addr zone=myRateLimit:10m rate=10r/s;
}
server {
    location / {
        limit_req zone=myRateLimit;
        proxy_pass http://your_upstream;
    }
}

3)其他方法可能会优化您的系统架构