Quentin Roy
Quentin Roy

Reputation: 7887

nginx config catches domains not specified in server_name

I have two configs enabled in my nginx sites-enabled folder.

The first one (my-domain.fr.conf) looks like this:

server {
    listen 443 ssl http2;
    server_name my-domain.fr;
    index index.html;
    location / {
        root /www/my-domain.fr;
    }
    include ssl_certif.conf;
}

# HTTP redirect
server {
    listen 80 default_server;
    server_name my-domain.fr;

    location / {
        return 301 https://my-domain.fr$request_uri;
    }
}

The second one (sub.my-domain.fr.conf) looks like this:

server {
    location / {
        proxy_pass http://127.0.0.1:8080;
    }
    include ssl_certif.conf;
    server_name sub.my-domain.fr;
    listen [::]:443 ssl;
}
server {
    if ($host = sub.my-domain.fr) {
        return 301 https://$host$request_uri;
    }

    server_name sub.my-domain.fr;

    listen [::]:80;
    return 404;
}

I would expect the last one to only catch requests to sub.my-domain.fr subdomains, but instead it catches anything (I have wildcards subdomains set up on my DNS), and even masks my-domain.fr.

How can I make sure it only catches sub.my-domain.fr requests?

Upvotes: 2

Views: 348

Answers (1)

Quentin Roy
Quentin Roy

Reputation: 7887

I found the reason.

sub.my-domain.fr supports ipv6 (listen [::]:443 ssl;). my-domain.fr doesn't. I suppose my connection is using ipv6 when it can, and in this case, sub.my-domain.fr is the only match.

Adding ipv6 support (listen 443 ssl => listen [::]:443 ssl;, and listen 80; => listen [::]:80;) in all server entries fix it.

Upvotes: 1

Related Questions