skip to Main Content

I want to handle to handle sub.domain.com and domain.com with different server blocks.
So I created the following config:

server {
  listen 443 ssl;

  server_name sub.domain.com;

  location / {
    ...
  }
}

server {
  listen 443 ssl;

  server_name domain.com;

  location / {
    ...
  }
}

Requests to sub.domain.com get correctly handled tby the first server block. However requests to domain.com also get handled by the first one.
Why?

From what I understand from the docs, requests to domain.com shouldn’t be matched by sub.domain.com?

2

Answers


  1. Probably won’t apply to all cases but for me I was using a .dev domain (which requires https). I hadn’t set up the cert yet and I guess nginx was just using the first https enabled site it could find even though the server_name was not a match.

    Once I enabled https using certbot and enabled the 443 redirect it started working.

    Login or Signup to reply.
  2. I had a similar problem and this question was quite high up in the Google results.

    In my case I had a development subdomain running http only, and the main domain running https. One of the redirects on the main domain caught the subdomain request and forwarded it to the main site.

    The fix was to specify http:// in the web browser or to enable https for the development domain.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search