Multiple flavours of nginx http to https redirects
Redirects in nginx are really powerful, here are some cool and common examples.
all domains http > https and remove www
Redirect all http traffic which has no other better matching server_name defined from www.domain.tld and domain.tld to https://domain.tld
# redirect http://www to https non www
# global and maybe not the best in all setups
server {
listen 80;
server_name ~^(www\.)(?<domain>.+)$;
return 301 https://$domain$request_uri;
}
http > https
Redirect all of the incoming http requests to https for selected domains
server {
listen 80;
server_name domain.tld www.domain.tld;
return 301 https://$host$request_uri;
}
http > https with exceptions
Redirect all or a part of the incoming http requests to https, you can use this the other way round to redirect a small part of the traffic to https and localion /
to the normal backend.
server {
listen 80;
server_name domain.tld *.domain.tld;
# Excluded location from https redirect
# I use this for lets encrypt validation
location ~ acme-challenge {
root /etc/letsencrypt/temp;
}
location / {
return 301 https://$host$request_uri;
}
}
More will be added ;-)
If you have a redirect you just can’t get to work, leave a comment so I can give it a try.