Configuring Nginx To Run Wordpress

Published July 1st, 2011


This post is about configuring nginx to run wordpress. The actual setup of WordPress remains the same as with any other server so I will not cover that in this post. For instruction on how to setup WordPress I suggest reading the documentation regarding WordPress installation contained within the WordPress Codex at http://codex.wordpress.org/Installing_WordPress.

The main difference between running WordPress on a server such as Apache is how the rewrites work. below is a working configuration which runs WordPress flawlessly using nginx which I use. This nginx site configuration file should reside in your /etc/nginx/sites-available/ folder. Be sure to change the paths within this configuration to reflect your paths to WordPress and log files. Once this configuration file is in place and linked to your sites-enabled folder restart your nginx process and you should be good to go with full support for WordPress’es Permalinks.

server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;
 
    # If the site is accessed via yourdomain.com
    # automatically redirect to www.yourdomain.com.
    if ($host = yourdomain.com' ) {
        rewrite  ^/(.*)$  http://www.yourdomain.com/$1  permanent;
    }
 
    access_log  /path/to/your/logs/access_log;
    error_log /path/to/your/logs/error_log;
 
    root   /path/to/wordpress;
    index  index.php index.html index.htm;
 
    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }
 
    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }
 
    # Deny all attempts to access hidden files
    # such as .htaccess, .htpasswd, etc...
    location ~ /\. {
        deny all;
        access_log off;
        log_not_found off;
    }
 
    # Begin WordPress specific items.
    location / {
        try_files $uri $uri/ /index.php?$args;
    }
 
    rewrite /wp-admin/$ /wp-admin/index.php last;
    # End WordPress specific items.
 
    location ~ \.php$ {
        root   /path/to/wordpress;
 
        # Security against php processing files of
        # the same name but diffrent extentions.
        if (!-f $request_filename) {
            return 404;
        }
 
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include  fastcgi_params;
    }
}