Skip to content

Instantly share code, notes, and snippets.

@dblazeski
Created October 31, 2017 14:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dblazeski/49a38a44fe378baeec0e61d0c2de41d4 to your computer and use it in GitHub Desktop.
Save dblazeski/49a38a44fe378baeec0e61d0c2de41d4 to your computer and use it in GitHub Desktop.
Nginx - Forces HTTPS redirect on all requests and strip WWW prefix
# 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