Назад | Перейти на главную страницу

Как повысить производительность Nginx при высокой загрузке PHP?

Интересно, как я могу улучшить производительность своего сервера? Он настроен с Ubuntu 18.04 (LTS), 4 ГБ ОЗУ, 100 ГБ SSD, 2 ЦП, с Nginx 1.14.0 и PHP 7.2-fpm для управления запуском и обслуживанием сайта, который генерирует динамические страницы из базы данных. В PHP включен кеш OP. Я хотел бы улучшить текущую настройку без Varnish или прокси, просто Nginx с настройкой PHP.

Мониторинг при нагрузочном тестировании с 500 одновременными посетителями показывает, что загрузка ЦП сразу же повышается до 100%, когда PHP использует большинство, 65% и 15% SQL. Использование памяти составляет около 2,5 ГБ, из которых 0,75 ГБ свободно, а файл подкачки не трогается. При посещении сайта во время теста выдает 502 ошибки.

Журналы, php7.2-fpm.log:

[12-May-2020 17:21:13] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 8 children, there are 0 idle, and 9 total children
[12-May-2020 17:21:14] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 16 children, there are 0 idle, and 10 total children
[12-May-2020 17:21:15] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 32 children, there are 0 idle, and 11 total childr

И в /var/log/nginx/error.log

2020/05/12 17:22:35 [error] 2936#2936: *25711 connect() to unix:/var/run/php/php7.2-fpm.sock failed (11: Resource temporarily unavailable) while connecting to upstream, client: [IPADDRESS], server: [DOMAINNAME], request: "GET /[URL] HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.2-fpm.sock:", host: "[HOSTNAME]"

Ниже приведены конфигурации Nginx и PHP.

соответствующие настройки в /etc/php/7.2/fpm/pool.d/www.conf:

listen = /run/php/php7.2-fpm.sock
pm = dynamic
pm.max_children = 150
pm.start_servers = 4
pm.min_spare_servers = 1
pm.max_spare_servers = 8
pm.max_requests = 250

/etc/nginx/nginx.conf:

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
    worker_connections 2048;
    multi_accept off;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    types_hash_max_size 2048;
    # server_tokens off;
    client_body_buffer_size 10K;
    client_header_buffer_size 1k;
    client_max_body_size 100m;
    large_client_header_buffers 4 8k;
    fastcgi_buffers 16 16k;
    fastcgi_buffer_size 32k;
    client_body_timeout 12;
    client_header_timeout 12;
    keepalive_timeout 15;
    send_timeout 10;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;
   # add_header Strict-Transport-Security "max-age=15768000; includeSubDomains" always;

    ##
    # Logging Settings
    ##

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

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_comp_level  2;
    gzip_min_length  1000;
    gzip_proxied     expired no-cache no-store private auth;
    gzip_types       text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

/ etc / nginx / sites-available / [SITENAME]:

server {
    listen 443 ssl http2; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/[SITENAME]/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/[SITENAME]/privkey.pem; # managed by Certbot
    #include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

    server_name [SITENAME];
    root /home/[SITENAME]/domains/[SITENAME]/public_html/web;
    index index.php index.html index.htm index.nginx-debian.html;

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

    location ~ \.php$ {
          include snippets/fastcgi-php.conf;
          fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    }

}
server {
    listen 443 ssl; # managed by Certbot
    server_name [SITENAME];
    return 301 https://[SITENAME]$request_uri;
    ssl_certificate /etc/letsencrypt/live/[SITENAME]/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/[SITENAME]/privkey.pem; # managed by Certbot
}

server {
    if ($host = [SITENAME]) {
    return 301 https://$host$request_uri;
} # managed by Certbot


listen 80;
    server_name [SITENAME];
    return 404; # managed by Certbot
}

server {
    listen 80;
    if ($host = [SITENAME]) {
    return 301 https://$host$request_uri;
} # managed by Certbot
    server_name [SITENAME];
    return 404; # managed by Certbot
}