Implicit root location?

J. Lewis Muir jlmuir at imca-cat.org
Tue Jul 30 21:20:41 UTC 2019


Hello, all!

I have a minimal nginx.conf with one server block that sets the root
directory but has *no* location directives, yet for a request of "/", it
serves "/index.html".  Why?  With no locations specified, I expected it
to return 404 or similar for any request.

Here's the server block (entire nginx.conf at end of message):

----
server {
    listen      127.0.0.1:80;
    listen      [::1]:80;
    server_name localhost "" 127.0.0.1 [::1];
    root        /srv/www/localhost;
}
----

Here's the contents of /srv/www/localhost:

----
$ ls -al /srv/www/localhost
total 4
drwxr-xr-x. 2 root root  24 Jul 30 15:50 .
drwxr-xr-x. 3 root root  23 Jun 26 21:34 ..
-rw-r--r--. 1 root root 140 Jun 26 22:22 index.html
----

And here's the curl invocation:

----
$ curl 'http://localhost/'
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8"/>
<title>localhost</title>
</head>
<body>
<p>localhost</p>
</body>
</html>
----

I know that the default index directive is

----
index index.html;
----

That explains how it knows to try index.html, but what makes it try
the root when there are no location directives?  Is there an implicit
location directive?

There is no default listed for the location directive:

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

And I couldn't find this behavior stated in "How nginx processes a
request:"

  https://nginx.org/en/docs/http/request_processing.html

Thank you!

Lewis

---- Complete nginx.conf ----
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    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;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include      /etc/nginx/mime.types;
    default_type application/octet-stream;

    server {
        listen      127.0.0.1:80;
        listen      [::1]:80;
        server_name localhost "" 127.0.0.1 [::1];
        root        /srv/www/localhost;
    }
}
----


More information about the nginx mailing list