Skip to content

Commit

Permalink
fix: prioritise HTTPS & static redirects in nginx configs
Browse files Browse the repository at this point in the history
These redirects were too far down the config and meant they might not even have
triggered correctly if the previous locations & try_files directives matched.
  • Loading branch information
kennethkalmer committed Mar 12, 2024
1 parent 5ddf800 commit d733e80
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions config/nginx.conf.erb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ http {
# Removes trailing slashes everywhere (by redirecting)
rewrite ^/(.*)/$ <%= ENV['SKIP_HTTPS'] == 'true' ? '$scheme' : 'https' %>://$host/$1 permanent;

<% unless ENV['SKIP_HTTPS'] == 'true' %>
# Enforce HTTPS
if ($http_x_forwarded_proto != "https") {
return 301 https://$host$request_uri;
}
<% end %>

# Apply our redirects before rewriting
if ($redirected_url != "none") {
rewrite ^ $redirected_url permanent;
}

# Strip /docs from the requests (we build with --prefix-paths and our files are in public/)
rewrite ^/docs/(.*)$ /$1 last;
rewrite ^/docs$ / last;
Expand Down Expand Up @@ -122,18 +134,6 @@ http {
try_files $uri =404;
}

<% unless ENV['SKIP_HTTPS'] == 'true' %>
# Enforce HTTPS
if ($http_x_forwarded_proto != "https") {
return 301 https://$host$request_uri;
}
<% end %>

# Apply our redirects
if ($redirected_url != "none") {
rewrite ^ $redirected_url permanent;
}

# need this b/c setting $fallback to =404 will try #{root}=404 instead of returning a 404
location @404 {
return 404;
Expand Down

0 comments on commit d733e80

Please sign in to comment.