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
- A working installation of Nginx. Learn how to do that in this tutorial about installing Nginx on Ubuntu 14.04 LTS
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
1 2 3 4 5 |
server { listen 80; root /var/www/domain1.com; index index.php index.html index.htm; server_name domain1.com www.domain1.com domain2.com www.domain2.com; |
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.
1 2 3 4 5 6 7 |
location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; } |
Now we need to declare some stuff for PHP5-fpm to work properly. This is all quite default and almost always the same.
1 2 3 |
location / { try_files $uri $uri/ /index.php; } |
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.
1 2 3 4 5 |
# If using a second domain with 301 redirect if ($host ~* ^domain2\.com$) { rewrite ^(.*) $scheme://domain1.com$1 permanent; break; } |
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.
1 2 3 |
#Yoast sitemap rewrite ^/sitemap_index\.xml$ /index.php?sitemap=1 last; rewrite ^/([^/]+?)-sitemap([0-9]+)?\.xml$ /index.php?sitemap=$1&sitemap_n=$2 last; |
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.
1 |
} |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
server { listen 80; root /var/www/domain1.com; index index.php index.html index.htm; server_name domain1.com www.domain1.com domain2.com www.domain2.com; location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; } location / { try_files $uri $uri/ /index.php; } if ($host ~* ^domain2\.com$) { rewrite ^(.*) $scheme://domain1.com$1 permanent; break; } # Yoast Sitemap rewrite ^/sitemap_index\.xml$ /index.php?sitemap=1 last; rewrite ^/([^/]+?)-sitemap([0-9]+)?\.xml$ /index.php?sitemap=$1&sitemap_n=$2 last; } |
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.