使nginx相对于当前路径提供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 ^(.*)$$1.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 "$1"; }
try_files $uri /${lang}notfound;

转载注明原文:使nginx相对于当前路径提供404文档 - 代码日志