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

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;

}

}

  • drmike
    • DEV MAN’s Mascot

    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.

  • Jim O
    • New Recruit

    @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.

  • drmike
    • DEV MAN’s Mascot

    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.

  • Jim O
    • New Recruit

    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.

  • Aaron
    • CTO

    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)

  • wpcdn
    • Syntax Hero

    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

  • HamRadioDude
    • HummingBird

    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.

    https://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?

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.