优化Nginx PHP-FPM,每日浏览量达500万次

我们运行了一些高容量的网站,每天可以产生大约500万的综合浏览量.由于我们预计会有增长,因此我们拥有最多的过度服务器,但我们有一些活跃用户的报告称该网站有时在第一次网页浏览时速度较慢.我每隔一段时间就会看到这个,第一次网页浏览需要3-5秒,然后在一天的剩余时间之后立即进行.这种情况在过去的24小时内可能发生过两次,所以还不足以弄清楚发生了什么.我们网站上的每个页面都使用 PHP,但有一次它发生在我身上,它是在没有任何数据库调用的PHP页面上,这让我认为问题仅限于NGINX,PHP-FPM或网络设置.

我们有3台NGINX服务器在负载均衡器后面运行.我们的数据库在群集上是独立的.我包括了我们的nginx和php-fpm配置文件,以及我们当前的RAM使用情况和PHP-FPM状态.这基于当天中午(我们的平均流量).如果您在我的设置中看到任何危险信号或有任何进一步优化的建议,请查看并告诉我们.

每个NGINX服务器的规格:

OS: CentOS 7
RAM: 128GB
CPU: 32 cores (2.4Ghz each)
Drives: 2xSSD on RAID 1

RAM使用(免费-g)

total        used        free      shared  buff/cache   available
Mem:            125          15          10           3         100         103
Swap:            15           0          15

PHP-FPM状态(IE:http://server1_ip/status)

pool:                 www
process manager:      dynamic
start time:           03/Mar/2016:03:42:49 -0800
start since:          1171262
accepted conn:        69827961
listen queue:         0
max listen queue:     0
listen queue len:     0
idle processes:       1670
active processes:     1
total processes:      1671
max active processes: 440
max children reached: 0
slow requests:        0

php-fpm配置文件:

[www]
user = nginx
group = nginx
listen = /var/opt/remi/php70/run/php-fpm/php-fpm.sock
listen.owner = nginx
listen.group = nginx
listen.mode = 0660
listen.allowed_clients = 127.0.0.1
pm = dynamic
pm.max_children = 6000
pm.start_servers = 1600
pm.min_spare_servers = 1500
pm.max_spare_servers = 2000
pm.max_requests = 1000
pm.status_path = /status
slowlog = /var/opt/remi/php70/log/php-fpm/www-slow.log
php_admin_value[error_log] = /var/opt/remi/php70/log/php-fpm/www-error.log
php_admin_flag[log_errors] = on
php_value[session.save_handler] = files
php_value[session.save_path]    = /var/opt/remi/php70/lib/php/session
php_value[soap.wsdl_cache_dir]  = /var/opt/remi/php70/lib/php/wsdlcache

nginx配置文件:

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

events {
    worker_connections 1000;
    multi_accept        on;
    use                 epoll;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_iso8601] "$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   10 10;
    send_timeout    60;
    types_hash_max_size 2048;

    client_max_body_size 50M;
    client_body_buffer_size 5m;
    client_body_timeout 60;
    client_header_timeout 60;

    fastcgi_buffers 256 16k;
    fastcgi_buffer_size 128k;
    fastcgi_connect_timeout 60s;
    fastcgi_send_timeout 60s;
    fastcgi_read_timeout 60s;
    fastcgi_busy_buffers_size 256k;
    fastcgi_temp_file_write_size 256k;
    reset_timedout_connection on;
    server_names_hash_bucket_size 100;


    #compression
    gzip  on;
    gzip_vary on;
    gzip_min_length 10240;
    gzip_proxied expired no-cache no-store private auth;
    gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/javascript application/xml;
    gzip_disable "MSIE [1-6]\.";

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

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  domain1.com;
        root         /folderpath;

        location / {
            index index.php;
        }
        location = /favicon.ico { access_log off; log_not_found off; }
        location = /robots.txt  { access_log off; log_not_found off; }


    #server status
        location /server-status {
            stub_status on;
        access_log off;
            auth_basic "Restricted";
            auth_basic_user_file /etc/nginx/.htpasswd;
        }

    location = /status {
        access_log off;
        allow 127.0.0.1;
            auth_basic "Restricted";
            auth_basic_user_file /etc/nginx/.htpasswd;

            fastcgi_pass unix:/var/opt/remi/php70/run/php-fpm/php-fpm.sock;
            fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }


        location ~ \.php${
            try_files $uri =404;
            fastcgi_pass unix:/var/opt/remi/php70/run/php-fpm/php-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }

更新:

我根据下面的建议安装了opcache.不确定它是否解决了这个问题.这是我的设置

opcache.enable=1
opcache.memory_consumption=1024
opcache.interned_strings_buffer=64
opcache.max_accelerated_files=32531
opcache.max_wasted_percentage=10
2个小技巧:

>如果你使用opcache,监视它以检查它的配置(特别是内存大小)是否正常,并避免重置OOM,你可以使用https://github.com/rlerdorf/opcache-status(单个php页面)>增加pm.max_requests以继续使用相同的进程

相关文章
相关标签/搜索