Tôi Thích Kiếm Tiền

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



  1. 1. Cấu hình permalink cho NGINX
  2. 2. Bảo mật các tập tin và thư mục nhạy cảm
  3. 3. Cấu hình cho sitemap củaYoast SEO
  4. 5. Cấu hình cho WP Super Cache
  5. 6. Cấu hình cho W3 Total Cache
  6. 7. Tạo thêm tập tin nginx.conf cho website

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.

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 { allow ĐIỀN IP MÁY TÍNH CỦA BẠN VÀO ĐÂY; 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; }# Chặn truy cập PHP trong thư mục uploadslocation /wp-content/uploads/ { location ~ /.php$ { #Prevent Direct Access Of PHP Files From Web Browsers deny all; }}

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

# Yoast sitemaplocation ~ ([^/]*)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 Cacheset $cache_uri $request_uri;# POST requests and URL with a query string should always go to phpif ($request_method = POST) { set $cache_uri 'null cache';}if ($query_string != "") { set $cache_uri 'null cache';}# Don't cache URL containing the following segmentsif ($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 commenterif ($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 WordPresslocation / { 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é.

Tổng hợp các cấu hình NGINX cho WordPress was last modified: Tháng Chín 19th, 2016 by Thạch Phạm
nginxwebserver

Categories