Jim
Jim

Reputation: 45

About priority of nginx location / {} and location = {}

When I study about the nginx location configuration, I have some questions.Here is my example.

the files structure is like this: test1/index.html test2/index.html

and the nginx.conf location part is like below:

    location = / {
            root test1;
            index index.html;
    #       deny all;
    }
    location  / {
        root test2;
        index  index.html;
    }

the question is , when i issue curl -v http://host/, I get the page of test2/index.html , but when I get rid of the # in the location = / {} part, the result will be 403 forbidden. can anyone explain why ? when both the location = same_uri {A} and location same_uri {B} are in the configuration file,which configuration will match[A or B]? Thank you very much.

http://nginx.org/en/docs/http/ngx_http_core_module.html#location

Upvotes: 4

Views: 3201

Answers (1)

Richard Smith
Richard Smith

Reputation: 49682

When you request the URI /, nginx will process two requests.

The first request (for the URI /) is processed by the location = / block, because that has highest precedence. The function of that block is to change the request to /index.html and restart the search for a matching location block.

The second request (for the URI /index.html) is processed by the location / block, because that matches any URI that does not match a more specific location.

So the final response comes from the second location block, but both blocks are involved in evaluating access.

See this document for location syntax and this document on the index directive.

Upvotes: 4

Related Questions