Implicit root location?

J. Lewis Muir jlmuir at
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      [::1]:80;
    server_name localhost "" [::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">
<meta charset="UTF-8"/>

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:

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

Thank you!


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

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      [::1]:80;
        server_name localhost "" [::1];
        root        /srv/www/localhost;

More information about the nginx mailing list