Twitter this

Powered by MariaDB Powered by nginx ...

WordPress permalinks with NGINX

chains

I had a little issue with WordPress once I changed the permalinks structure, suddenly all pages stopped working except index.php one.   After some initial checking and a pretty good idea beforehand that using Nginx could be the reason I started to Google for some more info.  It got me to a page written in Russian.

So what is the problem?  Well, basically, mod_rewrite, -an apache thingy- isn't there for nginx in so we have to solve that in another way.  It sure looks like nginx doesn't even need a mod like that due to it's configurable nature that sort of functionality is covered out of the box. (But you do need to know how to go about unleashing it.)

UPDATE: At the time of writing this I didn't discover the nginx pitfalls page yet. It seems I was  a sinner back then.  right now I try to build configs that take into account all the known pitfalls.  The config here should work however at the time I wrote this post.  I cleaned it up now.  So please be aware of this so I don't spread the error.

The author , Vladimir Kolesnikov wrote a wordpress plugin to fix this.

After install and activation, it didn't appear to be working, time to check his blog.  I don't read Russian but Google pretends to do so. In fact, it translated pretty well (=relatively decipherable) using Google Translate, except that it translates the nginx config and code blocks on a page as well... It kinda overdoes it :)
But I found enough information to merge his suggestions on the nginx part into my own nginx conf to enable me to work with permalinks.

More info can be found here http://blog.sjinks.pro/wordpress-plugins/nginx-compatibility/ on his blog.

In the end, this is what we used for the nginx.conf part

What it does it redirects all those permlinks to the index.php file which knows how to find the right way to direct this url to what you are trying to get to. (which is in itself worth a blog article or two with a few rants on how that parsing is handled inside WordPress...).

First part

Download
# The real target
   server {
      listen       192.168.1.1:80;		# your server's public IP address
      server_name  my-domain.be;		# your domain name
      root         /usr/share/wordpress/;	# absolute path to your WordPress installation
 
      index index.php;
 
      try_files $uri $uri/ @wordpress;
 

Last part

Download
 
      location / {
         gzip_http_version 1.0;
         gzip_vary on;
         gzip_comp_level 3;
         gzip_proxied any;
         gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
         gzip_buffers 16 8k;
         log_not_found off;
         error_page 404 = @wordpress;
 
# enable search for precompressed files ending in .gz
# nginx needs to be complied using –-with-http_gzip_static_module
# for this to work, comment out if using nginx from aptitude
         gzip_static on;
 
# A page google cached a bit too quckly after my initial installation(!).  This
# gives a 302 Page has moved return code so you don't loose page ranking next time
         rewrite ^/(.*)/hello-world/ http://my-domain.be/$1/running-wordpress-nginx/ permanent;
      }
 
# Some themes cause the clients to request an index of a certain dir
# you can see this with firebug and in your nxginx error log file
# The location below will fix that for the raindrop theme im using now
 
      location /wp-content/themes/raindrops/images/ {
         autoindex on;
      }
 
      location @wordpress {
         fastcgi_pass   localhost:53217;
         fastcgi_param SCRIPT_FILENAME /usr/share/wordpress/index.php;
         include fastcgi_params;
         fastcgi_param SCRIPT_NAME /index.php;
      }     
 
      location ~ \.php$ {
         if (!-e $request_filename) {
            rewrite ^(.+)$ /index.php break;
            break;
         }
 
         fastcgi_index index.php;
         fastcgi_pass   localhost:53217;
         fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
         include        fastcgi_params;
      }
   }
 

Please let me know if this helped you(or not), leave a comment. Note that my full config is available, there are lots of rewrite lines from a cache plugin in there which you don't need. I will cover Nginx friendly cache in a next article soon.

4 comments to WordPress permalinks with NGINX

recruitment
recruitment
recruitment
recruitment