For those who don’t know, this blog runs on Nginx web server using WordPress content Management systems (CMS).

WordPress works with almost all webservers in used today. From IIS to Nginx and Apache2, WordPress runs OK on these servers.

For some webmasters, Nginx is their preferred choice, including me. I switched from Apache2 to Nginx and haven’t looked back.

I am not saying Apache2 isn’t good. I just love the simplicity of Nginx webserver.  Others may disagree with this statement.

The reason I’m writing this is to show how we configured Nginx settings to achieve great performance. My site isn’t the fastest, but considering I don’t use CDNs or other managed host providers, this is fast enough for only $40 per month.

To achieve great results using Nginx, I’d recommend using the latest version of Nginx web server.

To install the latest version of Nginx, please read this post.

After installing Nginx, you should also installed FastCGI module for Nginx. To install PHP modules including FastCGI in CentOS, run the line below.

yum install php php-cli php-common php-xmlrpc php-mysql php-gd php-fpm

In Ubuntu, read this post.. in it we showed you how to install Nginx, including PHP modules that include FastCGI.

After installing Nginx and FastCGI modules, depending on where your nginx configurations are, will be based on the Linux distribution you’re using.

Most likely it will be in /etc/nginx/nginx.conf

And the default site configuration would be in /etc/nginx/conf.d/default.conf

or

/etc/nginx/sites-available/default

In our main nginx.conf file, we have these settings.

##Security
server_tokens off;
##
## Timeouts
send_timeout 10s;
keepalive_timeout 10s;
client_body_timeout 60s;
client_header_timeout 60s;
##
## Size limits
client_max_body_size 15m;
client_header_buffer_size 2k;
client_body_buffer_size 1K;
large_client_header_buffers 4 8k;
types_hash_max_size 2048;
##
## File caching
open_file_cache max=1024 inactive=2h;
open_file_cache_valid 1h;
open_file_cache_min_uses 1;
open_file_cache_errors on;
##
## General
ignore_invalid_headers on;
reset_timedout_connection on;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
##
## Gzip Settings
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_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
##
## Nginx FastCGI Cache
fastcgi_cache_path /var/cache/nginx/cache levels=1:2 keys_zone=LGCACHE:15m inactive=60m;
fastcgi_cache_key “$scheme$request_method$host$request_uri”;
fastcgi_cache_use_stale error timeout invalid_header http_500;

The above settings works great for us.

Then our default site configuration page has the below settings.

server {
listen 80 default_server;
server_name www.liberiangeek.net liberiangeek.net;
root /var/www/public_html/html;
index index.html index.php;
##
## Robots
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
##
## Good for WordPress permalinks
location / {
try_files $uri $uri/ /index.php?$args;
}
##
##Feedburner Redirect
if ($http_user_agent !~ FeedBurner) {
rewrite ^/feed/ http://feeds.feedburner.com/liberiangeek last;
}
#Yoast sitemap
location ~ ([^/]*)sitemap(.*)\.x(m|s)l$ {
## this redirects sitemap.xml to /sitemap_index.xml
rewrite ^/sitemap\.xml$ /sitemap_index.xml permanent;
## this makes the XML sitemaps work
rewrite ^/([a-z]+)?-?sitemap\.xsl$ /index.php?xsl=$1 last;
rewrite ^/sitemap_index\.xml$ /index.php?sitemap=1 last;
rewrite ^/([^/]+?)-sitemap([0-9]+)?\.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;
}
##
## Browser Caching
location ~* \.(css|js|ico|gif|jpe?g|png|svg|eot|otf|woff|woff2|ttf|ogg)$ {
expires max;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
##
## PHP settings
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
fastcgi_connect_timeout 60;
fastcgi_ignore_client_abort off;
fastcgi_send_timeout 60;
fastcgi_read_timeout 60;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
include fastcgi_params;
fastcgi_cache LGCACHE;
fastcgi_cache_valid 200 302 1h;
fastcgi_cache_valid 301 1h;
fastcgi_cache_valid any 1h;
fastcgi_cache_min_uses 2;
}
}

The highlighted texts are configurations for Nginx caching. These values can be changed based on your environments. The configurations above are based on CentOS 7 system.

It may also work in Ubuntu, but verify first.

Finally, our PHP FastCGI configuration file has

; '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
;listen = 127.0.0.1:9000
listen = /var/run/php-fpm/php-fpm.sock

In CentOS 7, FastCGI configuration file is at /etc/php-fpm.d/www.conf

The above settings have worked great for us, especially with the latest version of Nginx.

If everything works and Nginx is able to start, then you’re in good state. If not, look around to see if there a mistakes in the configurations.

Enjoy!