WordPress Custom Nginx Rules

Below are the custom Nginx rules for WordPress

<!-- wp:paragraph -->
<p>if (!-e $request_filename) { rewrite ^(.<em>)$ /index.php break; }<br>rewrite /wp-config.php /forbidden last;<br>rewrite /install.php /forbidden last;<br>rewrite ^(.</em>)/uploads/(.*).php(.?) /forbidden last;<br>rewrite /xmlrpc.php /forbidden last;<br>location = /forbidden { deny all; }</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>location = /nginx.conf { deny all; }</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>location ~* .(txt)$ { charset utf-8; }</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>location ~* .(png|jpg|jpeg|gif|ico|bmp|img|ttf|otf|eot|svg|woff)$ {<br>expires 90d;<br>add_header Pragma public;<br>add_header Cache-Control public;<br>try_files $uri @fallback;<br>}</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>location ~* .(bz2|exe|gz|pdf|rar||tgz|zip)$ {<br>expires 2w;<br>add_header Pragma public;<br>add_header Cache-Control public;<br>try_files $uri @fallback;<br>}</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>location ~* .(ac3|avi|flv|iso|mp3|mp4|mpeg|mpg|ogg|qt|rm|swf|wav)$ {<br>expires 1w;<br>add_header Pragma public;<br>add_header Cache-Control public;<br>try_files $uri @fallback;<br>}</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>location ~* .(js|css|htm|html|xhtml|xml|dat|doc|docx|dts|ppt|pptx|tar|txt|xls|xlsx)$ {<br>expires 1w;<br>add_header Pragma public;<br>add_header Cache-Control public;<br>try_files $uri @fallback;<br>}</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>gzip on;<br>gzip_proxied any;<br>gzip_min_length 100;<br>gzip_buffers 8 16k;	# number size, default 32 4k|16 8k<br>gzip_types text/css text/plain text/javascript application/javascript application/json application/x-javascript application/xml application/xml+rss application/xhtml+xml application/x-font-ttf application/x-font-opentype application/vnd.ms-fontobject image/svg+xml image/x-icon application/rss+xml application/atom_xml;<br>gzip_vary on;<br>gzip_comp_level 9;<br>gzip_http_version 1.0;<br>gzip_disable "MSIE [1-6].(?!.*SV1)";</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>if (!-e $request_filename) {<br>rewrite /wp-admin$ $scheme://$host$uri/ permanent;<br>rewrite ^(/[^/]+)?(/wp-.<em>) $2 last;<br>rewrite ^(/[^/]+)?(/.</em>.php) $2 last;<br>}</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>set $no_cache 0;</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>if ( $http_cookie ~* "comment_author_|wordpress_(?!test_cookie)|wp-postpass_" ) {<br>set $no_cache 1;<br>}</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>if ($request_uri ~* "/(wp-admin/|wp-login.php)"){<br>set $no_cache 1;<br>}</p>
<!-- /wp:paragraph -->

 

 

Plesk + Cpanel nginx directives for wordpress, this is the fastest formula we came up with.

One thought on “WordPress Custom Nginx Rules

Comments are closed.