25

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?

NARKOZ
  • 998
  • 3
  • 15
  • 23

1 Answers1

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/

Rick
  • 309
  • 2
  • 4
  • 15
Pramod Singh
  • 411
  • 4
  • 5
  • 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 https://serverfault.com/a/920459/485927 – Rick May 31 '19 at 04: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 03 '20 at 00:39
  • 1
    I recommend to read this --> https://nginx.org/en/docs/http/request_processing.html – Veerendra K Dec 30 '20 at 18:56