2

So I have two domains:

example.com
example.ca

This is a docker container running nginx and I am trying to accomplish http to https redirect and www to non-www redirect.

Docker on the host is bound to port 80 and 443. To be clear traffic works to and from the server without issue.

The issue is the www redirects seem to be ignored.

When a user goes to http://www.example.com I expect them to be redirected to https://example.com

What happens is they are actually redirected to https://www.example.com

So the https redirection works 100% of the time but for some reason nginx fails to perform the redirect without www

It's worth noting we are using CloudFlare to replace our self signed certificates. So maybe CloudFlare is causing the issue?

Here are my redirects.

    server {
        listen 8080;
        listen 8443;
        server_name www.example.com;
        return 301 https://$host$request_uri?$args;
    }

    server {
        listen 8080;
        listen 8443;
        server_name www.example.ca;
        return 301 https://$host$request_uri?$args;
    }

#   Force http to https
    server {
        listen 8080;
        server_name example.com;
        return 301 https://$host$request_uri?$args;
    }
    
    server {
        listen 8080;
        server_name example.ca;
        return 301 https://$host$request_uri?$args;
    }
Tyler Radlick
  • 184
  • 1
  • 6
  • 12

2 Answers2

0

You can redirect the user from www.example.com to https://example.com usign the page rules from cloudflare. Here is an example: enter image description here

Also you could try withing nginx config Try replacing $host with your domain name so you would have

 server {
        listen 8080;
        server_name www.example.com;
        return 301 https://example.com$request_uri?$args;
    }

Maybe this would do the trick

  • Thats awesome, I didn't even think to do it in cloudflare. It does work. I still wonder why my nginx config doesn't work though. If I don't get an answer to that in a few days I will accept your answer. Thank you! – Tyler Radlick Jan 27 '21 at 18:43
  • Updated the answer with the nginx config part that you could try – Claudiu Lazăr Jan 27 '21 at 19:20
0

So I was able to prove this does in fact work.

Turns out there was an error in our deployments that prevented the updated Nginx file from being loaded.

Once that was fixed, this functioned as expected.

Tyler Radlick
  • 184
  • 1
  • 6
  • 12