协助为大型Wordpress网站优化NGINX配置文件

时间:2017-08-02 01:08:58

标签: php wordpress amazon-web-services nginx

服务器正在运行带有PHP-FPM(PHP7)的NGINX和最新版本的WordPress。我正在使用NGINX缓存以及OPCache和W3 Total Cache我正在使用数据库和对象缓存到Memcached集群以及CDN。

一切都在AWS上。它实际上是两个负载均衡的Web服务器,但我没有得到我想要的性能。任何优化nginx.conf文件的帮助都会很棒。如果你需要知道我正在使用的实例有2vCPU和8GB的RAM以及高网络速度。服务器正在共享站点文件的NFS卷。另外,我有一个处理数据库的RDS实例。

    user  username;
    worker_processes 2 ;

    pid        /var/run/nginx.pid;

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

    worker_rlimit_nofile 4096;

    http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 60;
        types_hash_max_size 2018;
        client_max_body_size 124m;
        client_body_buffer_size 1m; 
        client_body_timeout 47; 
        client_header_timeout 47;


        # server_tokens off;

        server_names_hash_bucket_size 128;
        # server_name_in_redirect off;

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

        open_file_cache max=2000 inactive=20s; 
        open_file_cache_valid 60s; 
        open_file_cache_min_uses 5; 
        open_file_cache_errors off;

        fastcgi_buffers 256 16k; 
        fastcgi_buffer_size 128k; 
        fastcgi_connect_timeout 3s; 
        fastcgi_send_timeout 120s; 
        fastcgi_read_timeout 120s; 
        fastcgi_busy_buffers_size 256k; 
        fastcgi_temp_file_write_size 256k; 
        reset_timedout_connection on; 

        ##
        # SSL Settings       
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        # Gzip Settings
        gzip on;
        gzip_disable "msie6";

        gzip_vary on;
        gzip_proxied any;
        gzip_comp_level 2;
        gzip_buffers 16 8k;
        gzip_http_version 1.1;
        gzip_types text/css text/x-component application/x-javascript application/javascript text/javascript text/x-js text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/bmp application/java application/msword application/vnd.ms-fontobject application/x-msdownload image/x-icon image/webp application/json application/vnd.ms-access application/vnd.ms-project application/x-font-otf application/vnd.ms-opentype application/vnd.oasis.opendocument.database application/vnd.oasis.opendocument.chart application/vnd.oasis.opendocument.formula application/vnd.oasis.opendocument.graphics application/vnd.oasis.opendocument.spreadsheet application/vnd.oasis.opendocument.text audio/ogg application/pdf application/vnd.ms-powerpoint application/x-shockwave-flash image/tiff application/x-font-ttf audio/wav application/vnd.ms-write application/font-woff application/font-woff2 application/vnd.ms-excel; 

    # Nginx Fast Cache Settings
    fastcgi_cache_path /dev/shm/nginx-cache levels=1:2 keys_zone=WORDPRESS:1024m inactive=60m;
    fastcgi_cache_key "$scheme$request_method$host$request_uri";
    fastcgi_cache_use_stale error timeout invalid_header http_500;
    fastcgi_ignore_headers Cache-Control Expires Set-Cookie;

    # HTTP Server
        server {
            listen 80 default_server;
        listen 443 ssl;
            #listen [::]:80 default_server;
        server_name www.siteurl.com;

        ssl_certificate /efs/ssl/name.crt;
        ssl_certificate_key /efs/ssl/name.key;

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

            root /efs/public_html1/;

        include /efs/name/nginx.conf;

        index index.php;

            location = /favicon.ico {
            log_not_found off; 
            access_log off; 
        }

            location = /robots.txt { 
            log_not_found off; 
            access_log off; 
            allow all; 
        }

        #Set static file browser caching
            location ~* \.(css|js)$ {
                    expires 30d;
                    log_not_found off;
                    access_log off; 
        }

            location ~* \.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
                    expires 60d;
                    log_not_found off;
                    access_log off;
            }

        #Prevent access to hidden files
        location ~ /\. {
            access_log off;
            log_not_found off; 
            deny all;
        }

        #Cache Settings
        set $skip_cache 0;

        # POST requests and urls with a query string should always go to PHP
        if ($request_method = POST) {
            set $skip_cache 1;
        }   
        if ($query_string != "") {
            set $skip_cache 1;
        }   

        # Don't cache uris containing the following segments
        if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
            set $skip_cache 1;
        }   

        # Don't use the cache for logged in users or recent commenters
        if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
            set $skip_cache 1;
        }

            location / {
                    try_files $uri $uri/ /index.php$is_args$args;

        }

            location ~ \.php$ {
                    try_files $uri =404;
                    fastcgi_split_path_info ^(.+\.php)(/.+)$;
                    fastcgi_pass unix:/var/run/php-fpm-7.0.sock;
                    fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param SCRIPT_NAME $fastcgi_script_name;
                    include fastcgi_params;

            fastcgi_cache_bypass $skip_cache;
                fastcgi_no_cache $skip_cache;

            fastcgi_cache WORDPRESS;
            fastcgi_cache_valid 60m;
            }

        location ~ /purge(/.*) {
            fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1";
        }

        add_header X-Cache-Status $upstream_cache_status;   
    }

    }

0 个答案:

没有答案