Speed UP WordPress site with Nginx cache

Posted on
Speed ​​up WordPress site with Nginx cache
Speed ​​up WordPress site with Nginx cache

Think left and right, the last word solution is to use Nginx cache, the first article can refer to: Nginx configuration fastcgi cache. The advantage of fastcgi_cache is that the majority of users ’requests don’t get to affect back-end php-fpm, and send cached static pages directly, throwing various WordPress plugins at a speed of flash! In contrast, various plugins of WordPress still got to execute PHP, and it’s inevitable to access the database, which is weak!

Since the nginx cache is used, the web site runs smoothly, and there has never been a downtime. At an equivalent time, the CPU and memory usage of VPS have plummeted, and there’s no got to worry about the configuration of VPS anymore. I feel that ten times more traffic blogs can hold up!

Because nginx is as stable as a dog experience, now products that read more and write less for blogs are pushing nginx cache (fastcgi cache or proxy cache). insight of the likelihood of helping some netizens, the configuration file is posted for netizens’ reference (including SSL settings and gzip part):/etc/nginx/nginx.conf

# /etc/nginx/nginx.conf
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

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

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for" "$request_time"';

    access_log  /var/log/nginx/access.log  main buffer=32k flush=30s;

    server_tokens       off;
    client_max_body_size 100m;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

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

    # ssl
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384;
    ssl_ecdh_curve secp384r1;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    ssl_session_tickets off;
    ssl_stapling on; # Requires nginx >= 1.3.7
    ssl_stapling_verify on; # Requires nginx => 1.3.7
    add_header Strict-Transport-Security "max-age=63072000; preload";
    #add_header X-Frame-Options DENY;
    add_header X-Frame-Options SAMEORIGIN;
    add_header X-Content-Type-Options nosniff;
    add_header X-XSS-Protection "1; mode=block";

    # Fast CGI
    fastcgi_cache_path /var/cache/nginx/content.id levels=1:2 keys_zone=content.id:10m inactive=30m use_temp_path=off; 
    fastcgi_cache_key $request_method$scheme$host$request_uri;
    # note: can also use HTTP headers to form the cache key, e.g.
    #fastcgi_cache_key $scheme$request_method$host$request_uri$http_x_custom_header;
    #fastcgi_cache_lock on;
    fastcgi_cache_use_stale error timeout invalid_header updating http_500;
    fastcgi_cache_valid 200 301 302 10h;
    fastcgi_cache_valid 404 10m;
    fastcgi_ignore_headers Expires Set-Cookie Vary;

    # gzip 
    gzip on;
    gzip_min_length  1k;
    gzip_buffers     4 16k;
    gzip_comp_level 7;
    gzip_types
        text/css
        text/plain
        text/javascript
        application/javascript
        application/json
        application/x-javascript
        application/xml
        application/xml+rss
        application/xhtml+xml
        application/x-font-ttf
        application/x-font-opentype
        application/vnd.ms-fontobject
        image/svg+xml
        image/x-icon
        application/rss+xml
        application/atom_xml
        image/jpeg
        image/gif
        image/png
        image/icon
        image/bmp
        image/jpg;
    gzip_vary on;

    # 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;
}

Configuration for the host /etc/nginx/site-available/content.id.conf

server {
    listen 80;
    listen [::]:80;
    server_name www.content.id content.id;
    rewrite ^(.*) https://$server_name$1 permanent;
}

server {
    listen       443 ssl http2;
    listen       [::]:443 ssl http2;
    server_name www.content.id content.id;
    charset utf-8;
    ssl_certificate /etc/nginx/ssl/content.id.pem;
    ssl_certificate_key /etc/ssl/conf.d/content.id.key;

    set $host_path "/var/www/content.id";
    access_log  /var/log/nginx/content.id.access.log  main buffer=32k flush=30s;
    error_log /var/log/nginx/content.id.error.log;

    root   $host_path;

    set $skip_cache 0;
    if ($query_string != "") {
        set $skip_cache 1;
    }
    if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|sitemap(_index)?.xml") {
        set $skip_cache 1;
    }
    
    if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
        set $skip_cache 1;
    }

    location = / {
        index  index.php index.html;
        try_files /index.php?$args /index.php?$args;
    }

    location / {
        index  index.php index.html;
        try_files $uri $uri/ /index.php?$args;
    }
    location ~ ^/\.user\.ini {
            deny all;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_index index.php;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_cache content.id;
        fastcgi_cache_valid 200 301 302 30m;
        fastcgi_cache_valid 404 10m;
        fastcgi_cache_bypass $skip_cache;
        fastcgi_no_cache $skip_cache;
        fastcgi_cache_lock on;
        include fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    }
    location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar|jpeg)$ {
        expires max;
        access_log off;
        try_files $uri =404;
    }
}