How to Configure Nginx for Wordpress

Gonzalo

Member
Registered
Joined
May 6, 2016
Messages
21
Points
0
My wordpress sites are running as well on Apache but moving to Nginx, it has 404 error when viewing posts, permallinks is on. Are there any specific configurations to use Wordpress with Nginx?
 

ValeriaMxc

New member
Registered
Joined
Oct 12, 2012
Messages
12
Points
0
If you are getting this error, you must correct the line location / {...} in the configuration file of Nginx on the domain you're using.

Code:
location / {
        try_files $uri $uri/ /index.php?$args;
}
For Wordpress, you also need to do somethings to improve it on Nginx

For examples,

Protect your files and folders

#Limit Login
Code:
 location = /wp-login.php {
 allow your_IP_Address;
 deny all;
  
 try_files $uri =404;
 fastcgi_split_path_info ^(.+\.php)(/.+)$;
 include /etc/nginx/fastcgi_params;
 fastcgi_connect_timeout 180s;
 fastcgi_send_timeout 180s;
 fastcgi_read_timeout 180s;
 fastcgi_intercept_errors on;
 fastcgi_max_temp_file_size 0;
 fastcgi_pass 127.0.0.1:9000;
 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 fastcgi_index index.php;
 }
 

#Prevent Direct Access Of PHP Files From Web Browsers
location /wp-content/uploads/ {
   location ~ \.php$ {     
      deny all;
  }
}

Configure for sitemap of Yoast SEO


Code:
# Yoast sitemap
location ~ ([^/]*)sitemap(.*)\.x(m|s)l$ {
    rewrite ^/sitemap\.xml$ /sitemap_index.xml permanent;
    rewrite ^/([a-z]+)?-?sitemap\.xsl$ /index.php?xsl=$1 last;
    # Rules for yoast sitemap with wp|wpsubdir|wpsubdomain
    rewrite ^.*/sitemap_index\.xml$ /index.php?sitemap=1 last;
    rewrite ^.*/([^/]+?)-sitemap([0-9]+)?\.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;
    # Following lines are options. 
    rewrite ^/news_sitemap\.xml$ /index.php?sitemap=wpseo_news last;
    rewrite ^/locations\.kml$ /index.php?sitemap=wpseo_local_kml last;
    rewrite ^/geo_sitemap\.xml$ /index.php?sitemap=wpseo_local last;
    rewrite ^/video-sitemap\.xsl$ /index.php?xsl=video last;
access_log off;
}
Configure for WP Super Cache

Code:
# WP Super Cache
 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';
 }
After that, change location / {...} as follows

Code:
location / {
  try_files /wp-content/cache/supercache/$http_host/$cache_uri/index.html $uri $uri/ /index.php ;
}
Configure for W3 Total Cache

Code:
set $cache_uri $request_uri;
# POST requests and URL 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 URL containing the following segments
if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|wp-.*.php|index.php|/feed/|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 commenter
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in") {
  set $cache_uri 'null cache';
}
After that, change location / {...} to

Code:
# Use cached or actual file if they exists, Otherwise pass request to WordPress
location / {
  try_files /wp-content/cache/page_enhanced/${host}${cache_uri}_index.html $uri $uri/ /index.php?$args;
}

Creating nginx.conf file for your website


Code:
include /home/your_domain.com/public_html/nginx.conf;
Almost WordPress plugins today support Nginx and it will insert automatically data to nginx.conf so you need to create a file nginx.conf for them.

Good luck!
 
Latest Threads
Replies
1
Views
25
Replies
0
Views
180
Replies
1
Views
39
Replies
2
Views
81

Latest postsNew threads

Latest Hosting OffersNew Reviews

Sponsors

Tag Cloud

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top