Winner of the Best WordPress Business Website - WP Honors 2011
Since 2004
 

Forums

 

Manuals

 

Videos

 

Live Support

 

WPMU.org

Nginx rewrite rules with wpmu, wp super cache, bbpress and sitemaps support

Elite

server {
listen 80;
server_name _ bandpress.org *.bandpress.org;

error_log /var/log/nginx/bandpress.org.error.log;
access_log /var/log/nginx/bandpress.org.access.log;

# Set this to off if you're hosting more than one site
server_name_in_redirect off;

#redirects 301
rewrite /togodaddy$ http://www.kqzyfj.com/click-3348421-10378406 permanent;
rewrite /towoothemes$ http://www.woothemes.com/amember/go.php?r=12635&i=b16 permanent;

# Rewrite WordPress
location /
{
gzip on;
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;

root /var/www/bandpress.org;
index index.php;

#rewrite for sitemaps
rewrite ^(.*/)?sitemap.xml /wp-content/sitemap.php last;
#rewite for anyfont
rewrite images/(.*)/(.*)\.png$ /wp-content/plugins/anyfont/img.php last;
#rewrite for blog files
rewrite ^.*/files/(.*) /wp-content/blogs.php?file=$1 last;

# WordPress pretty URLs
if (-f $request_filename)
{
expires max;
break;
}
if (-d $request_filename)
{
break;
}
# WP Super Cache
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;
}

#BBPress
if (!-e $request_filename) {
rewrite ^/forums/topic/(.*)$ /forums/topic.php last;
rewrite ^/forums/forum/(.*)$ /forums/forum.php last;
rewrite ^/forums/profile/(.*)$ /forums/profile.php last;
rewrite ^/forums/view/(.*)$ /forums/view.php last;
rewrite ^/forums/tags/(.*)$ /forums/tags.php last;
rewrite ^/forums/rss/(.*)$ /forums/rss.php last;
rewrite ^/forums/bb-admin/ /forums/bb-admin/index.php last;
rewrite ^/forums/ /forums/index.php last;
}

if (!-e $request_filename) {
rewrite ^.+/?(/wp-.*) $1 last;
rewrite ^.+/?(/.*\.php)$ $1 last;
rewrite ^(.+)$ /index.php?q=$1 last;
}

error_page 404 = /index.php?q=$uri;

}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/bandpress.org$fastcgi_script_name;
include fastcgi_params;
}

# deny access to .htaccess files, if Apache's document root concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}

Everyday at WPMU DEV we help hundreds of WP Users. Become a member today to:

  • Download 250+ Premium Plugins and Themes
  • Get unlimited support from WordPress Experts
  • Get help with your WordPress or BuddyPress projects

Help & Support from WPMU DEV members and staff

Erstwhile founder

Thanks a ton for posting this. I'm sure it will help a ton of people looking to switch to Nginx!

Thanks,
Andrew

Keeper of the Dark Chocolate

What's this for?

error_page 404 = /index.php?q=$uri;

Get personal, comprehensive and timely support and assistance
from WordPress Experts

Elite

This is the 404 handler , it passes it back to WordPress front controller

Keeper of the Dark Chocolate

Hmmm, for some reason the above doesn;t work for me. I get the "No input file" error. Googleing for that pulls up thousands of different hits, all of them basically giving a different solution.

I went ahead with a real basic config and I'll build from there.

server {
listen 80;
server_name cynix.uni.cc *.cynix.uni.cc;

access_log /srv/www/cynix.uni.cc/logs/access.log;
error_log /srv/www/cynix.uni.cc/logs/error.log;

location ~* ^.+\.(xml|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|swf)$
{
root /srv/www/cynix.uni.cc/public_html;
rewrite ^/files(/.*)$ /wp-content/blogs.php?file=$1 last;
expires 10d;
break;
}

location / {
root /srv/www/cynix.uni.cc/public_html;
index index.php;
error_page 404 = /index.php?q=$uri;
}

location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /srv/www/cynix.uni.cc/public_html$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}

}

I'll build from that and see where I went wrong.

Keeper of the Dark Chocolate

I officially give up on Nginx. Too many platforms with different and conflicting rules that wouldn't work for us. Have to give lighttpd a try and even that we have to install the 1.5 development version.

Staff

We just started using nginx here and things are much more speedy. But of course some of that is due to the new server.

Get personal, comprehensive and timely support and assistance
from WordPress Experts

Elite

@drmike

The rules for nginx are exactly the same on all unix-like platforms". I can't speak to Windows but using it on Windows defeats the purpose. I've used the same configurations on Linux 2.6.x and on FreeBSD 7.2 and 8.0, aside from changing paths for different machines.

The error you received was almost certainly from a missing or incorrect fastcgi parameter.

