首页 > 解决方案 > AWS EC2 实例上的 Nginx + Php Fpm 扩展问题

问题描述

我们正在我们应用程序的网页上使用 locust(1000 个用户) 执行负载测试。

实例类型:t3a.medium 该实例在负载均衡器后面运行。我们正在使用 RDS Aurora 数据库,该数据库的 CPU 利用率达到 70% 左右。EC2 实例指标正常。编辑:实例内存消耗在可用 4 GB 中的 800 MB 以内

有多个错误502 Server error: Bad Gateway,有时500也有520错误。

错误一:

2020/10/08 16:58:21 [error] 4344#4344: *41841 connect() to unix:/var/run/php/php7.2-fpm.sock failed (11: Resource temporarily unavailable) while connecting to upstream, client: <PublicIP>, server: <Domain name>, request: "GET <webpage> HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.2-fpm.sock:", host: "<Domain name>"

错误 2(警报):

2020/10/08 19:15:11 [alert] 9109#9109: *105735 socket() failed (24: Too many open files) while connecting to upstream, client: <PublicIP>, server: <Domain name>, request: "GET <webpage> HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.2-fpm.sock:", host: "<Domain name>"

列出配置文件:

Nginx 配置

server {
        listen 80;
        listen [::]:80;

        root /var/www/####;
        index index.php;

    access_log /var/log/nginx/###access.log;
    error_log  /var/log/nginx/####error.log ;   

    server_name  #####;

        client_max_body_size 100M;

        autoindex off;

     location / {
        try_files $uri $uri/ /index.php?$query_string;
      }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_intercept_errors on;
        fastcgi_index index.php;
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
    }
}

/etc/nginx/nginx.conf

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  8096;
    multi_accept        on;
    use                 epoll;
    epoll_events        512;
}


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;
    tcp_nopush     on;
    tcp_nodelay on;

    keepalive_timeout  65;

    gzip  on;
    gzip_comp_level  2;
    gzip_min_length  1000;
    gzip_types  text/xml text/css;
    gzip_http_version 1.1;
    gzip_vary  on;
    gzip_disable "MSIE [4-6] \.";
    
include /etc/nginx/conf.d/*.conf;
}

/etc/php/7.2/fpm/php-fpm.conf

  emergency_restart_threshold 10
  emergency_restart_interval 1m
  process_control_timeout 10s

php-fpm 重要参数:

user = www-data
group = www-data
listen = /run/php/php7.2-fpm.sock
listen.owner = www-data
listen.group = www-data
;listen.mode = 0660
pm = static
pm.max_children = 300

/etc/security/limits.conf

nginx       soft    nofile  30000
nginx       hard    nofile  50000

/etc/sysctl.conf

net.nf_conntrack_max = 131072
net.core.somaxconn = 131072
net.core.netdev_max_backlog = 65535
kernel.msgmnb = 131072
kernel.msgmax = 131072
fs.file-max = 131072

我们缺少什么?谁能指出正确的方向?

标签: phpamazon-web-servicesnginxamazon-ec2

解决方案


所以我们能够解决这个问题。问题是php-fpm无权访问系统资源。您可能需要根据硬件规格更改值。因此,我们的最终配置如下所示:

  1. 在 /etc/security/limits.conf 中,添加以下行:

    nginx 软文件 10000

    nginx 硬文件 30000

    根软文件 10000

    根硬盘文件 30000

    www-data 软文件 10000

    www-data hard nofile 30000

  2. 在 /etc/sysctl.conf 中,添加以下值

    net.nf_conntrack_max = 231072

    net.core.somaxconn = 231072

    net.core.netdev_max_backlog = 65535

    内核.msgmnb = 231072

    kernel.msgmax = 231072

    fs.file-max = 70000

  3. 在 /etc/nginx/nginx.conf 中,更改或添加,因此最终它应该具有这些值(请根据您的用例和服务器容量更改它们):

    worker_processes 自动;

    worker_rlimit_nofile 30000;

    事件 {worker_connections 8096; 多接受;使用 epoll;epoll_events 512;}

    发送文件;

    tcp_nopush 开启;

    tcp_nodelay 开启;

    keepalive_timeout 65;

    gzip打开;

    gzip_comp_level 2;

    gzip_min_length 1000;

    gzip_types 文本/xml 文本/css;

    gzip_http_version 1.1;

    gzip_vary on;

    gzip_disable "MSIE [4-6] .";

  4. 在 /etc/php/7.2/fpm/php-fpm.conf 中,将值更改为如下所示:

    紧急重启阈值 = 10

    紧急重启间隔 = 1m

    process_control_timeout = 10s

    rlimit_files = 10000

  5. 在 /etc/php/7.2/fpm/pool.d/www.conf 中,将值更改为如下所示:

    用户 = www-数据

    组 = www-数据

    听积压 = 4096

    listen.owner = www-数据

    listen.group = www-数据

    ;listen.mode = 0660

    下午 = 静态

    pm.max_children = 1000


推荐阅读