Example Nginx config file for WordPress

After installing Nginx it can be quite a pity to configure it correctly to work with WordPress. This example config file can be copied almost completely and you have your WordPress running in minutes.

Requirements

It’s optional but quite handy to have the WordPress files already in the right folders.

Example Nginx config file for WordPress

We’ll first discuss why a certain rule is in it. If you’d like to copy the full example at once scroll down a bit.

Explained

We first start by declaring a server block. All the rules we need to tell Nginx will come in here.

Because we are having a regular website in production at the regular port we tell Nginx to listen to port 80.

Now we have to tell Nginx were to start looking for files that it can server to the visitors. In this case it is /var/www/domain1.com, change it to your needs.

After telling were to find the files we declare what files Nginx should use as index files. It will look them up in the order you declare them. So in this case it will first look for the index.php file, then the index.html file and when it can’t find the first two it will look for index.htm. Ofcourse you could change it to whatever you called your index-file.

Now we need to configure on what domains Nginx should serve this site. It does not matter in what order you do this but make sure you add the www and the non-www version. Otherwise your site won’t be accessible both ways. If you have a spare domain that should also point to your WordPress installation you may add it here too.

 

Now we need to declare some stuff for PHP5-fpm to work properly. This is all quite default and almost always the same.

 

This little rule is important for an WordPress installation. You’ll find a lot of tutorials saying you can use something like try_files $uri /index.php$is_args$args; but this will give you a bug in your WordPress administration dashboard, wp-admin. When you’d visit /wp-admin it would give a redirect loop.

So use the snippet above and this bug is gone.

 

The comment in the above snippet explains it already a bit. Use these lines to make a 301 redirect from a (optional) second domain. So you won’t need this if you do not have second domain for your WordPress.

Using a 301 redirect instead of just serving the same website at the second domain has a positive effect on the SEO on your website.

 

This part of the configuration is copied straight for the plugin WordPress SEO by Yoast. It is needed to make your sitemaps accessible. Sitemaps are used by search engines like Google to find all the pages on your WordPress site. This also has a positive effect on your SEO. Next to that is WordPress SEO by Yoast the best plugin to make your WordPress site rank higher in the search engine results.

 

When paying attention you’ve seen that we opened the server block with a but never closed it. So there it is, the closing }.

Complete config file

 Save, link and restart

Now you may save your .conf file, link it by making a symbolic link to Nginx’ sites-enabled folder and restart Nginx.