English (change)
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.
In 20 copy/paste steps .. from zero to hero, blank box to cute-as Linux server.
Scroll down for the full series index.
So, hey, shed shared & viva virtual! Hope it helps. the_guv
'Cos it handles heavy traffic and speeds up your site, buster! For more on that, see here. Essentially:-
To convert URI's like www.xyz.com/r/default.asp?iID=HFJJLH&iPostID=JMMGD to www.xyz.com/pretty-please. Essentially:-
Lovely.
If you want ONLY the permalinks, not the caching, skip to here.
Create a new file:-
sudo nano /usr/local/nginx/conf/wordpress_params.super_cache
And in there, paste:-
# 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;
}
Save and quit - CTRL-X and Enter.
If you've added the file above, ignore this section.
Create a file:
sudo nano /usr/local/nginx/conf/wordpress_params.regular
And paste this within:-
# 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;
Save and quit (CTRL-X, 'y' to confirm, then Enter).
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:-
sudo nano /usr/local/nginx/sites-available/waywiderweb.com
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:-
# Wordpress WP Super Cache plugin and permalinks. include /usr/local/nginx/conf/wordpress_params.super_cache;
If you want ONLY the friendly URLs
After the line index index.php index.html;, so before the }, paste this:-
# Wordpress permalinks. include /usr/local/nginx/conf/wordpress_params.regular;
Restart Nginx:-
n2r
Or, if you didn't follow Part 7: Edit bashrc for User-Friendly Linux, type:-
sudo /etc/init.d/nginx restart
Done. Check your site in a web browser.
I'd like to thank Matt Stevens and Gleb Esman for the Nginx integration.
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 ...
Serve multi sites & blogs on a budget .. at the fastest possible speed .. with the least downtime .. in the most secure environment .. and future-proofed for easy admin.
That's what the VPS Bible is about, stepped out in simple copy & paste guides.
From high traffic WordPress blogs to startup web hosts, here's what you need.
Set it up? Click here for the 21 part follow-up .. V-P-S Admin
» Setup bWordPress/b on NGINX (FURLs amp; Cache) - VPS Bible Pt 13 - GUVNR Wordpress UK: Wordpress UK, taking it further… June 2nd, 2009 at 8:08 pm
[...] Read the rest here: Setup bWordPress/b on NGINX (FURLs amp; Cache) - VPS Bible Pt 13 - GUVNR [...]
Setup WordPress on NGINX (FURLs & Cache) - VPS Bible Pt 13 - GUVNR | Bloggers Tools June 2nd, 2009 at 8:14 pm
[...] here to read the rest: Setup WordPress on NGINX (FURLs & Cache) - VPS Bible Pt 13 - GUVNR You May Like Linking To UsHello and welcome to our blog, you may consider linking to us, because [...]
kajdo June 2nd, 2009 at 11:22 pm
nice one again
the_guv June 3rd, 2009 at 1:53 am
cheers kajdo... pleased you like it, Sir.
Fartzilla June 5th, 2009 at 6:39 pm
You know, over the years I have tried many different tutorials for many things and have always spent countless hours trying to work out why something didn't work out only to find that it was because the author missed a "." here, or a "/" there, or some other minor mistake which meant that I did not get the same result as the author.
I am incredibly happy that I followed along your tutorials to date and there hasn't been a single error, everything has happened just the way you explained. Thanks heaps, you are doing a great job!
the_guv June 5th, 2009 at 8:08 pm
Wow! Fartzilla (God's teeth, what kind of a name is that
)
... I don't think I've before received such praise. You might have used a larger font!
I am honoured, thank you.
Darn kind, and I'm really rather chuffed it's working out so well for you.
eric June 6th, 2009 at 8:01 pm
hi nice vps bible, other than when i am trying to run the way you have instructed i keep getting this error
2009/06/06 18:00:04 [error] 30448#0: *4 rewrite or internal redirection cycle while processing "/index.php",
the_guv June 7th, 2009 at 8:06 pm
tx eric .. sorry to hear you've got a problem. surely a rewrite issue.
assuming you've done everything literally code for code, it will work. it's 100% tested.
there's a discrepancy between what I say and what you've done, most likely in either Part 13 or here.
...try again, retracing your steps carefully.
if that doesn't work, or you already have tried - let me know exactly what you want to achieve, if anything, with your rewrite ruleset and any even seemingly insignificant discrepancies (ie vps provider and linux distro) and maybe i or someone here can advise.
best of luck
Install/Upgrade WORDPRESS with SUBVERSION - VPS Bible #15 - GUVNR June 11th, 2009 at 12:56 pm
[...] Part 14: * Set up WordPress on Nginx with Pretty URLs & WP Super Cache [...]
custard July 14th, 2009 at 2:45 pm
Great stuff. thanks guv.
the_guv July 14th, 2009 at 3:59 pm
@custard .. big cheers.
Otto July 17th, 2009 at 5:47 pm
Simpler rule for pretty permalinks (no super-cache):
try_files $uri $uri/ /wordpress/index.php;
That's it. Easy. Works fine.
the_guv July 19th, 2009 at 11:17 am
@Otto .. nice tip, thank you. Will try that.
LnddMiles July 21st, 2009 at 6:48 pm
Pretty cool post. I just stumbled upon your blog and wanted to say
that I have really liked reading your blog posts. Anyway
I’ll be subscribing to your blog and I hope you post again soon!
the_guv July 25th, 2009 at 11:30 am
@LnddMiles .. you are clearly a wise and sensible person! thank you.
Elena August 4th, 2009 at 5:33 pm
thanks very helpful
trav 19 August 6th, 2009 at 7:08 pm
Good morning, guvnr,
I'm trying to install WordPress Mu but I'm a little apprehensive about using the conf we made along with the supercache referenced in it.
on the page below this link, there's a portion for the rewrite to be used in nginx servers:
http://codex.wordpress.org/Installing_WPMU
should i follow that? Do I delete everything in the one we made? Have you tried installing Mu with BuddyPress with the same conf + supercache?
I found this one but it doesn't have supercache
http://buddypress.org/forums/topic/nginx-re-write-rules
should i use that?
This one has supercache...
http://mu.wordpress.org/forums/topic/11705
Will that work? I'm not sure which one to delete, add, combine or what not. Thank you for your time, sir!
the_guv August 6th, 2009 at 9:00 pm
Howdy Trav 19,
I've set up MU + BP but haven't had a chance to play with it properly yet. Maybe too late for you, it'll be a few weeks before I crack out my findings.
That looks to be a top guide at WP. Their rewrite rule should just need to go in your vhost file, in the server block. Follow the other steps they set out too though, DNS and so on.
Try it, and let me know, I'm curious, and lots of other folks will be too.
Martonic August 26th, 2009 at 4:15 pm
Damn, that sound's so easy if you think about it.
nick September 19th, 2009 at 4:40 am
how do you get wordpress running with wp-ecommerce and able to add a product to your shopping cart on nginx?
the_guv September 23rd, 2009 at 10:12 am
@Martonic .. I must tighten up my spam filter
@Nick .. now come on, get it together! I mean, that's kind of like asking, what's the best way to cook up spuds?
tell me what you want to achieve, what factors to expect and then maybe i can give you some idea.
Alan aka Hyperdude October 16th, 2009 at 1:23 pm
@Nick.. I too have had a few problems with the wp-ecommerce plugin. I have feeling that it may be something to do with the permissions settings. Not sure if the authors of the plugin have ever tested with nginx? But I can't imagine that nginx is the cause of the problem.
the_guv October 16th, 2009 at 2:07 pm
ah, @Nick .. that's a plugin is it?! Probably best if I try to read a comment before replying, hmmn. Er, no idea about that though, sorry.
Jean October 25th, 2009 at 5:41 pm
Hi Guv, i've been following your tutorials with much thanks. I have a question as pretty permalinks don't work for my WP (It's always a 404 except if default is chosen in my WP permalinks settings) If my wp blog were at domain.com/blog instead, do I have to change the location of the vhost file ? (sudo nano /usr/local/nginx/sites-available/domain.com)?
I'm sorry if this is a really stupid question, I'm really, really new to CLI & VPS/linode. Thank you so much!
Test of URL Rewrite without .htaccess | Economatix October 27th, 2009 at 8:11 am
[...] guvnr.com [...]
the_guv October 28th, 2009 at 7:16 pm
@Jean .. yes, you are on the right track, a little tweaking of that vhost will do the trick .. post it up and I'll take a look.
10 Tips To Make WordPress Hack-Proof. The Ultimate Guide. - GUVNR November 2nd, 2009 at 12:05 am
[...] Set Up Unmanaged VPS BIBLE (4 Newbies) - Part 14: Tweak Nginx for WordPress - Pretty URLs & WP... 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 [...] [...]
thanh November 9th, 2009 at 5:05 pm
I followed these instructions and everything works. One question remains, is it possible to make the backend of wordpress allow installing plugins without asking for FTP login/password. I use to be able to install wordpress plugins via the wordpress backend on my on Cpanel host. Just was wondering if it's possible with nginx.
the_guv November 11th, 2009 at 3:11 pm
@thanh, good to hear. sure is possible .. just use the Add New option in the Plugins panel of your WordPress Dashboard .. that's what I tend to do and it's a great WP feature, giving an array of suggested plugins when you input a specific plugin name or keywords .. but then of course there is also Subversion which is darned handy, for instance, when you relocate a blog or set up one anew, and have a generic list of plugins that you want to install as a plugin 'base'.
That subversion method is covered in WordPress Setup & Updates from the Command Line. Installing subversion is covered in PHP5, MySQL, Xcache, Subversion (for Platforms like WordPress).
.. i never use that ftp login/pass method. i guess some folks like it.
Moses December 14th, 2009 at 11:15 am
Thanks for these great guides Guvnr, loving it so far.
Could I suggest at the end of this one you mention the way to check that the cache is working is to view the last line of your pages' source code and see the created time. Then refresh the page and check that it's served the prior page.
Cheers,
Moses.
the_guv January 9th, 2010 at 3:12 pm
@Moses .. nice tip, ta! .. will implement into a post revision.
(Will be revising all VPS Bible posts shortly, and ongoing, to add such ideas as well as keep up with any procedural changes due to new software etc.)
Andy Symonds January 22nd, 2010 at 12:25 pm
First up - amazing tutorial!!
Only slight issue I have is the same question as thanh had but I will rephrase it as I think you missed what he wanted
How do you enable the the backend of wordpress to allow installing plugins without asking for FTP login/password?
Currently after followung your turorial character by character I am asked for a FTP login when trying to install or deleted plugins and no matter what I put I can't use this but ideally I don't want to have to use this FTP login evenif it does work.
Thanks again
the_guv January 22nd, 2010 at 6:48 pm
@Andy - big cheers.
Permissions errors? Can't Install WP Plugins?
yes, to confirm, for WP (or any platform sat on Nginx that has internal facilities like plugin_installs/core_updates/file_uploads), do this to make it easy and stop any FTP detail requests:-
sudo chown -R www-data:www-data /home/public_html/blogOrWhatever.com/public
later on, if you try to SCP but get a permissions error, do:-
sudo chown -R yourUsername:www-data /home/public_html/blogOrWhatever.com/public
if you do SCP a lot, you can shortcut the chown procedures in bashrc .. search for bashrc on this site for details about command line shortcuts.
...
.. the Bible has fallen down here. Sorry for any confusion, will add detailed post and properly add this info in the WP section of the Bible next week.
franz February 3rd, 2010 at 10:55 pm
Hi guvnr, cannot find the videos. They are a lot easier to follow than written info. There is also
donations mentioned just where is the link. Ah I use
noscript could be that the reason for the missing
links?
the_guv February 3rd, 2010 at 11:52 pm
@franz .. you'll need to accept guvnr with your Noscrpt addon to get full functionality here, sure. hey, no dodgy scripts, I promise.
there's a dollar sign up there somewhere (er, script-wrapped!) for a donations link, but keep you dosh and sign up for vpsBible.com instead, which is about to launch and will have the VPS Bible ported there, along with a growing VPS knowledge base.
no video on this tutorial though .. I do have plans to make more though.