[WordPress] 外掛分享: nginx Compatibility

首頁外掛目錄 › nginx Compatibility
⚠ 此外掛已下架 — 不再更新維護,建議勿安裝。
100+
安裝啟用
★★★☆☆
3.7/5 分(3 則評價)
5371 天前
最後更新
問題解決
WordPress 2.5+ v0.2.5 上架:2009-04-14

內容簡介

rdPress is installed in a subdirectory, you need to adjust the path accordingly.

Finally, don't forget to save your changes and restart nginx to apply them.

這個外掛解決了兩個問題:

當 WordPress 檢測到使用 FastCGI PHP SAPI 時,它會忽略傳遞給 wp_redirect 的重定向狀態碼。因此,所有的 301 重定向都會變成 302 重定向,這對 SEO 可能不利。該外掛會在檢測到使用 nginx 時,覆蓋 wp_redirect。
當 WordPress 檢測到 mod_rewrite 無法使用時,它會回傳 Permalink 設置頁面中的 PATHINFO permalink。nginx 本身具有 URL 重寫的內置支持,因此不需要使用 PATHINFO permalink。因此,該外掛檢測到使用 nginx 時,使 WordPress 認為 mod_rewrite 已加載,可以使用漂亮的永久鏈接。

該插件不需要進行任何配置,它只要工作就好。安裝並忘記它的存在就好。

警告:nginx 必須正確配置以支持永久鏈接。

nginx 配置

nginx 0.7.32 及更高版本:

server {
server_name mysite.com;

root /path/to/blog;

index index.php;

error_page 404 = @wordpress;
log_not_found off;

location ^~ /files/ {
rewrite /files/(.+) /wp-includes/ms-files.php?file=$1 last;
}

location @wordpress {
fastcgi_pass ...;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_NAME /index.php;
}

location ~ \.php$ {
try_files $uri @wordpress;
fastcgi_index index.php;
fastcgi_pass ...;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}

location ^~ /blogs.dir/ {
internal;
root /path/to/blog/wp-content;
}
}

舊版:

server {
server_name mysite.com;

root /path/to/blog;

index index.php;

log_not_found off;
error_page 404 = @wordpress;

location ^~ /files/ {
rewrite /files/(.+) /wp-includes/ms-files.php?file=$1 last;
}

location @wordpress {
fastcgi_pass ...;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_NAME /index.php;
}

location ~ \.php$ {
if (!-e $request_filename) {
rewrite ^(.+)$ /index.php break;
break;
}

fastcgi_index index.php;
fastcgi_pass ...;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}

location ^~ /blogs.dir/ {
internal;
root /path/to/blog/wp-content;
}
}

當然,不要忘記在 fastcgi_pass 中用 php-cgi 監聽的地址/套接字替換 ...,並將 /path/to/blog 替換為實際路徑。

請注意,SCRIPT_NAME 中的路徑應該相對於 DOCUMENT_ROOT (root 指令)。因此,如果您的 WordPress 安裝在子目錄中,需要相應地調整路徑。

最後,請不要忘記保存更改並重新啟動 nginx,以應用它們。

外掛標籤

開發者團隊

⬇ 下載最新版 (v0.2.5) 或搜尋安裝

① 下載 ZIP → 後台「外掛 › 安裝外掛 › 上傳外掛」
② 後台搜尋「nginx Compatibility」→ 直接安裝(推薦)
📦 歷史版本下載

原文外掛簡介

The plugin solves two problems:

When WordPress detects that FastCGI PHP SAPI is in use, it
disregards the redirect status code
passed to wp_redirect. Thus, all 301 redrects become 302 redirects
which may not be good for SEO. The plugin overrides wp_redirect when it detects
that nginx is used.
When WordPress detects that mod_rewrite is not loaded (which is the case for nginx as
it does not load any Apache modules) it falls back to PATHINFO permalinks
in Permalink Settings page. nginx itself has built-in support for URL rewriting and does not need
PATHINFO permalinks. Thus, when the plugin detects that nginx is used, it makes WordPress think
that mod_rewrite is loaded and it is OK to use pretty permalinks.

The plugin does not require any configuration. It just does its work.
You won’t notice it — install and forget.
WARNING: nginx must be configured properly to support permalinks.
nginx Configuration
nginx 0.7.32 and higher:
server {
server_name mysite.com;

root /path/to/blog;

index index.php;

error_page 404 = @wordpress;
log_not_found off;

location ^~ /files/ {
rewrite /files/(.+) /wp-includes/ms-files.php?file=$1 last;
}

location @wordpress {
fastcgi_pass ...;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_NAME /index.php;
}

location ~ \.php$ {
try_files $uri @wordpress;
fastcgi_index index.php;
fastcgi_pass ...;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}

location ^~ /blogs.dir/ {
internal;
root /path/to/blog/wp-content;
}
}

Older versions:
server {
server_name mysite.com;

root /path/to/blog;

index index.php;

log_not_found off;
error_page 404 = @wordpress;

location ^~ /files/ {
rewrite /files/(.+) /wp-includes/ms-files.php?file=$1 last;
}

location @wordpress {
fastcgi_pass ...;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_NAME /index.php;
}

location ~ \.php$ {
if (!-e $request_filename) {
rewrite ^(.+)$ /index.php break;
break;
}

fastcgi_index index.php;
fastcgi_pass ...;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}

location ^~ /blogs.dir/ {
internal;
root /path/to/blog/wp-content;
}
}

Of course, do not forget to replace ... in fastcgi_pass with the address/socket
php-cgi is listening on and replace /path/to/blog with the actual path.
Also please note that the path in SCRIPT_NAME should be relative to the DOCUMENT_ROOT (root directive).
Thus, if your WordPress blog resides in http://example.com/blog/, root is set to /path.to/example.com,
SCRIPT_NAME in location @wordpress will be /blog/index.php.
Multi-Site Configuration: the above configs work perfectly with WordPress MultiSite. To make downloads faster, consider adding this line to wp-config.php:
define('WPMU_ACCEL_REDIRECT', true);

Need help with configuring nginx? Contact me: vkolesnikov at odesk dot com, I will try to help you.

延伸相關外掛

文章
Filter
Apply Filters
Mastodon