Best Ways to Optimize WordPress Performance with NGINX

Speed and advancement is the most testing piece of the consistently developing Computer age. Performance is legitimately relative to the client experience. You yourself will turn off the site in the event that it is requiring plenty of time to load. Nginx is one of the broadly utilized web servers and it is an option of Apache 2. It is prevalent for dealing with substantial traffic and for its steadiness. It is very easy to understand and simple to configure.

Nearly all website admins call for their sites and blogs to be the quickest. Satisfying clients when they visit your sites; should be your prime concern as a web-master .

WordPress’ request is its plainness, both for end-users and for usage. Be that as it may, the design of a WordPress site presents issues when utilization inclines upward a few stages. Including caching and joining WordPress and NGINX can tackle these issues.

Apache vs. NGINX — Maximum Opportunity?

The two best known open-source HTTP servers on the planet are struggling about a clear chunk of information available. Both of them can provide a full web stack. Nonetheless, NGINX says it supports dynamic content better – tempting VPS and dedicated hosting users, and sharing Apache speed and perfection remains an advantage for hosting customers.

It tends to be set up as an invert-proxy before Apache, which is a ground-breaking arrangement. That enables you to utilize the majority of the features and intensity of Apache while profiting by the speed of Nginx.

There are a couple of leading contrasts influencing WordPress usage that you should know about before continuing. In this post, I’ll give some tips to help beat average WordPress performance challenges:

1 – Cache Static Resources

NGINX is an amazing engine for static content conveyance (ex. pictures, JavaScript, CSS, and so forth.). It proposes a straightforward component for caching on a customer’s site permitting to decrease server load and increment content conveyance speed.

Below an example of server configuration is shown.

server {
    # substitute your web server's URL for www.example.com
    server_name www.example.com;
    root /var/www/example.com/htdocs;
    index index.php;

    access_log /var/log/nginx/example.com.access.log;
    error_log /var/log/nginx/example.com.error.log;

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

    location ~ .php$ {
        try_files $uri =404;
        include fastcgi_params;
        # substitute the socket, or address and port, of your WordPress server
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        #fastcgi_pass 127.0.0.1:9000;
    }   

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

A permalink is only the web address used to connect to your blog content. The URL to each blog entry or cms post ought to be lasting and never show signs of change. WordPress has the ability to make a custom URL structure for your blog entries and chronicles. There are three fundamental sorts of WordPress permalinks:

Ugly: https://athemeart.com/faq/?p=2284
Pretty using mod_rewrite: https://athemeart.com/faq/bash-for-loop/
Almost pretty using PHP PATHINFO: https://athemeart.com/faq/index.php/bash-for-loop/

Numerous WordPress sites rely upon .htaccess documents, NGINX doesn’t assist .htaccess documents. Luckily, you can empower permalinks in WordPress with NGINX by including the accompanying area hinder in your fundamental server.

location / {
        # try_files $uri $uri/ =404;
        try_files $uri $uri/ /index.php?q=$uri&$args; 
        #ignored: "-" thing used or unknown variable in regex/rew 
        if (!-f $request_filename){
        set $rule_1 1$rule_1;
        }
        if (!-d $request_filename){
        set $rule_1 2$rule_1;
        }
        if ($rule_1 = "21"){
        rewrite /. /index.php last;
        }   
    }

if you wish incloude site map then check bellow Code Snipps

location / {
        # try_files $uri $uri/ =404;
        try_files $uri $uri/ /index.php?q=$uri&$args; 
        
        #ignored: "-" thing used or unknown variable in regex/rew 
        if (!-f $request_filename){
        set $rule_1 1$rule_1;
        }
        if (!-d $request_filename){
        set $rule_1 2$rule_1;
        }
        if ($rule_1 = "21"){
        rewrite /. /index.php last;
        }   
        
        rewrite ^/sitemap(-+([a-zA-Z0-9_-]+))?\.xml$ "/index.php?xml_sitemap=params=$2" last;
        rewrite ^/sitemap(-+([a-zA-Z0-9_-]+))?\.xml\.gz$ "/index.php?xml_sitemap=params=$2;zip=true" last;
        rewrite ^/sitemap(-+([a-zA-Z0-9_-]+))?\.html$ "/index.php?xml_sitemap=params=$2;html=true" last;
        rewrite ^/sitemap(-+([a-zA-Z0-9_-]+))?\.html.gz$ "/index.php?xml_sitemap=params=$2;html=true;zip=true" last;
    
    }

3 – Configure NGINX for PHP-FPM (FastCGI Process Manager)

Nginx will perform caching on its own finish to scale back load on your server. once you need to use Nginx’s inbuilt fastcgi_cache, you higher compile nginx with fastcgi_cache_purge module. it’ll facilitate nginx purge cache for a page once it gets emended. On the WordPress aspect, you would like to put in a plugin like Nginx Helper to utilize fastcgi_cache_purge feature.

fastcgi_cache_path /var/run/nginx-cache levels=1:2
keys_zone=WORDPRESS:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";

server {
server_name example.com www.example.com;
root /var/www/example.com/htdocs;
index index.php;

access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;

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?$args;
}

location ~ .php$ {
try_files $uri /index.php;
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
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";
}

location ~* ^.+.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg|jpeg
|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi
|wav|bmp|rtf)$ {

access_log off;
log_not_found off;
expires max;
}

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

