skip to Main Content

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

2

Answers


  1. Chosen as BEST ANSWER

    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.


  2. You can redirect the user from http://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

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