Nginx Redirect HTTP to HTTPS

To force SSL (HTTPS) on a website hosted on an Nginx web server, edit server entry for the website, add

if ($server_port !~ 443){
    rewrite ^(/.*)$ https://$host$1 permanent;
}

Method 2

if ($scheme = http) {
    return 301 https://$server_name$request_uri;
}

Method 3

Advertisement

Create a server entry for port 80, that only do the redirect to HTTPS like the following

server {
    listen 80;
    server_name  DOMAIN_NAME www.DOMAIN_NAME;
    return       301 https://DOMAIN_NAME$request_uri;
}

Restart Nginx

systemctl restart nginx
Add a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Advertisement