0

I have a few websites, and when I type in a subdomain that does not exist, Nginx redirects to the first available website in /etc/nginx/sites-enabled. My config is as follows for the websites:


server { 
  listen 80; 
  listen [::]:80; 
  server_name *.example.com; 
  return 301 https://$host$request_uri; 
} 

server { 
   listen 443 ssl; 
   server_name *.example.com; 
   
   ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; 
   ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; 
   include /etc/letsencrypt/options-ssl-nginx.conf; 
   ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; 
   
   root /var/www/app_name/build;
   
   index index.html index.htm;
   
   location / {
      try_files $uri /index.html =404;
   }
}
  • 1
    This is an expected behavior. One of the server block will always act as a **default** one. If you didn't specify such a block using `default_server` flag on a `listen` directive, it will be the first one in your configuration. For additional details read [1](https://stackoverflow.com/questions/69824838/block-access-with-https-ipaddress-on-nginx/69825652#69825652), [2](https://stackoverflow.com/questions/60362642/nginx-doesnt-listen-on-port-80-twice/60362700#60362700). – Ivan Shatsky Feb 25 '22 at 15:18

1 Answers1

0

Perfect, Ivan Shatsky's comment about default_server fixed it. However I had an error duplicate default_server error, which I fixed with the following:

cd /etc/nginx/sites-enabled
unlink default