WordPress + Nginx Minimal Configuration

Update: This article is updated for WordPress 3.5 multisite’s file-handling.

easyengine (ee) note: If you are using easyengine, you can accomplish everything in this article using following command:

ee site create example.com --wpsubdir

In today’s article, we will setup WordPress Multisite using Subdirectories. This is simplest mode of Multisite setup as it doesn’t have any external dependency on DNS records or server/network configuration.

If you haven’t created a WordPress-Multisite network yet, please check our guide here.

Nginx Config

Below is recommended Nginx configuration.

server {
        ##DM - uncomment following line for domain mapping  
        #listen 80 default_server;
    server_name example.com *.example.com ;
    ##DM - uncomment following line for domain mapping
    #server_name_in_redirect off;

    access_log   /var/log/nginx/example.com.access.log;
    error_log    /var/log/nginx/example.com.error.log;

    root /var/www/example.com/htdocs;
    index index.php;

    if (!-e $request_filename) {
        rewrite /wp-admin$ $scheme://$host$uri/ permanent;  
        rewrite ^(/[^/]+)?(/wp-.*) $2 last;                     
        rewrite ^(/[^/]+)?(/.*\.php) $2 last;                   
    }

    location / {
        try_files $uri $uri/ /index.php?$args ;
    }

    location ~ \.php$ {
        try_files $uri =404;
        include fastcgi_params;
        fastcgi_pass 127.0.0.1:9000;
    }

    location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
        access_log off; log_not_found off; expires max;
    }

    location = /robots.txt { access_log off; log_not_found off; }
    location ~ /\. { deny  all; access_log off; log_not_found off; }
}

Domain Mapping

You need to uncomment few lines in above nginx-config to get domain-mapping working. Apart from above config changes, you can read this guide to setup/configure domain-mapping.

Must Read:

10 responses to “WordPress + Nginx Minimal Configuration”

  1. Hey Rahul,
    Thanks for your tutorials – they’ve been really helpful. However, I’m still having one issue with multisite/nginx configuration after using this tutorial. Basically, it seems like something’s wrong with pages that are within my subdirectory multsites. Going to the subdirectory (e.g. example.com/mydir) seems to work, but going further (e.g. example.com/mydir/post-1) does not. I asked the question on stackoverflow, with the specific domain, etc. to see the problem here: stackoverflow.com/questions/14590274/urls-of-a-wordpress-multisite-subdirectory-not-showing-up-correctly-with-nginx

    Any help would be much appreciated!

  2. I added a comment earlier today, but my config has since started working (I’m not really sure why…). Just wanted to send you a note to say that my previous comment (and this one) are not necessary to be approved.

    Thanks again for the great tutorial!

  3. Hello, thank you for the guide, it was really helpful, however beyond a point I encountered some difficulties.

    Basically I can’t get nginx to handle websites with the following siteurl:

    localhost/wptest/site1

    If I modify the siteurl after I create a new site in the network dashboard, it works though:

    localhost/site2

    I described the issue here, complete with error log: WordPress 3.5 Multisite and nginx

    Your comment would be appreciated!

    Thank you!

  4. I forgot to mention the files are uploading to

    blogs.dir/2/files/2013/07/

    where /2/ is the id of the site, which is good, the mapping is not right and they are not showing on the site.

  5. Indeed, as I mentioned, I am trying to set-up 3.4 as well. In fact, I am working on migrating 3.4 to 3.5 (not simply upgrading) and this is why I am setting up to different versions.

    Now, I’ve seen that link and already had the two locations for blogs.dir and for ms-files. However with the second location, the one for ms-files, nginx doesn’t even start… Here’s how my server config looks now:

    http://d.pr/i/wP7C

    Thanks, again, for your support.