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

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

Nginx > Nginx Redirect

Comments

Leave a Reply

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