Before giving up you should consider the nginx mailing list or the forum which connects to it. The developer himself is usually happy to help.

We've been using nignx on all of our servers for quite a while now, with the exception of our shared hosting business (and we would change it if there was an easy way). It beats Apache hands down. Lighttpd is a consideration but the bugfix time is long. Have they even fixed the memory leak problem that was around for years? They're trying to be everything for everyone. Igor release bugfix patches for nginx almost immediately.

Keeper of the Dark Chocolate

Not sure where you see me using it on Windows there. Using FreeBSD or OSX on the servers and either Fedora or Debain on the VPSes.

The issue though was having over 80+ different sets of rewrite rules for the 80+ different platforms we support plus well over 200+ platforms that we don;t but still wind up installed on our servers. Since we're the ones installing the software, it makes it hard in this case to manage each and every sites-available file for each and every account. Since those files aren't normally available to endusers, we're the ones responsible to making sure they're correct and that's just not going to work.

I'm not going to get into a discussion of their mailing list. I've posted it it previously and it was pretty much a black hole for us. And I go nuts when asking for help when it comes to detail.

What's actually funny in addition is that every single tutorial I've seen except for single mention on the mailing list shows the incorrect method of setting up reporting to error logs.

Still running it on a test VPS we have that I'm now having to move all of my sites to thanks to a recent SEC ruling concerning my hosting. (*grumble* *grumble* *grumble*)

Thanks though for the feedback.

Keeper of the Dark Chocolate

I will say this though. Currently running a sitemap generator/ link checker against my blog. With apache, I could watch the load build. With Nginx, it's barely a tick.

Elite

If done *correctly* it should be the same set of rewrites for each platform. As I said, I've used it various Linux and FreeBSD platforms without changes for many types of sites, not just WP/WPMU. As for the sites-available or sites-included files, don't you do an Apache configuration for each vhost? Write a Perl or shell script to generate the file and send a HUP to nginx and you only have to enter the vhost name and run it. Not really hard to do.

I don't see your problems with the mailing list. Yeah sure, dumb questions aren't answered. Questions that have been answered *a lot* aren't answered again. Reasonable questions get a reasonable responses.

Also not sure what you mean about reporting to error logs. It's pretty straight forward and we can separate it per vhost, per location, or per server. Or we can create multiple logs, one per server and one for each vhost. The log module is highly flexible and easy to configure.

"With apache, I could watch the load build. With Nginx, it's barely a tick. "

Ding, ding, ding, we have a winner!

You can run huge sites with thousands of requests per second on commodity hardware much less expensively. There is a learning curve but you'd do yourself and your clients a favor climbing it.

Get personal, comprehensive and timely support and assistance
from WordPress Experts

Staff

Just switched my server to nginx. Very nice and smooth with those rules. They worked out of the box. Stopped Apache, started nginx all all was perfect. Thanks @johnnytee!

Oh, one thing I ran into that I didn't find anywhere is file uploads break unless you add this to the nginx.conf file:

client_max_body_size 8M;

(However many megs you allow for uploads, same as in php.ini)

Keeper of the Dark Chocolate

Just to mention, I was tracking down a "no input file specified" error elsewhere and came across this:

http://techtitbits.com/2010/07/nginx-resolving-no-input-file-specified-error/

Just to throw this out there.

Get personal, comprehensive and timely support and assistance
from WordPress Experts

Elite

If we're running suPHP and not FCGI, should we just delete the FCGI section? And do we need to replace it with anything?

Thanks,

Mark

Staff

You still need it, you have to pass scripts to php still.

Elite

Hi Aaron,

I'm not sure I understand...if we're running suphp, we don't have an FCGI server running at port 9000, etc...right? Or are you saying we need to set that up in addition to suphp?

Here's the part of the code I mean:

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/bandpress.org$fastcgi_script_name;
include fastcgi_params;
}

Sorry if this is a stupid question...FastCGI is not something we normally deal with.

Thanks,

Mark

Elite

Hi I'm in the process of gathering Nginx rewrite rules and anything related to help me install nginxcp here is my thread I started.
http://premium.wpmudev.org/forums/topic/planing-on-installing-nginx-need-advice
I'm sure this one will help a lot
thanks

Edited just read through the thread and I would like to know this also, as I'm also running suphp
I'm not sure I understand...if we're running suphp, we don't have an FCGI server running at port 9000, etc...right? Or are you saying we need to set that up in addition to suphp?

Get personal, comprehensive and timely support and assistance
from WordPress Experts

We can help you out. Become a member today to:

  • Let our team help you out today and access to over 250+ Premium Plugins and Themes
  • Find out why people saying "it's like having your own developer"
  • WPMU DEV - The WordPress Experts