0

This is my current configuration:

server {
    if ($host = www.mywebsite.com) {
        return 301 https://mywebsite.com$request_uri;
    } # managed by Certbot


    if ($host = mywebsite.com) {
        return 301 https://mywebsite.com$request_uri;
    } # managed by Certbot

    listen 80;
    server_name mywebsite.com www.mywebsite.com;
    return 404; # managed by Certbot
}

server {
    server_name mywebsite.com;
    root /var/www/html/mywebsite.com/public;

    location / {
        try_files $uri $uri/index.html @sinatra;
    }

    location @sinatra {
        proxy_pass http://127.0.0.1:4567;
    }

    listen [::]:443 ssl http2 ipv6only=on;
    listen 443 ssl http2; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/mywebsite.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/mywebsite.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

What I want is to have everything... www-version or http-version of any URL redirected to the non-www HTTPS url.

Right now, everything works with a couple of catches:

  • https://mywebsite.com works fine
  • http://mywebsite.com redirects to https://mywebsite.com (perfect too)
  • https://www.mywebsite.com redirects to https://mywebsite.com// (double trailing slash!)
  • http://www.mywebsite.com redirects to https://mywebsite.com// (double trailing slash, again!)

How do I go about fixing this?

Dr.Kameleon
  • 101
  • 2
  • 1
    Does `www.mywebsite.com` point to this server? The above configuration does not redirect from `https://www.mywebsite.com` to `https://mywebsite.com`. – Richard Smith Jul 14 '21 at 10:29
  • 1
    Yes, there is definitely a lot of stuff missing that should be here. Please post the output of `nginx -T`. – Michael Hampton Jul 14 '21 at 11:49

0 Answers0