Thứ Tư, 4 tháng 11, 2015

Tổng hợp các cấu hình NGINX cho WordPress

Hiện nay trên ThachPham.com có khá nhiều serie liên quan đến NGINX và WordPress mà mình viết rải rác, các serie đó thường đều rất dài nhưng thực ra nó có chung một phần là cấu hình NGINX để chạy website WordPress. Như vậy sắp tới mình sẽ viết lại các serie đó theo hướng dễ hiểu hơn và không quá đi sâu vào WordPress, nên mình sẽ đăng tổng hợp các cấu hình NGINX dành cho WordPress trong bài viết này để mọi người có thể dễ dàng tìm kiếm.
Nếu bạn dùng EasyEngine thì không cần chèn thêm cái gì cả.

Mục lục nội dung

1. Cấu hình permalink cho NGINX
2. Bảo mật các tập tin và thư mục nhạy cảm
3. Cấu hình cho sitemap của SEO by Yoast
5. Cấu hình cho WP Super Cache
6. Cấu hình cho W3 Total Cache
7. Tạo thêm tập tin nginx.conf cho website
Có rất nhiều bạn hỏi là tại sao cài NGINX vào thì website luôn bị lỗi 404 khi xem bài viết có chỉnh permalinks, đơn giản là đối với WordPress, bạn phải sửa lại dòng location / {...} trong tập tin cấu hình NGINX của domain bạn đang sử dụng.
01
02
03
location / {
        try_files $uri $uri/ /index.php?$args;
}

2. Bảo mật các tập tin và thư mục nhạy cảm

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
# Hạn chế đăng nhâp
location = /wp-login.php {
  limit_req zone=one burst=1 nodelay;
  include fastcgi_params;
  fastcgi_pass php;
}
 
# Chặn truy cập wp-config.php
location = /wp-config.php {
  deny all;
  access_log off;
  log_not_found off;
}
 
# Chặn truy cập PHP trong thư mục uploads
location /wp-content/uploads/ {
  location ~ \.php$ {
    #Prevent Direct Access Of PHP Files From Web Browsers
    deny all;
  }
}

3. Cấu hình cho sitemap của SEO by Yoast

01
02
03
04
05
06
07
08
09
10
11
12
13
14
# 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. Needed for WordPress <a href="http://thachpham.com/category/seo">seo</a> addons
  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;
}

5. Cấu hình cho WP Super Cache

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
# WP Super Cache
set $skip_cache 0;
# POST requests and URL with a query string should always go to php
if ($request_method = POST) {
  set $skip_cache 1;
}
if ($query_string != "") {
  set $skip_cache 1;
}
# 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 $skip_cache 1;
}
# Don't use the cache for logged in users or recent commenter
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
  set $skip_cache 1;
}
Sau đó sửa location / {...} thành như sau:
01
02
03
location / {
  try_files $uri $uri/ /index.php?$args;
}

6. Cấu hình cho W3 Total Cache

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
# <a href="http://thachpham.com/wordpress/wp-plugin/huong-dan-w3-total-cache-toan-tap.html">W3 Total Cache</a>
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';
}
Sau đó sửa location / {...} thành:
01
02
03
04
# 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;
}

7. Tạo thêm tập tin nginx.conf cho website

Hiện nay các plugin WordPress nếu có hỗ trợ NGINX thì có thể sẽ tự chèn các cấu hình NGINX vào tập tin nginx.conf trong thư mục gốc của website. Do vậy, bạn nên tạo sẵn một tập tin nginx.conf trong thư mục public của website và tiến hành nhúng nó vào tập tin cấu hình NGINX của domain như sau:
01
include /home/domain.com/public_html/nginx.conf;
Nhớ sửa lại đường dẫn cho chính xác.
Nếu bạn cần thêm cấu hình nào cho NGINX thông dụng trong WordPress thì hãy comment để mình bổ sung nhé.

Không có nhận xét nào:

Đăng nhận xét