1

Can anyone tell me why is this ngnix config doesn't match all URL that starts with /admin :

    location /admin {
        alias {{path_to_static_page}}/admin/build/;
        try_files $uri $uri/ /index.html;
    }

It always fall back to the default content of location / . However, I hardcoded all the possible URL in the Nginx config, it works and only matches the hard coded URL, something like :

        location /admin {
            alias {{path_to_static_page}}/admin/build/;
            try_files $uri $uri/ /index.html;
        }

        location /admin/news/ {
            alias {{path_to_static_page}}/admin/build/;
            try_files $uri $uri/ /index.html;
        }

        location /admin/another-url/ {
            alias {{path_to_static_page}}/admin/build/;
            try_files $uri $uri/ /index.html;
        }

Thanks for your help.

2
  • The two solutions are not the same. And part of the problem may be the /index.html which is a URI that does not begin with /admin. Perhaps you mean to use /admin/index.html? Commented May 14, 2020 at 10:14
  • The index.html file is located in {{path_to_static_page}}/admin/build/ , my goal is to whenever someone type /admin/something, they should see the index.html file inside the {{path_to_static_page}}/admin/build/ directory Commented May 14, 2020 at 10:19

1 Answer 1

1

The final term of the try_files statement is a URI. The URI of the index.html file at /path/to/admin/build/index.html is /admin/index.html.

Using alias and try_files in the same location block can be problematic.

You may want to use a more reliable solution:

location ^~ /admin {
    alias /path/to/admin/build;
    if (!-e $request_filename) { rewrite ^ /admin/index.html last; }
}

The location and alias values should both end with / or neither end with /. The ^~ operator will prevent other regular expression location blocks from matching any URI that begins with /admin. See this caution on the use of if.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.