need to preserve / in location

Maxim Dounin mdounin at mdounin.ru
Wed Apr 22 15:17:36 UTC 2020


Hello!

On Wed, Apr 22, 2020 at 11:46:25AM +0300, Gregory Edigarov wrote:

> Hello, Everybody
> 
> this is directory structure:
> 
> /front/admin/index.html
> 
> /front/superadmin/index.html
> 
> that's what I have in config
> 
>     rewrite ^/(admin)$ /$1/ last;
>     location /admin/ {
>         index index.html;
>         root /front;
>         try_files $uri  admin/index.html;                 #direct all request to index.html
>    }
> 
> and the errors:
> 
> 2020/04/22 08:35:13 [error] 73#73: *1 open() "/frontindex.html" failed (2:
> No such file or directory), client: 192.168.224.1, server: , request: "GET
> /admin HTTP/1.1", host: "127.0.0.1"
> 192.168.224.1 - - [22/Apr/2020:08:35:13 +0000] "GET /admin HTTP/1.1" 404 146
> "-" "curl/7.58.0" "-"

That's stange, because with the configuration given the "/admin" 
request is expected to be mapped into "admin/index.html" as per 
try_files, and will end up opening "/frontadmin/index.html".

2020/04/22 17:42:59 [error] 38574#100110: *1 open() "/frontadmin/index.html" failed (2: No such file or directory), client: 127.0.0.1, server: , request: "GET /admin HTTP/1.1", host: "127.0.0.1:8080"

> 2020/04/22 08:35:24 [error] 73#73: *2 open() "/frontindex.html" failed (2:
> No such file or directory), client: 192.168.224.1, server: , request: "GET
> /admin/ HTTP/1.1", host: "127.0.0.1"
> 192.168.224.1 - - [22/Apr/2020:08:35:24 +0000] "GET /admin/ HTTP/1.1" 404
> 146 "-" "curl/7.58.0" "-"

Same here.

2020/04/22 17:46:20 [error] 38574#100110: *2 open() "/frontadmin/index.html" failed (2: No such file or directory), client: 127.0.0.1, server: , request: "GET /admin/ HTTP/1.1", host: "127.0.0.1:8080"

> what's the right config in situation given?

The right config depends on what you are trying to get.  In most 
cases, just as simple configuration with appropriate "root" will do 
what's expected ("index index.html;" is the default and can be 
safely omitted):

    location / {
        root /front;
    }

With such a configuration any request to "/admin" will end up with 
a redirect to "/admin/" (as long as "/front/admin" is a 
directory).  Any request to "/admin/" will return 
"/front/admin/index.html" (if exists).  And any request to a 
non-existent file will return 404.

If you really want to return a positive response regardless of 
whether a file exists or not, adding a leading "/" before 
"admin/index.html" in your configuration might work for you:  

     rewrite ^/(admin)$ /$1/ last;
     location /admin/ {
         root /front;
         try_files $uri /admin/index.html;
    }

(Note that "index index.html;" is meaningless - it is never used, 
as "try_files" without explicitly specified trailing "/" prevents 
access to directories.)

Alternatively, you may want to simplify configuration into 
something like:

    root /front;

    location = /admin {
        rewrite ^ /admin/ last;
    }

    location /admin/ {
        error_page 404 = /admin/index.html;
	log_not_found off;
    }

This configuration works much like the one with only "location /" 
above, but explicitly handles requests to "/admin" similarly to 
how it's handled in your configuration, and also handles 404 
errors to return appropriate index file.

-- 
Maxim Dounin
http://mdounin.ru/


More information about the nginx mailing list