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?
Best Answer
Short answer: Yes, there is difference between
server_name "";
andserver_name _;
Long answer:
server_name "";
defines a match for a request withouthost
header since 0.8.48 and it has been supported since 0.7.12.https://nginx.org/en/docs/http/request_processing.html
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 notserver_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/