manshou
manshou

Reputation: 357

Why is nginx "location = /" rule not work?

I want to define an exact match for '/' request by using 'location = /' rule and specify 'index.html' as the response of this request. But why my setting isn't work?

I have defined two locations as below (Updated: I also post the whole content of my nginx.conf at the bottom.):

location = / {
    root  /opt/www/static/;
    index index.html;
}
location / {
    root /opt/www/resource/;
}

And the files in my "/opt/www" directory are as below. (The comments after # The content is: describe the file content in them.)

/opt/www
    ├── resource
    │   ├── hello.html     # The content is: The hello.html from /resource
    │   └── index.html     # The content is: The index.html from /resource
    └── static
        └── index.html     # The content is: The index.html from /static

But i when i access following urls, the outputs are:

  1. http://localhost or http://localhost/ - Response is: The index.html from /resource.
  2. http://localhost/index.html - Response is: The index.html from /resource.
  3. http://localhost/hello.html - Response is: The hello.html from /resource.

I think the result of #2 and #3 are correct, but for #1, why it returns the resource/index.html as response instead of static/index.html? Because i think, according to the definition of location, the response should come from static/index.html file.

using the “=” modifier it is possible to define an exact match of URI and location. If an exact match is found, the search terminates. For example, if a “/” request happens frequently, defining “location = /” will speed up the processing of these requests, as search terminates right after the first comparison.

Another question is, how to change my conf file to specify static/index.html as the response of http://localhost or http://localhost/ by using an exact match?


Updated

I found the trick after turning on nginx debug log by using error_log logs/error.log debug;. According the log, for #1, the request is exactly matched by the first rule location = /, and /opt/www/static/index.html is open. But later, the request is internal redirected to /index.html, then the second rule is matched, as a result, /opt/www/resource/index.html is used.

But my question is, why it redirects the request to /index.html when the exact rule (the first one) is already matched and /opt/www/static/index.html is found? can i stop the internal redirect with some configuration or other directive?

The nginx log is (my nginx version is 1.4.6):

2015/05/01 11:59:46 [debug] 112241#0: *1 http process request line
2015/05/01 11:59:46 [debug] 112241#0: *1 http request line: "GET / HTTP/1.1"
2015/05/01 11:59:46 [debug] 112241#0: *1 http uri: "/"
2015/05/01 11:59:46 [debug] 112241#0: *1 http args: ""
2015/05/01 11:59:46 [debug] 112241#0: *1 http exten: ""
*** omit some logs to process request header and others ***
2015/05/01 11:59:46 [debug] 112241#0: *1 event timer del: 3: 1430452846698
2015/05/01 11:59:46 [debug] 112241#0: *1 generic phase: 0
2015/05/01 11:59:46 [debug] 112241#0: *1 rewrite phase: 1
2015/05/01 11:59:46 [debug] 112241#0: *1 test location: "/"
2015/05/01 11:59:46 [debug] 112241#0: *1 using configuration "=/"
2015/05/01 11:59:46 [debug] 112241#0: *1 http cl:-1 max:1048576
2015/05/01 11:59:46 [debug] 112241#0: *1 rewrite phase: 3
2015/05/01 11:59:46 [debug] 112241#0: *1 post rewrite phase: 4
2015/05/01 11:59:46 [debug] 112241#0: *1 generic phase: 5
2015/05/01 11:59:46 [debug] 112241#0: *1 generic phase: 6
2015/05/01 11:59:46 [debug] 112241#0: *1 generic phase: 7
2015/05/01 11:59:46 [debug] 112241#0: *1 access phase: 8
2015/05/01 11:59:46 [debug] 112241#0: *1 access phase: 9
2015/05/01 11:59:46 [debug] 112241#0: *1 post access phase: 10
2015/05/01 11:59:46 [debug] 112241#0: *1 content phase: 11
2015/05/01 11:59:46 [debug] 112241#0: *1 open index "/opt/www/static/index.html"
2015/05/01 11:59:46 [debug] 112241#0: *1 internal redirect: "/index.html?"
2015/05/01 11:59:46 [debug] 112241#0: *1 rewrite phase: 1
2015/05/01 11:59:46 [debug] 112241#0: *1 test location: "/"
2015/05/01 11:59:46 [debug] 112241#0: *1 using configuration "/"
2015/05/01 11:59:46 [debug] 112241#0: *1 http cl:-1 max:1048576
2015/05/01 11:59:46 [debug] 112241#0: *1 rewrite phase: 3
2015/05/01 11:59:46 [debug] 112241#0: *1 post rewrite phase: 4
2015/05/01 11:59:46 [debug] 112241#0: *1 generic phase: 5
2015/05/01 11:59:46 [debug] 112241#0: *1 generic phase: 6
2015/05/01 11:59:46 [debug] 112241#0: *1 generic phase: 7
2015/05/01 11:59:46 [debug] 112241#0: *1 access phase: 8
2015/05/01 11:59:46 [debug] 112241#0: *1 access phase: 9
2015/05/01 11:59:46 [debug] 112241#0: *1 post access phase: 10
2015/05/01 11:59:46 [debug] 112241#0: *1 content phase: 11
2015/05/01 11:59:46 [debug] 112241#0: *1 content phase: 12
2015/05/01 11:59:46 [debug] 112241#0: *1 content phase: 13
2015/05/01 11:59:46 [debug] 112241#0: *1 content phase: 14
2015/05/01 11:59:46 [debug] 112241#0: *1 content phase: 15
2015/05/01 11:59:46 [debug] 112241#0: *1 http filename: "/opt/www/resource/index.html"

Updated again to post the whole content of my nginx.conf.

worker_processes  1;
error_log  /var/log/nginx/error.log debug;
pid        /var/run/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /var/log/nginx/access.log  main;
    sendfile        on;
    keepalive_timeout  65;
    include /etc/nginx/conf.d/*.conf;
    server {
        listen       80;
        server_name  localhost;
        location = / {
            root  /opt/www/static/;
            index index.html;
        }
       location / {
            root /opt/www/resource/;
       }
    }
}

Upvotes: 4

Views: 1541

Answers (1)

manshou
manshou

Reputation: 357

After doing some research and google, i found a solution to resolve this question by myself :-). Using the try_files directive to replace the index one, it does not trigger an internal redirect when static/index.html is found. The final definitions are:

location = / {
    root  /opt/www/static/;
    #index index.html;
    try_files /index.html =404;
}
location / {
    root /opt/www/resource/;
}

Upvotes: 2

Related Questions