Last active
June 30, 2023 12:20
-
-
Save williamgomes/a7a396fa3b5709c51c5630717d6f53b0 to your computer and use it in GitHub Desktop.
Nginx Config for Wordpress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
########### | |
paste this inside virtual host file /etc/nginx/sites-available/<YOUR CONFIG FILE> | |
########### | |
server { | |
listen 80; | |
server_name <DOMAIN_OR_HOST_NAME>; | |
root /var/www/html/<FOLDER_NAME>; | |
index index.php index.html index.htm; | |
access_log /var/log/nginx/<DOMAIN_OR_HOST_NAME>.access.log; | |
error_log /var/log/nginx/<DOMAIN_OR_HOST_NAME>.error.log; | |
include global/wordpress.conf; | |
include global/restrictions.conf; | |
include global/wordpress-w3-total-cache.conf; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
########### | |
paste below code inside /etc/nginx/restrictions.conf | |
########### | |
# Global restrictions configuration file. | |
# Designed to be included in any server {} block. | |
location = /favicon.ico { | |
log_not_found off; | |
access_log off; | |
} | |
location = /robots.txt { | |
allow all; | |
log_not_found off; | |
access_log off; | |
} | |
# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac). | |
# Keep logging the requests to parse later (or to pass to firewall utilities such as fail2ban) | |
location ~ /\. { | |
deny all; | |
} | |
# Deny access to any files with a .php extension in the uploads directory | |
# Works in sub-directory installs and also in multisite network | |
# Keep logging the requests to parse later (or to pass to firewall utilities such as fail2ban) | |
location ~* /(?:uploads|files)/.*\.php$ { | |
deny all; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
########### | |
If you are using w3 total cache then this file will be useful. Otherwise no need. | |
paste this inside /etc/nginx/global/wordpress-w3-total-cache.conf file | |
########### | |
#W3 TOTAL CACHE CHECK | |
set $cache_uri $request_uri; | |
# POST requests and urls with a query string should always go to PHP | |
if ($request_method = POST) { | |
set $cache_uri 'null cache'; | |
} | |
if ($query_string != "") { | |
set $cache_uri 'null cache'; | |
} | |
# Don't cache uris containing the following segments | |
if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") { | |
set $cache_uri 'null cache'; | |
} | |
# Don't use the cache for logged in users or recent commenters | |
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in") { | |
set $cache_uri 'null cache'; | |
} | |
#ADD mobile rules from WP SUPER CACHE section above | |
# Use cached or actual file if they exists, otherwise pass request to WordPress | |
location / { | |
try_files /wp-content/w3tc/pgcache/$cache_uri/_index.html $uri $uri/ /index.php?$args ; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
########### | |
paste this inside /etc/nginx/global/wordpress.conf file | |
########### | |
if ($http_x_forwarded_proto != 'https') { | |
return 301 https://$server_name$request_uri; | |
} | |
elseif ($http_x_forwarded_proto == 'https') { | |
fastcgi_param HTTPS on; | |
} | |
location / { | |
# This is cool because no php is touched for static content. | |
# include the "?$args" part so non-default permalinks doesn't break when using query string | |
try_files $uri $uri/ /index.php?$args; | |
} | |
location ~ \.php$ { | |
try_files $uri =404; | |
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini | |
include fastcgi_params; | |
# fastcgi_param HTTPS on; | |
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; | |
fastcgi_intercept_errors on; | |
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; | |
fastcgi_split_path_info ^(.+\.php)(/.+)$; | |
} | |
# Only need this part if you using timestamp with your css, js or image files to keep track of update | |
location ~* (.+)\.(?:\d+)\.(js|css|png|jpg|jpeg|gif)$ { | |
try_files $uri $1.$2; | |
} | |
# Add trailing slash to */wp-admin requests. | |
rewrite /wp-admin$ $scheme://$host$uri/ permanent; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment