I'm having a problem with my nginx server configuration. The current behavior is that if a domain can't be found in any of the configuration files, it will redirect to a random other configuration. Instead, I would like to simply display a 404 error or something similar.
I've looked into using "default_server", and looked at other solutions - but then I noticed this:
If I use "default_server" in /etc/nginx/sites-enabled/, and test it with nginx -t
, it'll warn me about a duplicate: nginx: [emerg] a duplicate default server for 0.0.0.0:80 in /etc/nginx/sites-enabled/catchall:5
. I searched for the duplicate using grep -R default_server /etc/nginx
, and there is only one file that has default_server
in it: /etc/nginx/nginx.conf
.
Now, my confusion starts when I look at that file: It clearly already does what I wanted to do, in this block:
...
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
server_name_in_redirect off;
log_not_found off;
return 410;
}
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
...
My question is: Why does this not work already? Why does a wrong domain that isn't listed in any of the config files not default to a 410 error? Does the nginx.conf
even do something? What am I missing here?
I feel uncomfortable changing nginx.conf
without asking what the best approach here is, first. All I want to achieve, is to NGINX not randomly jumping to other domains if it can't find a specific domain.
How can I make a 404 catchall and where can I place it?