Nginx redirect to HTTPS

Redirect all traffic to https in nginx

Overview

If you have enabled https, have a public IP and want to redirect all traffic from http to https you can change the “/ location {}” section in nginx.conf to the following.

location / {
  rewrite ^ https://$host$request_uri? permanent;
}

If your webserver is located behind a proxy, loadbalancer or WAF (that sends x-forwarded headers) you can simply use the below snippet instead.

if ($http_x_forwarded_proto != "https") {
  rewrite ^ https://$host$request_uri? permanent;
}
Last modified February 26, 2026: k8s-1.33-changelog (#253) (ea3be5e)