Home > Software engineering >  Nginx is taking the whole domain and not the subdomain
Nginx is taking the whole domain and not the subdomain

Time:09-09

I've installed Nginx on a fresh EC2 instance (Amazon Linux 2) with a basic config file:

server {
    listen 80;
    listen [::]:80;

    root /var/www/html;

    index index.html index.htm index.nginx-debian.html;

    server_name atlasalgorithms.kadiemqazzaz.com;

    location / {
           try_files $uri $uri/ =404;
    }
}

Now Nginx is serving both http://atlasalgorithms.kadiemqazzaz.com and http://kadiemqazzaz.com but I want Nginx to serve only http://atlasalgorithms.kadiemqazzaz.com.

I declared only atlasalgorithms.kadiemqazzaz.com in the server_name so what am I missing?

CodePudding user response:

The rule server_name atlasalgorithms.kadiemqazzaz.com; is actually only matching http://atlasalgorithms.kadiemqazzaz.com.

But there is the only server block in the conf file. This means that this also serves as the default server. Since, http://kadiemqazzaz.com matches none, request is routed to the default server block.

nginx tests only the request’s header field “Host” to determine which server the request should be routed to. If its value does not match any server name, or the request does not contain this header field at all, then nginx will route the request to the default server

Read more about nginx request routing here.

If you need a different routing for http://kadiemqazzaz.com, you should have another server block defining different rules.

  • Related