【问题标题】:Nginx + Php Fpm scaling issues on AWS EC2 instanceAWS EC2 实例上的 Nginx + Php Fpm 扩展问题
【发布时间】:2021-01-23 20:51:44
【问题描述】:

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

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

有多个502 Server error: Bad Gateway,有时还有500520 错误。

错误 1:

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

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

【问题讨论】:

  • 这看起来你的服务器可能内存不足,因为它们产生了 php-fpm 子节点。这将导致间歇性的 502/504,并且不会使实例在 AWS 控制面板中显得不健康。假设您在测试环境中,您可以编写一个脚本来检查服务器上的可用内存,并让它在它低于某个值时重新启动 fpm 服务。我不会在 prod 中这样做,但它至少会缩小问题的范围。
  • 另一个潜在问题是您可能正在耗尽极光中可用的连接数。您允许 8000 名工作人员,但 RDS 根据实例大小强制执行 max_connections - 检查 this document 并确认您没有违反该规定
  • @TheGentleman 在可用的 4 GB 内存中,它永远不会超过 800 MB 的使用量。我们已经确认在测试运行期间。
  • @mcfinnigan,谢谢调查

标签: php amazon-web-services nginx amazon-ec2


【解决方案1】:

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

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

    nginx 软文件 10000

    nginx hard nofile 30000

    root soft nofile 10000

    root hard nofile 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

    kernel.msgmnb = 231072

    kernel.msgmax = 231072

    fs.file-max = 70000

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

    worker_processes 自动;

    worker_rlimit_nofile 30000;

    事件{ 工人连接 8096; 多接受; 使用 epoll; epoll_events 512; }

    发送文件;

    tcp_nopush 开启;

    tcp_nodelay 开启;

    keepalive_timeout 65;

    gzip 开启;

    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] .";

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

    emergency_restart_threshold = 10

    emergency_restart_interval = 1m

    process_control_timeout = 10s

    rlimit_files = 10000

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

    用户 = www-数据

    组 = www-数据

    listen.backlog = 4096

    listen.owner = www-data

    listen.group = www-data

    ;listen.mode = 0660

    pm = 静态

    pm.max_children = 1000

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-23
    • 2022-01-14
    • 2013-02-01
    • 2022-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多