Relation between server_name in nginx sites-available, /etc/hosts file and A-records

Posted by user2818584 on Server Fault See other posts from Server Fault or by user2818584
Published on 2014-05-26T20:38:12Z Indexed on 2014/05/26 21:31 UTC
Read the original article Hit count: 267

Filed under:
|
|

I have the following two server-blocks in my config-file in sites-available:

server {
       listen 80;
       server_name www.mydomain.be;

       root /usr/share/nginx/html;
       index index.html index.htm;


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

server {
       listen 80;
       server_name sub.mydomain.be;

       root /usr/share/nginx/sub;
       index index.html index.htm;


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

I also created an A-record for both www.domain.be and sub.domain.be with the IP of my server as value.

Yet, when I try to reload my nginx configuration with service nginx reload it fails.

When I remove the second server-block, it reloads as expected.

I know this topic is popular, and that there are loads of such [nginx][subdomain] questions here, but none of them seems to discuss explicitly how the following three things hang together:

  1. virtual hosts or server blocks in nginx (est. server_name matching)
  2. the effect of A-records on how nginx processes requests
  3. the need to add hosts to /etc/hosts

Right now I have the impression that a lack of knowledge of this bigger picture, rather than specific knowledge of nginx configuration prevents me from making this work.

© Server Fault or respective owner

Related posts about nginx

Related posts about subdomain