使 nginx 提供相对于当前路径的 404 文档
make nginx serve 404 documents relative to current path
我有以下服务器块:
server {
listen 80;
server_name petpal.co.il;
root /usr/share/nginx/petpal;
index index.php;
location / {
try_files $uri $uri/ @extensionless-php;
}
location @extensionless-php {
rewrite ^(.*)$ .php last;
}
location ~ \.php$ {
try_files $uri /notfound;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
我需要做哪些修改才能使 404 文档根据用户当前所在的目录显示,例如在目录“/en”中显示“/en/notfound" 和目录 "/" 中它会显示 "/notfound"?
也许 if
可行?
set $lang "";
if ($uri ~ "^/en/") { set $lang "en/"; }
try_files $uri /${lang}notfound;
或者,如果您想要更通用的规则:
set $lang "";
if ($uri ~ "^/([a-z][a-z])/)") { set $lang ""; }
try_files $uri /${lang}notfound;
感谢cnst的帮助,终于解决了问题!
基本上我所要做的就是将这段代码放在 php 位置块之外:
set $lang "";
if ($uri ~ "^/en/") { set $lang "en/"; }
error_page 404 /${lang}notfound;
然后在 php 块内简单地做:
try_files $uri =404;
我有以下服务器块:
server {
listen 80;
server_name petpal.co.il;
root /usr/share/nginx/petpal;
index index.php;
location / {
try_files $uri $uri/ @extensionless-php;
}
location @extensionless-php {
rewrite ^(.*)$ .php last;
}
location ~ \.php$ {
try_files $uri /notfound;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
我需要做哪些修改才能使 404 文档根据用户当前所在的目录显示,例如在目录“/en”中显示“/en/notfound" 和目录 "/" 中它会显示 "/notfound"?
也许 if
可行?
set $lang "";
if ($uri ~ "^/en/") { set $lang "en/"; }
try_files $uri /${lang}notfound;
或者,如果您想要更通用的规则:
set $lang "";
if ($uri ~ "^/([a-z][a-z])/)") { set $lang ""; }
try_files $uri /${lang}notfound;
感谢cnst的帮助,终于解决了问题! 基本上我所要做的就是将这段代码放在 php 位置块之外:
set $lang "";
if ($uri ~ "^/en/") { set $lang "en/"; }
error_page 404 /${lang}notfound;
然后在 php 块内简单地做:
try_files $uri =404;