我正在尝试转换一组为 Apache 创建的重写规则,用于托管 WordPress 网站以供 NginX(fastcgi、PHP)使用。
据我了解,WordPress 有一个页面(“ expo
”),然后使用主题中的代码来检查请求参数(“ p
”)。然后它使用“ p
”进行一些数据库查询并呈现内容。
因此您可以通过调用 来访问此页面/expo?p=name_of_expo
。
要求 URL 的结构如下:/expo/name_of_expo
我已将其精简为最基本的重写规则:
location ~ ^/expo/.+/?$ {
rewrite ^/(.+)/(.+)/?$ /$1/?p=$2? last;
try_files $uri $uri/ /index.php?$args;
}
location / {
index index.php;
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
当转到 时/expo?p=name_of_expo
,会显示正确的页面。但是,当转到 时/expo/name_of_expo
,会触发 WordPress 404。
nginx 的重写日志表明写入了正确的 URL:
2012/03/08 16:56:16 [notice] 15995#0: *7698 "^/(.+)/(.+)/?$" matches "/expo/name_of_expo", client: 192.168.60.116 [snip...]
2012/03/08 16:56:16 [notice] 15995#0: *7698 rewritten data: "/expo/", args: "p=name_of_expo", client: 192.168.60.116 [snip...]
必需的软件版本信息:
nginx version: nginx/1.0.13
spawn-fcgi v1.6.3 (ipv6) - spawns FastCGI processes
Build-Date: May 25 2010 12:33:48
PHP 5.2.10 (cli) (built: Nov 13 2009 11:44:05)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2009 Zend Technologies
CentOS release 5.7 (Final)
答案1
尝试这个
server {
error_page 405 = @handler;
error_page 404 = @handler;
location / {
if (-f $request_filename) {
break;
}
if (!-e $request_filename) {
rewrite . /index.php last;
}
}
location @handler {
rewrite / /index.php;
}
location ~ \.php/ {
rewrite ^(.*.php)/ $1 last;
}
location ~ \.php$ {
if (!-e $request_filename) { rewrite / /index.php last; }
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
在你的 fastcgi_params 中
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;