Trang chủ WordpressHướng dẫn Wordpress Tổng hợp các cấu hình NGINX cho WordPress

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

bởi Thạch Phạm
10 bình luận 8,5K views

Tham gia nhóm hỗ trợ WordPress

Tham gia nhóm Hỗ trợ Server - Hosting & WordPress để cùng nhau hỏi đáp và hỗ trợ các vấn đề về WordPress, tối ưu máy chủ/server.

Tham gia ngay

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ả.

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.

[bash]
location / {
try_files $uri $uri/ /index.php?$args;
}
[/bash]

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

[bash]
# Hạn chế đăng nhâp
location = /wp-login.php {
allow ĐIỀN IP MÁY TÍNH CỦA BẠN VÀO ĐÂY;
deny all;

Khuyến mãi Black Friday

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;
}

# 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;
}
}
[/bash]

3. Cấu hình cho sitemap củaYoast SEO

[bash]
# 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;
}
[/bash]

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

[bash]
# 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’;
}
[/bash]

Sau đó sửa location / {...} thành như sau:

[bash]
location / {
try_files /wp-content/cache/supercache/$http_host/$cache_uri/index.html $uri $uri/ /index.php ;
}
[/bash]

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

[bash]
# 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’;
}
[/bash]

Sau đó sửa location / {...} thành:

[bash]
# 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;
}
[/bash]

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:

[bash]include /home/domain.com/public_html/nginx.conf;[/bash]

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é.

Đánh giá nội dung này
10 bình luận

Có thể bạn quan tâm

Hiện tại blog tạm đóng bình luận vì mình cần tập trung thời gian vào cập nhật bài viết. Bình luận sẽ mở ra cho đến khi mình sẵn sàng.