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:
- virtual hosts or server blocks in nginx (est. server_name matching)
- the effect of A-records on how nginx processes requests
- 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.
Update:
Tracked down the problem
Initially I got a fail without any explanation.
Using
sudo nginx -c /etc/nginx/nginx.conf -t
I got this message
nginx: [emerg] could not build the server_names_hash, you should increase server_names_hash_bucket_size: 32
after uncommenting
server_names_hash_bucket_size 64;
the problem was resolved.