我想为我的一个虚拟服务器进行特定的配置,但我无法向 Nginx 解释这一点:)
这很简单。如果 URI 看起来像
example.com/whatever_1/whatever_2/.../whatever_n
我想将其重写为
example.com/index.php?request=whatever_1/whatever_2/.../whatever_n
第二种情况是,如果 URI 以/administration/
类似
example.com/administration/whatever_1/.../whatever_n
我希望它被重写为
example.com/administration/index.php?request=whatever_1/.../whatever_n
我四处摸索并尝试:
server
{
# listen 80;
server_name example.com;
index index.html index.php;
root /srv/example/;
location ~ /administration/(.*)$
{
if (!-e $request_filename)
{
rewrite ^/(.*)$ /administration/index.php?request=$1 last;
}
break; #tried with and without it
}
location / #tried with and without this location block
{
if (!-e $request_filename)
{
#rewrite ^/(.*)$ /index.php?/$1 last;
rewrite ^/(.*)$ /index.php?request=$1 last;
}
}
location ~ \.php$ #boilerplate
{
# Filter out arbitrary code execution
fastcgi_index index.php;
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
}
但这不起作用。我对 Nginx 还很陌生,所以任何帮助我都会很感激。
谢谢,简。
答案1
这是 nginx,不是 apache。您应该尽可能避免重写(而且通常是可能的)。
使用try_files
而是。例如:
location /administration/ {
try_files $uri $uri/ /administration/index.php?request=$request_uri;
}
location / {
try_files $uri $uri/ /index.php?request=$request_uri;
}