Created
October 31, 2017 14:34
-
-
Save dblazeski/49a38a44fe378baeec0e61d0c2de41d4 to your computer and use it in GitHub Desktop.
Nginx - Forces HTTPS redirect on all requests and strip WWW prefix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Forces HTTPS redirect on all requests | |
server { | |
listen 80 default_server; | |
listen [::]:80 default_server; | |
server_name _; | |
return 301 https://$host$request_uri; | |
} | |
# Strip www. from all requests (including subdomains) | |
# and force HTTPS | |
server { | |
listen 80 default_server; | |
listen [::]:80 default_server; | |
server_name _; | |
# If the request has www prefix, strip it | |
if ($host ~* ^www\.(.*)) { | |
set $host_without_www $1; | |
rewrite ^(.*) https://$host_without_www$1 permanent; | |
} | |
return 301 https://$host$request_uri; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment