C4f1151075b447779af31e99d6cf70e2c6eb47ac
By:
newbie

Nginx redirect error when http to https including www

May 15, 2017 51 views
Nginx Ubuntu 16.04

Hello,
i need all http to redirect to https. also non-www to be redirect to www version as well. so i tried with this single redirect rule
return 301 https://www.$server_name$request_uri;

And this is my server block.

server {
        listen 80 default_server;
        listen [::]:80 default_server;
        server_name mysite.com www.mysite.com;
        return 301 https://www.$server_name$request_uri;
}

server {
        listen 80;
        listen [::]:80;
        server_name forum.mysite.com;
        return 301 https://$server_name$request_uri;
}

server {
        listen 443 ssl http2 default_server;
        listen [::]:443 ssl http2 default_server;

        root /var/www/html;
        index index.php index.html index.htm index.nginx-debian.html;
        server_name mysite.com www.mysite.com forum.mysite.com;

all domain work perfectly except when i type https://mysite.com it redirect to address https://www.mysite.com/wp-signup.php?new=mysite.com

note, this happen only when i typed with https.

anyone has any idea why this is happening? i found out this today when i went to my both domains to google search console. when i typed https://mysite.com i saw this issue.

@jtittle @hansen

3 Answers

@newbie

The issue is the first server block :-).

server {
        listen 80 default_server;
        listen [::]:80 default_server;
        server_name mysite.com www.mysite.com;
        return 301 https://www.$server_name$request_uri;
}

The return is set to redirect to the www variant by default. You'd need to change this:

return 301 https://www.$server_name$request_uri;

to

return 301 https://$server_name$request_uri;

i.e. remove the www.

@newbie

My apologies, I originally thought this was an issue with the redirect -- I misread.

For WordPress Multi-Site using sub-domains, you may need to use a Wild Card, that way you're not having to add each sub-domain to the server block manually.

To do this, you'd need to setup an A entry that points * to the same IP as your main domain. You'd then add the Wild Card to server_name, like so:

server_name mysite.com www.mysite.com *.mysite.com;

You would then remove the second server block for forum.mysite.com and remove that site from the third server block.

So what you should end up with is something that looks like:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    server_name mysite.com www.mysite.com *.mysite.com;

    return 301 https://www.$server_name$request_uri;
}

server {
    listen 443 ssl http2 default_server;
    listen [::]:443 ssl http2 default_server;

    server_name mysite.com www.mysite.com *.mysite.com;

    root /var/www/html;
    index index.php index.html index.htm index.nginx-debian.html;

    ...
    ...
    ...
}

The first server block handles all your main HTTP requests and redirects them to the second, which handles your HTTPS requests.

With the above, if you enter in:

site01.mysite.com
site02.mysite.com
site03.mysite.com
forum.mysite.com

...once the DNS resolves, they should all be redirected to the main site. Of course, you need to make sure that your SSL certificate is setup to handle each sub-domain as Let's Encrypt doesn't handle Wild Cards right now (and may not ever).

That should help simplify things though.

@jtittle
bro i think i find the actual cause of this error.

you see before install wordpress, i added that 301 redirect rule to my server block. so when i installed wordpress, the site address became www.mysite.com instead of mysite.com

so whenever i type https://mysite.com it actually fails to detect any site saying there is no mysitecom.mysite.com. i actually didnt read all the lines properly that time.

i also realize, this might give me other errors as my dns configuration is not as per my site setup as www was added as cname.

so just to be careful and avoid future issues, i uninstalled my multisite and installed again. this time my main site address is https://mysite.com

now everything works fine. all the links are working http or https, www or non-www
but now my site becomes non-www one. whichever i choose all redirects to https://mysite.com

though it removed the error, but i would like to use the www one.

Have another answer? Share your knowledge.