Configuring Nginx for WordPress


A typical Vhost file (for example /etc/nginx/sites-available/default) would look like this:

server {
  listen   80;
  server_name  YOUR_SERVER_NAME;
  access_log  /var/log/nginx/access.log;

  root /var/www;
  index index.php;

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

  location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
                fastcgi_pass unix:/var/run/php-fpm-www.sock;
                fastcgi_index index.php;
                include fastcgi_params;
  }

  if (!-e $request_filename) {
        rewrite /wp-admin$ $scheme://$host$uri/ permanent;    
        rewrite ^(/[^/]+)?(/wp-.*) $2 last; 
        rewrite ^(/[^/]+)?(/.*\.php) $2 last; 
  }
  location ~ /\.ht {
               deny all;   } }

The 2 passages that are put in bold are needed for WordPress to run correctly – and to rewrite permalinks etc. to index.php.

If you would like to install Nginx with PHP-FPM, have a look here.