Automatically redirect to HTTPS in nginx
- 23 Mar 2017: Post was created (diff)
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;
server_name example.com;
root /var/www/example.com/public;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
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.