php - AWS EC2 实例上的 Nginx + Php Fpm 扩展问题

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

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

实例类型:t3a.medium 该实例在负载均衡器后面运行。我们使用的是 RDS Aurora 数据库,其 CPU 利用率峰值约为 70%。 EC2 实例指标运行状况良好。编辑:实例内存消耗在可用 4 GB 中的 800 MB 以内

存在多个 502 服务器错误:错误网关,有时还有 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 无法访问系统资源。您可能需要根据硬件规范更改值。 所以,我们的最终配置如下所示:

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

    nginx 软 nofile 10000

    nginx硬nofile 30000

    根软nofile 10000

    root硬nofile 30000

    www-data 软 nofile 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

    内核.msgmax = 231072

    fs.file-max = 70000

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

    worker_processes 自动;

    worker_rlimit_nofile 30000;

    事件{ worker 连接8096; 多重接受; 使用epoll; epoll_事件512; }

    发送文件;

    tcp_nopush 开启;

    tcp_nodelay 开启;

    keepalive_timeout 65;

    gzip 开启;

    gzip_comp_level 2;

    gzip_min_length 1000;

    gzip_types 文本/xml 文本/css;

    gzip_http_版本 1.1;

    gzip_vary 开启;

    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-data

    listen.backlog = 4096

    listen.owner = www-data

    listen.group = www-data

    ;监听模式=0660

    pm = 静态

    pm.max_children = 1000

关于php - AWS EC2 实例上的 Nginx + Php Fpm 扩展问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64268764/

相关文章:

amazon-web-services - rsa公钥没有这样的文件或目录?

azure - 利用云服务对某个时间窗口内的实时统计数据进行聚合和分组,以触发通知

apache - 静态 Web 服务器的资源使用情况

nginx - 如何将 $request_uri 添加到 auth_request 模块?

php检查2个变量是否相等并且都等于0

javascript - Ajax - 通过 ajax 将输入文件和附加变量发送到 php 文件

php - Postgres pg_dump 每次都以不同的顺序转储数据库

amazon-web-services - AWS EC2 - 多个(公共(public)/私有(private))网络接口(interface)

php - MySQL 查询执行错误

php - 如何更改 OSX 上的 docker 安装卷的权限以与运行 Laravel 5.1 的 Nginx Web 服务器一起使用?