FAQ

Page Discussion Edit History

WordPress

(Redirected from Wordpress)

Nginx works perfectly well with a wide variety of applications, and WordPress is certainly one of them. NginX's configuration language is very powerful and straightforward if one is familiar with it, but often people coming from other servers are not sure how things work in NginX and just copy and paste whatever they see from a blog that seems to fill their needs. Everyone, especially people new to NginX, should check out the nginx.org documentation for an overview of how things work and should be done in NginX.

Contents

[edit] Abridged basic setup

Hopefully you have read the documentation above and maybe worked on setting up a virtual server or two in Nginx already - if not there are a few notes below, but you should still read the documentation.

First we setup a named upstream for our php, which allows us to abstract the backend and easily change the port or add more backends. After that, we setup our virtual host configuration for domain.tld.

# Upstream to abstract backend connection(s) for php
upstream php {
        server unix:/tmp/php-cgi.socket;
        server 127.0.0.1:9000;
}
 
server {
        ## Your website name goes here.
        server_name domain.tld;
        ## Your only path reference.
        root /var/www/wordpress;
        ## This should be in your http block and if it is, it's not needed here.
        index index.php;
 
        location = /favicon.ico {
                log_not_found off;
                access_log off;
        }
 
        location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
        }
 
        location / {
                # This is cool because no php is touched for static content
                try_files $uri $uri/ /index.php;
        }
 
        location ~ \.php$ {
                #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
                include fastcgi.conf;
                fastcgi_intercept_errors on;
                fastcgi_pass php;
        }
 
        location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
                expires max;
                log_not_found off;
        }
}

With this configuration we should be able to serve wordpress very easily. Once you setup your backend (php-cgi or php-fpm) should work perfectly.

[edit] Location Strategies

There are many ways to declare your locations in your configuration that allow you to do basically whatever you want with your URLs. Usually, people want to have "pretty" URLs that hide the query strings and script files. Here are a few different strategies based on different goals. Here we are defining locations that should be used to replace the basic locations above in order to achieve the desired results.

[edit] Non-root try_files to URL redirect

If you want to serve WordPress as a sub directory, you will want to make the following changes (relative to the above configuration).

        location /wordpress {
                try_files $uri $uri/ /wordpress/index.php;
        }
 
        location ~ \.php$ {
                fastcgi_split_path_info ^(/wordpress)(/.*)$;
        }

[edit] Pre-0.8.30 fastcgi settings

If you are using a version below 0.8.30, you will want to add this to your fastcgi_params file.

    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;


[edit] Rewrite rules for Multisite

Wordpress Multisite require permalinks and redirection to existing PHP files, thus we have to remove the blog name from the URLs.

location / {
    error_page 404 = @wp;
 
    location ~* \.(php|inc)$ {
# Forbid PHP on upload dirs
        if ($uri ~ "uploads") {
            return 403;
        }
 
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_intercept_errors on;
        include fastcgi_params;
        fastcgi_pass php;
    }
 
}
 
# Rewrite rules. Blog name must be removed.
location @wp {
 
# Redirect to files
  rewrite ^(/[^/]+/)?files/(.+) /wp-includes/ms-files.php?file=$2 last;
 
# Match only one section
  rewrite ^(/[^/]+)?(/wp-.*) $2 last;
  rewrite ^(/[^/]+)?(/.*\.php) $2 last;
 
# Send everything else to index.php (permalinks)
  rewrite ^/(.*)$ /index.php?q=$1 last;
 
}