location ~ /. {
deny all;
access_log off;
log_not_found off;
}
#fastcgi_cache start
set $no_cache 0;

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

# Don't cache uris containing the following segments
if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") {
set $no_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 $no_cache 1;
}
}

4 – Configure NGINX for W3 Total Cache

W3 Total Cache (by Frederick Townes of W3-Edge), is a WordPress reserving system or caching framework that assists NGINX. It’s an option to FastCGI reserve (cache) and has a wide scope of alternative settings. The reserving plug-in displays an assortment of caching configurations and furthermore incorporates choices for database and object caching, minification of HTML, CSS, and JavaScript, just as alternatives to coordinate with mainstream CDNs.

The plugin handles NGINX configuration by keeping in touch with an NGINX setup document situated in the root catalog of your domain

server {
    server_name example.com www.example.com;

    root /var/www/example.com/htdocs;
    index index.php;
    access_log /var/log/nginx/example.com.access.log;
    error_log  /var/log/nginx/example.com.error.log;

    include /path/to/wordpress/installation/nginx.conf;

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

    location ~ .php$ {
        try_files $uri =404;
        include fastcgi_params;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
    }
}

5- Configure NGINX for WP Super Cache

NGINX configurations can switch depending on your preference. One possible setup structure is given below:

server {
    server_name example.com www.example.com;
    root /var/www/example.com/htdocs;
    index index.php;

    access_log /var/log/nginx/example.com.access.log;
    error_log  /var/log/nginx/example.com.error.log debug;

    set $cache_uri $request_uri;

    # POST requests and URLs with a query string should always go to PHP
    if ($request_method = POST) {
        set $cache_uri 'null cache';
    }  
    if ($query_string != "") {
        set $cache_uri 'null cache';
    }   

    # Don't cache URIs containing the following segments
    if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php
                          |wp-.*.php|/feed/|index.php|wp-comments-popup.php
                          |wp-links-opml.php|wp-locations.php |sitemap(_index)?.xml
                          |[a-z0-9_-]+-sitemap([0-9]+)?.xml)") {

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

    # Use cached or actual file if it exists, otherwise pass request to WordPress
    location / {
        try_files /wp-content/cache/supercache/$http_host/$cache_uri/index.html 
                  $uri $uri/ /index.php;
    }    

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

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

    location ~ .php$ {
        try_files $uri /index.php;
        include fastcgi_params;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        #fastcgi_pass 127.0.0.1:9000;
    }
    
    # Cache static files for as long as possible
    location ~*.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg
                 |jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid
                 |midi|wav|bmp|rtf)$ {
        expires max;
        log_not_found off;
        access_log off;
    }
}

6 – Security Precautions to NGINX Configuration

Computers are just as smart as individuals utilizing them. Nginx is worked to be steady and secure, yet it might be as secure as the client who configures it. Once Nginx is manufactured and installed, configuring the server to be as insignificant as conceivable is necessary.

To secure with assaults, you can monitoring access to secret weapons and farthest point the capacity of bots to over-burden the login utility.

Permit just specific IP delivers to get to the WordPress Dashboard.

# Restrict access to WordPress dashboard
location /wp-admin {
    deny  192.192.9.9;
    allow 192.192.1.0/24;
    allow 10.1.1.0/16;
    deny  all;
}

Only allow uploading of specific types of files. to prevent programs with malicious intent from being uploaded and running.

# Deny access to uploads that aren’t images, videos, music, etc.
location ~* ^/wp-content/uploads/.*.(html|htm|shtml|php|js|swf)$ {
    deny all;
}

Deny access to wp-config.php, the WordPress configuration file. Another way to deny access is to move the file one directory level above the domain root.

# Deny public access to wp-config.php
location ~* wp-config.php {
    deny all;
}

Rate limit access to wp-login.php to block against brute force attacks.

# Deny access to wp-login.php
location = /wp-login.php {
    limit_req zone=one burst=1 nodelay;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    #fastcgi_pass 127.0.0.1:9000;
}

Conclusion

Nginx is an extremely amazing web server and able to distribute sites of any size and traffic. In any case, it is constantly prescribed to change it to your needs with the goal that your sites ought to react in an auspicious way. The previously mentioned changes, whenever pursued, should make your web server competent to adapt to medium to high measure traffic sites.

Inspire us with your love!

FacebookTwitterReddit
Avatar

Art of Awesome Professional WordPress themes & Site templates. Our responsive themes are designed to work seamlessly for all desktop and mobile devices. Modern, Clean & Easy To Use.

You can check also