Redirect non www to www on https

August 2, 2019 192 views
Nginx DigitalOcean Let's Encrypt Ubuntu 18.04

I am trying to redirect https:// to https://www. Here is the block of code I am using:

server {

       # root /home/user/www;
       index index.html index.htm index.nginx-debian.html;

       server_name ookma-kyi.tech www.ookma-kyi.tech;

       location / {
          root /home/user/www;
          try_files $uri $uri/ =404;
       }

       location /demo {
           root /home/user/app;
           rewrite /demo/(.*) /$1  break;
           proxy_pass http://localhost:3000;
           proxy_http_version 1.1;
           proxy_set_header Upgrade $http_upgrade;
           proxy_set_header Connection 'upgrade';
           proxy_set_header Host $host;
           proxy_cache_bypass $http_upgrade;
       }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/ookma-kyi.tech/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/ookma-kyi.tech/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


}
server {
    if ($host = www.ookma-kyi.tech) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = ookma-kyi.tech) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


       listen 80;
       listen [::]:80;

    server_name ookma-kyi.tech www.ookma-kyi.tech;
    return 404; # managed by Certbot

}

I tried adding a server block at the top with a 301 redirect like so:

server {
    listen 443;
    server_name ookma-kyi.tech
    ssl_certificate /etc/letsencrypt/live/ookma-kyi.tech/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/ookma-kyi.tech/privkey.pem; # managed by Certbot
    return 301 https://www.ookma-kyi.tech$request_uri;
}

server {

       # root /home/user/www;
       index index.html index.htm index.nginx-debian.html;

       server_name ookma-kyi.tech www.ookma-kyi.tech;

       location / {
          root /home/user/www;
          try_files $uri $uri/ =404;
       }

       location /demo {
           root /home/user/app;
           rewrite /demo/(.*) /$1  break;
           proxy_pass http://localhost:3000;
           proxy_http_version 1.1;
           proxy_set_header Upgrade $http_upgrade;
           proxy_set_header Connection 'upgrade';
           proxy_set_header Host $host;
           proxy_cache_bypass $http_upgrade;
       }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/ookma-kyi.tech/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/ookma-kyi.tech/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


}
server {
    if ($host = www.ookma-kyi.tech) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = ookma-kyi.tech) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


       listen 80;
       listen [::]:80;

    server_name ookma-kyi.tech www.ookma-kyi.tech;
    return 404; # managed by Certbot

}

All that did was show the Nginx welcome page. Any ideas?

1 Answer

Hello,

In order to redirect non https:// traffic to https://www you can use:

 ### redirect HTTPS n
server {
    listen 443 ssl;
    server_name ookma-kyi.tech;
    ssl_certificate /etc/letsencrypt/live/ookma-kyi.tech/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/ookma-kyi.tech/privkey.pem;
    return 301 https://www.ookma-kyi.tech$request_uri;
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

Redirect all HTTP traffic to HTTPS

# Redirect www.ookma-kyi.tech:80 to https://www.ookma-kyi.tech:443
server {
    listen      80;
    access_log  off;
    error_log   off;
    server_name www.ookma-kyi.tech;
    return         301 https://$server_name$request_uri;
}
# # Redirect http://ookma-kyi.tech:80 to https://ookma-kyi.tech:443
server {
    listen      80;
    access_log  off;
    error_log   off;
    server_name theos.in;
    return         301 https://$server_name$request_uri;
}

Check the nginx configuration file for any errors:

nginx -T

If there are any errors, you can try to fix them and then check the syntax of the file again.

systemctl restart nginx

Let me know how it goes.

Alex

Have another answer? Share your knowledge.