简体   繁体   中英

NGINX Only path equals to without trailing slash and starts with path with trailing slash

I'm encountering an annoying error when it comes to my current NGINX app configuration.

I have a static web app which I am indexing on the path /admin/* . I want the index to be available on /admin , with and without a trailing slash, as well as available on a wildcard /admin/* (anything after the trailing slash).

The issue I am facing is that the index is accessable when appending anything after the admin path, for example /adminA/example .

The original NGINX configuration was as follows:

location /admin {
    alias /home/user/app/static;
    index index.html;
    try_files $uri $uri/ /index.html;
}

The best I've been able to implement to stop this at the moment is as follows, however i'm sure it can be done more efficiently:

location = /admin {
    alias /home/user/app/static;
    index index.html;
    try_files $uri $uri/ /index.html;
}

location /admin/ {
    alias /home/user/app/static/;
    index index.html;
    try_files $uri $uri/ /admin/index.html;
}

The two location blocks are already efficient, but you could eliminate the redundant code in the first block by redirecting to the second.

Using an internal redirect will be invisible to the browser. For example:

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

location /admin/ {
    ...
}

Or use permanent instead of last for an external redirect, which will change the browser's address bar from /admin to /admin/ . See the rewrite documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM