Skip to content

Instantly share code, notes, and snippets.

@jweyrich
Last active February 9, 2017 13:35
Show Gist options
  • Save jweyrich/7c94b8fe30eba8205981f2b359027bb4 to your computer and use it in GitHub Desktop.
Save jweyrich/7c94b8fe30eba8205981f2b359027bb4 to your computer and use it in GitHub Desktop.
Nginx - Redirect some traffic from HTTP to HTTPS
set $needs_https_redir 1;
# Do not force HTTPS if the client is directly connecting to this server via
# HTTPS.
if ($scheme = 'https') {
set $needs_https_redir 0;
}
# Do not force HTTPS if the client is indireclty connecting to this server via
# HTTPS and the gateway tells us that through the X-Forwarded-Proto header.
if ($http_x_forwarded_proto = 'https') {
set $needs_https_redir 0;
}
# Do not force HTTPS if the client is requesting /foo/bar
if ($request_uri ~ ^/foo/bar$) {
set $needs_https_redir 0;
}
if ($needs_https_redir = 1) {
#rewrite ^ https://$host$uri permanent;
return 301 https://$host$uri;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment