1
votes

I have two server blocks in my nginx configuration:

server {
    listen 80;
    server_name domain.com;
    return 301 https://domain.com$request_uri;
}

server {
    listen 443 ssl;
    server_name domain.com;
    <ssl stuff>
    <root directory>
}

I'm basically redirecting all HTTP traffic to HTTPS using the first server block. I'm hardcoding the redirect domain name because I want to explicitly avoid redirecting to htps://www.domain... - I want https://domain...

When I request non-www HTTP domain, nginx correctly redirects to non-www HTTPS domain

However, when I request the www HTTP domain, nginx is not redirecting to the non-www HTTPS domain. Somehow, it adds a www to the HTTPS redirect even when I explicitly specified not to.

WHY?

1
Justo be clear, http:// www.domain.com goes to https:// www.domain.com? Or to http:// www.domain.com?TinyTheBrontosaurus
http:// www.domain.com goes to https:// www.domain.com and I dont want that. I want it to go to https:// domain.com which is the hardcoded redirect, but nginx is not honoring thatForhad Ahmed

1 Answers

1
votes

Just looking at this without testing, I would say it's not picking up www.domain.com because that's not specified in the config. I belive you need a separate specification for www, or use a regex.

server {
    listen 80;
    server_name domain.com
                www.domain.com;
    return 301 https://domain.com$request_uri;
}