13

I tried the 4 cases of code with default_server below one by one in Nginx. The file default.conf with the code is located at /etc/nginx/conf.d/default.conf.

1st: With server_name _;

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name _;

    return 500;
}

2nd: With a different server name(private ip) server_name 10.0.0.0; (My computer's private ip is 10.156.58.216)

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name 10.0.0.0;
    
    return 500;
}

3rd: With server_name "";

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name "";

    return 500;
}

4th: Without server name

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    return 500;
}

My computer's private ip is 10.156.58.216.

enter image description here

Then, the results are all the same showing 500 Internal Server Error page.

enter image description here

I think for default server, we don't need server name. So the 4th code without server name is fine to use.

4th: Without server name

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    return 500;
}

I ask again, Is server name really needed for default server?

2

2 Answers 2

17

From the Nginx documentation:

If no server_name is defined in a server block then nginx uses the empty name as the server name.

Regarding server_name _;, the same document states:

There is nothing special about this name, it is just one of a myriad of invalid domain names which never intersect with any real name.

3

The only time NGINX looks at the server_name directive and listen's default_server parameter during processing the configuration is when there is more than one server block listening on the same [ip_address:]port. (It's also worth pointing out that there can be more than one default_server declaration in one config - one per [ip_address:]port.)

If a server block stands alone in an active configuration (i.e., there are no other server blocks listening on the same port, such as 80), then all of your examples can omit

  • the default_server parameter

    (because they are the default by default - and there is no other server block around to process requests on port 80 anyway)

  • the server_name directive

    (as they are the only server blocks for port 80, so NGINX won't even bother comparing server_name with a request's Host header: all the requests would be directed there anyway)

When does the server_name directive need to be used in tandem with default_server?

One example is when you need a catch-all block to dismiss any connection that is not explicitly handled in other server blocks.

For a concrete example, here's an HTTP-to-HTTPS redirect:

##########################
# HTTP-to-HTTPS redirect #
##########################
server {
    listen 80;
    server_name ourdomain.com;

    return 301 https://$host$request_uri;
}

##########################
# HTTP-to-HTTPS redirect #
##########################
server {
    listen 80;
    server_name another.doma.in;

    return 301 https://$host$request_uri;
}

# ... more `server` blocks listening on port 80

#############
# CATCH-ALL #
#############
server {
   listen 80 default_server; 
   server_name _;

   return 404;
}

In this case, the catch-all block must have both:

  • a server_name declaration that doesn't match any valid domain

    When NGINX sees that there are more than one block for port 80, it compares the request's Host field to server_name to choose the right one. If none match, then the request will be routed to the default_server. (See How NGINX processes a request and the flowchart at the bottom.)

    It's important this block does not match anything because it can be anywhere in the config file, so it can accidentally process a request intended for another one. (Plus, the point is to explicitly configure the domains / IP addresses the server will handle, and deny everything else.)

  • a listen ... default_server ... declaration

    If the catch-all block for that [ip_address:]port is the first one in the file, it will implicitly become the default server for it. Then again, it can also be anywhere in the config, plus it never hurts to be explicit.

flowchart for processing NGINX configurations

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .