WordPress NGINX configuration
I’ve been installing some WordPress sites on my new VPS running NGINX.
Installing WordPress with NGINX is not as complicated as it might seem but you have to do some configurations to increase security and to make it work with cache plugins.
As cache plugin I’ve chosen W3 total cache because the “Page Cache”, “disk:enhanced” method allows serving static HTML pages of my site and it includes some other performance improvements.
I’m not going to cover all the process of installing and configuring NGINX. Here you can find some tutorials if you’re looking for that.
First of all you have to include your domain/server configuration in “/etc/nginx/sites-available”. Create a new file with this content and adapt it with your domain / routes:
1server {
2 listen 80; ## listen for ipv4
3 listen [::]:80; ## listen for ipv6
4 server_name domain.com;
5 access_log /var/log/nginx/domain.com.access.log;
6 error_log /var/log/nginx/domain.com.error.log;
7
8 root /folder/with/wordpress/installation/;
9
10 # Include wordpress config file
11 include sites-available/wordpress.conf;
12}As I have various WordPress installations on my server I’m going to separate the WordPress configuration in another file and use it in my different server configurations. I’ve created a new file called “wordpress.conf” in “/etc/nginx/sites-available” (file is commented to explain the different options)
1index index.php index.html;
2
3set $cache_uri $request_uri;
4
5# POST requests and urls with a query string should always go to PHP
6if ($request_method = POST) {
7 set $cache_uri 'null cache';
8}
9if ($query_string != "") {
10 set $cache_uri 'null cache';
11}
12# Don't use the cache for logged in users or recent commenters
13if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in") {
14 set $cache_uri 'null cache';
15}
16
17# Use cached or actual file if they exists, otherwise pass request to WordPress
18location / {
19 rewrite ^([^.]*[^/])$ $1/ permanent;
20 try_files /wp-content/cache/page_enhanced/${host}${cache_uri}_index.html $uri $uri/ /index.php?$args;
21}
22
23location ~ ^/wp-content/cache/minify/[^/]+/(.*)$ {
24 try_files $uri /wp-content/plugins/w3-total-cache/pub/minify.php?file=$1;
25}
26
27# Show "Not Found" 404 errors in place of "Forbidden" 403 errors, because
28# forbidden errors allow attackers potential insight into your server's
29# layout and contents
30 error_page 403 =404;
31
32# Prevent access to any files starting with a dot, like .htaccess
33# or text editor temp files
34 location ~ /\. { access_log off; log_not_found off; deny all; }
35
36# Prevent access to any files starting with a $ (usually temp files)
37 location ~ ~$ { access_log off; log_not_found off; deny all; }
38
39# Do not log access to robots.txt, to keep the logs cleaner
40 location = /robots.txt { access_log off; log_not_found off; }
41
42# Do not log access to the favicon, to keep the logs cleaner
43 location = /favicon.ico { access_log off; log_not_found off; }
44
45# Keep images, CSS and other static files around in browser cache for
46# as long as possible, to cut down on server load
47location ~* .(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
48 expires max; log_not_found off; access_log off;
49}
50
51# Common deny or internal locations, to help prevent access to areas of
52# the site that should not be public
53 location ~* wp-admin/includes { deny all; }
54 location ~* wp-includes/theme-compat/ { deny all; }
55 location ~* wp-includes/js/tinymce/langs/.*\.php { deny all; }
56 location /wp-content/ { internal; }
57 location /wp-includes/ { internal; }
58# Protects the wp-config.php|readme.html|license.txt files from being
59# accessed (uncomment after wordpress installation)
60# location ~ /(\.|wp-config.php|readme.html|license.txt) { deny all; }
61
62# Prevent any potentially-executable files in the uploads directory from
63# being executed by forcing their MIME type to text/plain
64 location ~* ^/wp-content/uploads/.*.(html|htm|shtml|php)$ {
65 types { }
66 default_type text/plain;
67 }
68
69# Add trailing slash to */wp-admin requests so the admin interface
70# works correctly
71 rewrite /wp-admin$ $scheme://$host$uri/ permanent;
72
73location ~ \.php$ {
74 fastcgi_pass 127.0.0.1:9000;
75 fastcgi_index index.php;
76 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
77 include fastcgi_params;
78}Now, you have to make a symbolic link of the first file that you have created into “/etc/nginx/sites-enabled” and restart NGINX.
After that you can install WordPress normally. Once installed, you want to uncomment the line 59 of the “wordpress.conf” file ( location ~ /(.|wp-config.php|readme.html|license.txt) { deny all; } ) to disallow the access to the config.php file, the readme and the license.
For the W3 Total Cache plugin, you can enable the page Cache as “disk:enhanced” to serve the cached pages of your site. You can also enable minify or any other option (Object and Database Cache, CDN … etc). “Browser cache” is not really necessary because NGINX is going to take care of it.
And that’s all 🙂 Any comments or improvements are more than welcome.
Sources: