Nginx location testing is not redirecting to the correct page

August 6, 2015 553 views
Nginx LEMP


I am completely new to nginx, therefore please be kind. I have my domain registered and I am trying to get the following to happen.

When a user enters I want them to get a 404. When they enter in I want them to get my index.html. This is an unusual use case, but its what I would like.

I have set-up a site under

/var/www/example/publichtml // /with index.html as well
html/test //with index.html as well

The config is located under:


The symbolic link is working and is:

Heres my config for example:

        listen 80;
        server_name example;

        #not sure if i need the below to lines?
    root /var/www/example/public_html;
        index index.html index.htm;

        location /
                return 404;

        location /test/
                root /var/www/example/public_html/test;
                index index.html index.htm;

When I enter, I do get my 404. But when I go to I just get a 404 as well, when I was hoping the /test/ would be matching and change the root so the index would show.

If I get rid of the two location checks, then typing goes to my index.html page as expected.

Any help would be appreciated.

Thank you.

1 Answer

Hello, I solved this just in case anyone is interested, After looking in the error.log, etc/logs/nginx/error.log I found the following:

2015/08/06 10:19:46 [error] 8402#0: 3 "/var/www/example/public_html/test/test/index.html" is not found (2: No such file or directory), client: **, server: example, request: "GET /test/ HTTP/1.1", host: ""

So it looks like when you are modifying the root, it also concatenates the current get uri context which explains the test\test. Hope this might help someone. Thank you.

Have another answer? Share your knowledge.