D
D
DEamON_M2016-10-19 20:34:31
Nginx
DEamON_M, 2016-10-19 20:34:31

nginx. How to show an error when trying to access a non-existent subdomain?

There are 3 subdomains
test1.domain.ru
test2.domain.ru
test3.domain.ru
and the main domain domain.ru
Each has its own nginx config and everything works. However, if you go to some test234123.domain.ru, then the contents of domain.ru will appear. How to write an error in this case?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Ukolov, 2016-10-19
@DEamON_M

nginx first decides which server should process the request. Consider a simple configuration where all three virtual servers are listening on port *:80:
server {
    listen      80;
    server_name example.org www.example.org;
    ...
}

server {
    listen      80;
    server_name example.net www.example.net;
    ...
}

server {
    listen      80;
    server_name example.com www.example.com;
    ...
}

In this configuration, nginx only checks the “Host” field of the request header to determine which server to send the request to. If its value does not match any of the server names, or the request header does not contain this field at all, nginx will forward the request to the default server for that port. In the configuration above, the default server will be the first server, which is the default behavior of nginx. The default server can be set explicitly using the default_server parameter in the listen directive:
server {
    listen      80 default_server;
    server_name example.net www.example.net;
    ...
}
https://nginx.org/ru/docs/http/request_processing.html
Thus, you need to create a separate server for the wrong domains, make it the default server and simply redirect to the file with the error message.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question