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

前端之家收集整理的这篇文章主要介绍了优化Nginx PHP-FPM,每日浏览量达500万次前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

我们有3台Nginx服务器在负载均衡器后面运行.我们的数据库在群集上是独立的.我包括了我们的NginxPHP-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以继续使用相同的进程

猜你在找的Nginx相关文章