Make nginx submit 404 documents relative to the current path

I have the following server block:

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;
    }
}

What changes do I need to make in order for the 404 document to be displayed in accordance with the directory in which the user is located, for example, in the directory "/ en" will display "/ en / notfound" and in the directory "/" will display / notfound "?

+4
source share
2 answers

Thanks to the help of cnst, I was able to solve the problem! Basically all I needed to do was put this piece of code outside the php location block:

set $lang "";
if ($uri ~ "^/en/") { set $lang "en/"; }
error_page 404 /${lang}notfound;

and then inside the php block just do:

try_files $uri =404;
0
source

Perhaps it will work if?

set $lang "";
if ($uri ~ "^/en/") { set $lang "en/"; }
try_files $uri /${lang}notfound;

Or, if you want a more general rule:

set $lang "";
if ($uri ~ "^/([a-z][a-z])/)") { set $lang "$1"; }
try_files $uri /${lang}notfound;
0
source

Source: https://habr.com/ru/post/1598912/


All Articles