Automatically redirect to HTTPS in nginx

Revision history
Tags: nginx SSL

The if statement in the below site configuration will redirect all HTTP requests to its HTTPS equivalent.

server {
  listen 80;
  listen [::]:80;
  listen 443 ssl;
  listen [::]:443 ssl;
  root /var/www/;

  ssl_certificate     /etc/letsencrypt/live/;
  ssl_certificate_key /etc/letsencrypt/live/;

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

This can be done in a more modular approach using an include directive, but you probably get the gist of it.


If you have any comments or feedback, please send me an e-mail. (stig at stigok dotcom).

Did you find any typos, incorrect information, or have something to add? Then please propose a change to this post.

Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.