Finally using nginx's "try_files" directive
OLD:
error_page 404 = /wordpress/index.php?q=$request_uri;
or:
if (!-e $request_filename) { rewrite ^/(.*) /wordpress/index.php?uri=$request_uri last; }
NEW:
try_files $uri $uri/ /wordpress/index.php?q=$uri&$args;
Why make life more complicated if you don't need to?
Also thanks to Igor's patch you can have multiple of these, i.e.:
location /wordpress { try_files $uri $uri/ /wordpress/index.php?q=$uri&$args; } location /anotherapp { try_files $uri $uri/ /anotherapp/controllerfile.php?q=$uri&$args; }
etc.
This is good, as Drupal, WordPress and many other packages (including anything I develop anymore) use the Front Controller pattern of development, which makes deploying applications a lot simpler and helps support a consistent framework across the entire application.
NOTE: Updated on 1/5/10 to include the $args 🙂
So how exactly would this look like in a normal nginx config, is that all we post?
Well, for example my config is so... I use this site for wordpress or normal physical files, so no other apps. Otherwise I'd need to throw in some more location {} blocks. But I like to keep it simple.
Hmm.. seems that this requires wordpress to be installed into directory, not at the main url, ie http://site.com/blog instead of http://site.com - I got myself a rewrite loop with this since my wordpress uses later configuration. Still looking into it.
Dr T
Have you tried nginx 0.7.44? I believe he put in the fix that stops the infinite redirection. At least with location /foo { try_files with handler /foo/foo.php } - that was a bug/something that did not work in 0.7.43 without a patch.
If not, post it on the mailing list. Perhaps it's something he can patch.
Works well for me, with WordPress (2.7.1) in the domain root:
Nginx 0.7.50
I've got an issue where everything works but I cannot page my posts on the main page (ie "older posts" link). The important part is my permalink setting is category/postname. Paging works great everywhere except index.php.
Any ideas?
Post your entire config to pastebin or something. ask on #nginx on freenode, or the nginx mailing list.
So, I got this to work by updating to the latest stable build (0.7.59) but I can't get any images, styleshets or js code to even show.
Any clue how I can fix that?
I was able to fix it by adding this location directive (I put it right below the main /)
That does not look right at all. That's setting the root to be the root of your filesystem?
Thank you!
this fixed redirect loop problem with drupal, nginx and imagecache...
but i have no idea how.
I will issue a statement of caution - with Drupal, it will work, but depending on the plugins and modules you use it might break. This is something I need to draft up in a bug or something.
I used to be able to have just:
However, $uri winds up inheriting a "/" at the beginning which breaks certain code. WordPress handles it just fine. Drupal can too, probably with a very simple line of code in how it parses URIs. For now this appears to be the safest bet:
Note: Drupal also hosts some stuff people shouldn't be accessing in the webroot (ugh) so marking those in a block as "internal" will keep people from accessing them. I still haven't validated that rule yet (I am just starting to support Drupal) but I always maintain my "do it in the simplest possible way" rule. A lot of the examples out there look like a mess and can probably be done a hell of a lot cleaner.
Why are you adding the ?q= stuff? It's not necessary in the slightest.
Done. WordPress handles parsing the URL internally, all you have to do is force index.php to load. That's it.
Or, if you prefer try_files:
you are right. just a habit i suppose. might be interesting to look at the internals though, forcing the "q" parameter might cut down on some processing overhead (or vice-versa)
i need to look into trying to submit a patch to drupal to get them to handle the stuff with a / prefix being ignored. or ask someone. it must be a simple fix.
Thank you!! The try_files line worked perfect for me.
We're using the permalink settings by wordpress. An article gets an URL like: http://www.agileweboperations.com/20-devops-guys. I cannot make it work:
The try_files approach works for the homepage and the backoffice (/wp-admin), but not for the pretty permalink urls.
The !-e approach works for home and permalinks but does not recognize /wp-admin as an existing directory and tries to redirect, too, which fails.
Anyone experienced similar issues? If it helps I can post my full nginx.conf of course...
OMG! Can't tell you that how many people you have saved from going insane 😀
Thanks for your post - just what's needed 🙂
Thank you so much for this, it took me ages to find that I had to add &$args, something none of the other examples on other websites have shown, there are a few small things such as the theme preview in the WordPress admin that require this in order to work.
Thank you very much, you get me out of the hell ! ;p