This post is part 2 of 3 of this series and it will show you how we improved this site performance by switching to Nginx. Part One of this series can be found here.

In part one we told you why we switched to Nignx from Apache and how by doing so kept our web server up daily and sped things up a bit. We also made it clear that Apache is great and that majority of the websites online are using it. But for some reasons, we couldn’t get all the benefits it offers on this site, and that’s why we switched.

This post is going to show how we configured Nginx and PHP-FPM to power our sites. All the settings here may not work for you so you may have to change things a bit. If you follow these steps to the letter, you may be able to  speed up your blog as well.

So, part one showed you how to install Nginx, PHP, MySQL in Ubuntu. If your OS is different, you may have to change the commands a bit. For CentOS or Suse, check your OS documentation.

After installing Nginx, PHP and MySQL, we decided to configure Nginx for speed. Again, these settings may not work for your site, but they are the best I found around the web.

The first thing we did was to configure Nginx. Now, after installing Nginx in most Linux distributions, it gets installed in the /etc/ directory. In Ubuntu, all files and folders are in /etc/nginx directory.

Nginx’s main configuration file is nginx.conf. In this file is where you set global configurations. Ours is configured as followed:

At the very top of the file, we have these lines. Because our server has 4 CPU cores, we have 4 worker_processes.


user  nginx;
worker_processes  4;
worker_rlimit_nofile 50000;
error_log  /var/log/nginx/error.log;
pid        /var/run/;


For Nginx events block, we have these lines. You can specify you own values here but these work for our site.


events {
    worker_connections  4096;
    use epoll;
    multi_accept on;


For the http block, here’s what we have it configured

http {
    include      /etc/nginx/mime.types;
    default_type  application/octet-stream;
    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    open_file_cache max=1000 inactive=2h;
    open_file_cache_valid 1h;
    open_file_cache_min_uses 1;
    open_file_cache_errors on;

    client_header_timeout  60;
    client_body_timeout    60;
    send_timeout           60;
    client_max_body_size   15m;
    client_body_buffer_size  1K;
    client_header_buffer_size 1k;
    large_client_header_buffers 4 8k;
    types_hash_max_size 2048;
    reset_timedout_connection on;
    sendfile       on;
    tcp_nopush     on;
    tcp_nodelay    on;
    keepalive_timeout 15;
    server_tokens   off;

    gzip  on;
    gzip_static on;
    gzip_comp_level 6;
    gzip_disable "msie6";
    gzip_vary on;
    gzip_proxied any;
    gzip_min_length 512;
    gzip_http_version 1.1;
    gzip_types text/css text/javascript text/xml text/plain application/javascript application/x-javascript application/json application/xml application/rss+xml;

    fastcgi_cache_path /var/cache/nginx/fastcgi_temp levels=1:2 keys_zone=CZONE:15m inactive=60m;
    fastcgi_cache_key "$scheme$request_method$host$request_uri";
    fastcgi_cache_use_stale error timeout invalid_header http_500;

    include /etc/nginx/conf.d/*.conf;


That’s how our main Nginx configuration file set setup. You can add/remove stuff you feel isn’t necessary but these settings work for our site.

The next file is the per site configuration file. This file contains settings for each site or virtual sites. Below is how ours is setup for this site. In most Linux OS, the default file is at this location; /etc/nginx/conf.d/default.conf. In Ubuntu, it’s in /etc/nginx/sites-available/default.

For our site, here’s how our site configuration file is configured.

At the very top, we have this server block which provides 301 redirect to the site. Anyone types or or even, he/she will be redirected to That’s what this block does.


server {
        server_name _;
        return 301 $scheme://$request_uri;


The main server block inside the default site file is configured as shown below. Again, you can change some settings to match your environment but the one below works for our site. In the server block, we have 301 redirect for our feed. Out site feed URL is redirected to feedburner.


server {
    listen       80;
    root /var/www/liberiangeek;
    index index.php;
    try_files $uri $uri/ /index.php?$args;

    client_max_body_size 15m;
    client_body_buffer_size 128k;


#FeedBurner 301 redirect


    if ($http_user_agent !~ FeedBurner) {
    rewrite ^/feed/ last;

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

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

     location ~* \.(js|css|rss|png|jpg|jpeg|gif|ico|woff)$ {
                expires max;
                log_not_found off;
                access_log    off;
                add_header Pragma public;
                add_header Cache-Control "public, must-revalidate, proxy-revalidate";


# Do not cache content for logged-in users


    set $no_cache 0;

    if ($request_method = POST) {
        set $no_cache 1;

    if ($query_string != "") {
        set $no_cache 1;

    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;

   if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
        set $no_cache 1;

    location ~ \.php$ {
        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;
        include fastcgi_params;
        fastcgi_intercept_errors on;
        fastcgi_ignore_client_abort off;
        fastcgi_connect_timeout 60;
        fastcgi_send_timeout 90;
        fastcgi_read_timeout 90;
        fastcgi_buffer_size 128k;
        fastcgi_buffers 4 256k;
        fastcgi_busy_buffers_size 256k;
        fastcgi_temp_file_write_size 256k;
        fastcgi_cache_bypass $no_cache;
        fastcgi_no_cache $no_cache;
        fastcgi_cache   CZONE;
        fastcgi_cache_valid   200 302  1h;
        fastcgi_cache_valid   301 1h;
        fastcgi_cache_valid   any 1h;
        fastcgi_cache_min_uses  1;

    location ~* /\.ht {
        deny            all;
        access_log      off;
        log_not_found   off;


The formatting might not show up correctly on your screen but copy and paste the entire code in the default file, then save it.

Finally, we configure PHP-FPM module. Nginx uses this PHP-FPM module to cache its content. If you want to speed up your website, then you might want to enable it. In most Linux OS, it is installed in /etc/php-fpm.d/ directory. In Ubuntu, the configuration file is at /etc/php5/fpm/pool.d/www.conf.

The configuration file might be named www-conf or www.conf.

In that file, we’ll want to add or uncomment the line below.

listen = /var/run/php-fpm/php-fpm.sock


And comment out other listen parameters in the file. When you’re done, save the file.

That’s it for Nginx and PHP-FPM.

In the last post, we’ll download WordPress and move its content to our Nginx root directory. Until then, please come back.