Forhad Ahmed
Forhad Ahmed

Reputation: 1771

nginx https redirect adds www

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?

Upvotes: 1

Views: 239

Answers (1)

TinyTheBrontosaurus
TinyTheBrontosaurus

Reputation: 4840

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;
}

Upvotes: 1

Related Questions