2

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

1 Answer 1

1

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.

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.