I have a Nginx server that is hosting both the main domain foo.com
and a subdomain bar.foo.com
for a website. I only have a certificate for my main domain, so I only want HTTP requests for the subdomain. I have configured both server blocks, and all requests work well: http://foo.com
will be redirected to https://foo.com
, and http://bar.foo.com
just stays on HTTP.
However, a problem rises when I tried to connect https://bar.foo.com
, although I have not configured the subdomain server block to listen for 443 or ssl requests, the request is still handled, and after getting a warning from the browser that the certificate has a different host name than the one I am requesting, the page I get is from the main domain foo.com
.
So, apparently, the server
block that was supposed to only listen to HTTPS requests of my main domain, somehow is also listening to HTTPS requests of the subdomain, I don't want this to happen.
Here is my configuration for the main domain:
server {
listen 80;
server_name foo.com www.foo.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name foo.com www.foo.com;
ssl on;
ssl_certificate /etc/nginx/cert/foo.com.pem;
ssl_certificate_key /etc/nginx/cert/foo.com.key;
...
location / {
root /var/www/foo.com/public_html;
index index.html index.htm;
}
}
Here is my configuration for the subdomain:
server {
listen 80;
server_name bar.foo.com www.bar.foo.com;
location / {
root /var/www/bar.foo.com/public_html;
index index.html index.htm;
}
}
And here is a snippet of the Nginx configuration file:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
location / {
}
I have tried deleting foo.com
after listen
in the 443 server block of my main domain, but it doesn't help.
I would like Nginx either not listen to requests such as https://bar.foo.com
or to simply return 404 or 444.
return 444
for unmatched SSL certificate names. This answer contains more details: serverfault.com/q/373929/131019 This relies on your visitors implementing SNI, which you can mostly rely on these days..conf
. Just curious, because it seems to be ignored.