26

I want to set a default catch-all server block to handle any host names that do not match my exact server_name values. But I'm unsure which one should I use.

server {
    listen 80 default_server;
    server_name ""; # this is by default if no server_name specified
    return 444;
}

or

server {
    listen 80 default_server;
    server_name _;
    return 444;
}

I've tested and they seem to behave equally. Is there any difference between them?

2

1 Answer 1

30

Short answer: Yes, there is difference between server_name ""; and server_name _;

Long answer: server_name ""; defines a match for a request without host header since 0.8.48 and it has been supported since 0.7.12.

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

If requests without the “Host” header field should not be allowed, a server that just drops the requests can be defined:

server {
    listen      80;
    server_name "";
    return      444;
}

Here, the server name is set to an empty string that will match requests without the “Host” header field, and a special nginx’s non-standard code 444 is returned that closes the connection.

On the other side, server_name _; defines an invalid server names which never intersect with any real name. It is just a non-match. So in the event of no matches, nginx will select the first server{} block and use that.

To conclude, you can use server_name _; for catch-all server block but not server_name "";.

Reference - https://stackoverflow.com/questions/9454764/nginx-server-name-wildcard-or-catch-all https://blog.gahooa.com/2013/08/21/nginx-how-to-specify-a-default-server/

3
  • Worth noting that you need to use HTTP 1.0 (-0 paramter in curl) to test the server_name "" server block. curl -v -0 -H "Host:" http://localhost. See the accepted answer from serverfault.com/a/920459/485927
    – Rick
    May 31, 2019 at 4:38
  • 10
    I wonder, why do we need server_name _; directive when we have listen with default_server? Is this for informational purposes?
    – Psylone
    Feb 3, 2020 at 0:39
  • 2
    I recommend to read this --> nginx.org/en/docs/http/request_processing.html Dec 30, 2020 at 18:56

You must log in to answer this question.

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