Tổng hợp các cấu hình NGINX cho WordPress
Hiện nay trên cddos.net 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.
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
1. Cấu hình permalink cho NGINX
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.
...}
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
# 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
# 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 seo 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
# 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';
}
Sau đó sửa location / { thành như sau:
...}
location / {
try_files /wp-content/cache/supercache/$http_host/$cache_uri/index.html $uri $uri/ /index.php ;
}
6. Cấu hình cho W3 Total Cache
# W3 Total Cache 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:
...}
# 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:
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é.