Support » Networking WordPress » Working Nginx rewrite rules for wordpress multisite 3.x WP Super Cache

  • First of all, Hi.
    I got back into blogging recently and because i already switched to a LEMP stack for another project i decided to stick with it. I just spent all day long trying to set the rewrite rules correctly but 99% of the ones found on the internet simply didn’t work until i got to a really good one. Everything works as it should, subdomains work properly and media files show properly.

    To spare others from having to search for hours like i did i’m going to post my nginx.conf file contents here and other useful tips.

    nginx.conf

    server {
            listen   80;
        server_name *.domain.tld;
        access_log /srv/www/www.domain.tld/logs/access.log;
        error_log /srv/www/www.domain.tld/logs/error.log;
        root   /srv/www/www.domain.tld/public_html;
        index  index.html index.htm index.php;
    
        location ~ .*\.(php|php5)?$ {
            include fastcgi_params;
            fastcgi_pass  127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param  SCRIPT_FILENAME  /srv/www/www.domain.tld/public_html$fastcgi_script_name;
        }
    
           rewrite ^.*/files/(.*)$ /wp-includes/ms-files.php?file=$1 last;
           if (!-e $request_filename) {
            rewrite  ^(.+)$  /index.php?q=$1  last;
          }

    Save and do a sudo service nginx restart to apply changes.

    Everything else is just like the Apache setup:

    add

    define('WP_DEBUG', false);
    define( 'MULTISITE', true );
    define( 'SUBDOMAIN_INSTALL', true );
    $base = '/';
    define( 'DOMAIN_CURRENT_SITE', 'domain.tld' );
    define( 'PATH_CURRENT_SITE', '/' );
    define( 'SITE_ID_CURRENT_SITE', 1 );
    define( 'BLOG_ID_CURRENT_SITE', 1 );

    in your wp-config.php before the line /* That’s all, stop editing! Happy blogging. */

    create the /wp-content/blogs.dir/ and set permissions to 777 ( it has to be writable )

    That’s pretty much it. I will set up WP Super Cache now and post that config file as well.

Viewing 11 replies - 1 through 11 (of 11 total)
  • Bookmarking so when someone asks I can point ’em here. ;P

    Thread Starter mmaama

    (@mmaama)

    I can’t edit the first post anymore so i’ll post the wp-super-cache config here.

    nginx.conf

    server {
            listen   80;
        server_name *.domain.tld;
        access_log /srv/www/www.domain.tld/logs/access.log;
        error_log /srv/www/www.domain.tld/logs/error.log;
        root   /srv/www/www.domain.tld/public_html;
        index  index.html index.htm index.php;
    
        location ~ .*\.(php|php5)?$ {
            include fastcgi_params;
            fastcgi_pass  127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param  SCRIPT_FILENAME  /srv/www/www.domain.tld/public_html$fastcgi_script_name;
        }
    
    #super-cache start
    
        location / {
          index index.php;
          if (-f $request_filename) {
             expires max;
             break;
          }
          if (-d $request_filename) {
             break;
          }
          set $supercache_file '';
          set $supercache_uri $request_uri;
    
          if ($request_method = POST) {
             set $supercache_uri '';
          }
          if ($query_string) {
             set $supercache_uri '';
          }
    
          if ($http_cookie ~* "comment_author_|wordpress|wp-postpass_" ) {
             set $supercache_uri '';
          }
    
          if ($supercache_uri ~ ^(.+)$) {
             set $supercache_file /wp-content/cache/supercache/$http_host/$1index.html;
          }
    
          if (-f $document_root$supercache_file) {
             rewrite ^(.*)$ $supercache_file break;
          }
          }
    #super-cache end
    
    #multisite start
           rewrite ^.*/files/(.*)$ /wp-includes/ms-files.php?file=$1 last;
           if (!-e $request_filename) {
            rewrite  ^(.+)$  /index.php?q=$1  last;
          }
    #multisite end

    I added comments in case people want to have a single instance blog with super-cache instead of a multisite.

    I hope this helps people running on nginx to set up their blogs faster and easier 🙂

    Tested and guaranteed working ( My server has nginx 1.0.4 + wordpress 3.2 RC1 but it will work on any nginx+wp3.x setup ).

    Thread Starter mmaama

    (@mmaama)

    sorry to bump again but i noticed that super-cache might not work properly ( super-caches only the homepage and never the other pages ). to fix that change

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

    to

    if (!-e $request_filename) {
            rewrite  ^(.+)$  /index.php  last;
          }

    I tested repeatedly with multiple browsers and it super-caches correctly and for all pages.

    Hi I have one more hair to pull out before I have to stop looking for the answer.
    I have looked at so many tutorials and HowTo’s that I’m confused
    I have 1 multisite and 3 single wp sites a few phpld directory site and 3 phpbb sites on the same Private Cloud VPS server
    where ( nginx.conf ?)and how would you put the additional rewrites do you do the same but list the different domains?
    and the super caches rewrites they just have to be listed once and they will not effect other sites (phpbb ect) or do I put it in each domain section?
    sorry for all the questions
    like this

    server {
            listen   80;
        server_name *.domain1.tld;
        access_log /srv/www/www.domain.tld/logs/access.log;
        error_log /srv/www/www.domain.tld/logs/error.log;
        root   /srv/www/www.domain.tld/public_html;
        index  index.html index.htm index.php;
    
        location ~ .*\.(php|php5)?$ {
            include fastcgi_params;
            fastcgi_pass  127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param  SCRIPT_FILENAME  /srv/www/www.domain1.tld/public_html$fastcgi_script_name;
        }
    
    server {
            listen   80;
        server_name *.domain2.tld;
        access_log /srv/www/www.domain.tld/logs/access.log;
        error_log /srv/www/www.domain.tld/logs/error.log;
        root   /srv/www/www.domain.tld/public_html;
        index  index.html index.htm index.php;
    
        location ~ .*\.(php|php5)?$ {
            include fastcgi_params;
            fastcgi_pass  127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param  SCRIPT_FILENAME  /srv/www/www.domain2.tld/public_html$fastcgi_script_name;
        }
    server {
            listen   80;
        server_name *.domain3.tld;
        access_log /srv/www/www.domain.tld/logs/access.log;
        error_log /srv/www/www.domain.tld/logs/error.log;
        root   /srv/www/www.domain.tld/public_html;
        index  index.html index.htm index.php;
    
        location ~ .*\.(php|php5)?$ {
            include fastcgi_params;
            fastcgi_pass  127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param  SCRIPT_FILENAME  /srv/www/www.domain3.tld/public_html$fastcgi_script_name;
        }

    Thanks in advance I was going to do it like the codex suggests but I be-leave its not clear

    You need to use try_files in your location block for security, performance and simplicity.

    Here are two great resources:

    No Sites you must be editing now I was too fast :>
    I will probably finish typing and you will be done editing. let me save and check :>

    No problem Mitch!

    There are all sorts of tips and tricks on nginx’s official website. Not sure if you’d been there before, but it never hurts to go straight to the source.

    In regard to your questions, ideally you want to set up separate .conf files for each site. They usually go in the /sites-enabled directory, depending on which OS and how you installed nginx. Using your example, I do mine like this:
    domain1.tld.conf
    domain2.tld.conf
    domain3.tld.conf

    I found this
    try_files $uri $uri/ /index.php?q=$uri&$args;

    I think I need to hire someone to do this its over my head. I just finished installing nginx 1.04 I didn’t start it yet or switch Apache over to 81.

    I have a lot of sites I need rules for.
    3 – phpBB
    1 – WordPress 3.2 MultiSite – New no users yet
    4 – WordPress 3.2 Single Site
    7 – phpLD Link Directory

    Mitch, with VPS’s, you’ve got lots of options!

    You could stick with apache and move mysql on to a separate VPS. Apache really does make life simple. Control panels support it, all the major caching projects (for your projects) support it and there’s tons of info on how to tweak apache and php to handle heavy traffic.

    Moving to nginx means no control panel. Limited information. Limited resources. A lot more work.

    If you do put mysql on its own small vps, you can optimize it too.

    +1 on the WordPress side, where you can use WP Super Cache (or that other one) to plug in to a CDN. Offloading your static files to a CDN lightens the load on your apache VPS.

    See, I call W3TC “the other one”, because every time I’ve tried to use it with nginx and multisite, I end up pulling my hair. I’ve never got it to work right. Apparently it’s dead simple with apache.

    Plus, with nginx, you also have to install/configure and run php-fpm…. Even if you hire someone to set it all up, it’s not entirely “set and forget”.

    Thanks for your help You gave me some great ideas

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Working Nginx rewrite rules for wordpress multisite 3.x WP Super Cache’ is closed to new replies.