WordPress – How to reduce response time using Nginx

Spread the love

One of the big WordPress problem – long response time (over 1000 milliseconds). Optimal response time should be no more 500 ms. One of the effective way for it – enable cache on web server level.

Add to nginx.conf file next: 
fastcgi_cache_path /tmp/fcgi-cache levels=1:2 keys_zone=fcgicache:64m inactive=120m; # Cache path, size and inactive time
fastcgi_cache_key "$scheme$request_method$host$request_uri"; # Key for cache value (see nginx variables)

 
And add to location (where defined php):
fastcgi_cache fcgicache; # Path to cache
fastcgi_cache_valid 200 60m; # Caching responses only with HTTP code 200 on 1 hour
fastcgi_cache_methods GET HEAD; # Caching only GET and HEAD requests

Sample of configuration for website:

fastcgi_cache_path /tmp/fcgi-cache levels=1:2 keys_zone=fcgicache:64m inactive=120m; # Cache path, size and inactive time
fastcgi_cache_key "$scheme$request_method$host$request_uri"; # Key for cache value (see nginx variables)
 
server{
listen 80;
 
server_name default;
 
index index.php index.html index.htm;
root /var/www/html;
 
access_log /var/log/nginx/example.log;
error_log /var/log/nginx/example.log;
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_min_length 256;
gzip_types application/javascript text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/vnd.ms-fontobject;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
 
location ~ \.php$ {
try_files $uri =404;
fastcgi_cache fcgicache; # Path to cache
fastcgi_cache_valid 200 60m; # Caching responses only with HTTP code 200 on 1 hour
fastcgi_cache_methods GET HEAD; # Caching only GET and HEAD requests
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php7:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

Leave a Reply