.

Set Up Unmanaged VPS (4 Newbies) – Part 14: Tweak Nginx for WordPress – Pretty URLs & WP Super Cache

English   English (change)

Set Up Unmanaged VPS (4 Newbies) – Part 14: Tweak Nginx for WordPress – Pretty URLs & WP Super Cache

nginx wordpress logos image

For the blog platform WordPress to work properly with Nginx we need to tweak the web server configuration. This tutorial will enable both caching – integrating the WP Super Cache plugin – and activate pretty permalinks (FURLs, CURLs, all that!) with some simple rewrite rules.

OK, so you’re a WordPress blogger. If you’re not, move swiftly on. Otherwise, and you want Nginx-juice, keep it here.

Because seeing as I’m all loved up on WordPress, and apparently so is the world and her sister, and because the almighty WP needs a little TLC to live happily with the Nginx server, I’ll spell out what’s required.

[sniplet guvSellBox]
[sniplet vpsIndexSell]

Why bother with the WP Super Cache?

‘Cos it handles heavy traffic and speeds up your site, buster! For more on that, see here. Essentially:-

  • to Digg-proof your blog

As for permalinks?

To convert URI’s like www.xyz.com/r/default.asp?iID=HFJJLH&iPostID=JMMGD to www.xyz.com/pretty-please. Essentially:-

  • for usability
  • for SEO

Lovely.

WordPress Plugin WP Super Cache & Permalinks Bundle

If you want ONLY the permalinks, not the caching, skip to here.

Create a new file:-

[text]sudo nano /usr/local/nginx/conf/wordpress_params.super_cache[/text]

And in there, paste:-

[text]# if the requested file exists, return it immediately
if (-f $request_filename) {
break;
}

set $supercache_file ”;
set $supercache_uri $request_uri;

if ($request_method = POST) {
set $supercache_uri ”;
}

# Using pretty permalinks, so bypass the cache for any query string
if ($query_string) {
set $supercache_uri ”;
}

if ($http_cookie ~* “comment_author_|wordpress|wp-postpass_” ) {
set $supercache_uri ”;
}

# if we haven’t bypassed the cache, specify our supercache file
if ($supercache_uri ~ ^(.+)$) {
set $supercache_file /wp-content/cache/supercache/$http_host/$1index.html;
}

# only rewrite to the supercache file if it actually exists
if (-f $document_root$supercache_file) {
rewrite ^(.*)$ $supercache_file break;
}

# all other requests go to WordPress
if (!-e $request_filename) {
rewrite . /index.php last;
}
[/text]

Save and quit – CTRL-X and Enter.

WordPress Permalinks Without WP Super Cache

If you’ve added the file above, ignore this section.

Create a file:

[text]sudo nano /usr/local/nginx/conf/wordpress_params.regular[/text]

And paste this within:-

[text]
# WordPress pretty URLs: (as per dominiek.com)
if (-f $request_filename) {
break;
}
if (-d $request_filename) {
break;
}
rewrite ^(.+)$ /index.php?q=$1 last;
# Enable nice permalinks for WordPress: (as per Yawn.it)
error_page 404 = //index.php?q=$uri;
[/text]

Save and quit (CTRL-X, ‘y’ to confirm, then Enter).

Configure Nginx for WordPress Permalinks & WP Super Cache

We’ll link to either of those files now, from the virtual host file we created in Part 13: Serve Multiple Sites & Blogs with Virtual Hosts. If you have more than one standalone blog, as per Part 13, you’ll have a vhost file for each, and must edit each of them.

Open the vhost file for each WP blog. For instance, for my site waywiderweb, which I’ve been exampling in this series:-

[text]sudo nano /usr/local/nginx/sites-available/waywiderweb.com[/text]

If you want BOTH the WP Super Cache and the friendly URLs

After the line index index.php index.html;, so before the }, paste this:-

[text]
# WordPress WP Super Cache plugin and permalinks.
include /usr/local/nginx/conf/wordpress_params.super_cache;
[/text]

If you want ONLY the friendly URLs

After the line index index.php index.html;, so before the }, paste this:-

[text]
# WordPress permalinks.
include /usr/local/nginx/conf/wordpress_params.regular;
[/text]

Restart Nginx:-

[text]n2r[/text]

Or, if you didn’t follow Part 7: Edit bashrc for User-Friendly Linux, type:-

[text]sudo /etc/init.d/nginx restart[/text]

Done. Check your site in a web browser.

I’d like to thank Matt Stevens and Gleb Esman for the Nginx integration.

Next Up, Installing WordPress & Other Platforms with Subversion.

That’s it for pretty URIs, friendly URLs, FURLs, CURLs, permalinks or whatever else they’re called. And we’ll enable the caching when we install the WP Super Cache plugin. Talking of which, in the next how-to guide in this series, we do just that, using super-duper Subversion, with which we’ll also simplify the WordPress installation process.

.. Damn, I’m good to you ;)   Guess what …

[sniplet vpsIndex]

.