Skip to content
This repository has been archived by the owner on Nov 1, 2023. It is now read-only.

Commit

Permalink
Merge pull request #65 from heroku/https_only_proxies
Browse files Browse the repository at this point in the history
https_only happens over proxies
  • Loading branch information
hone authored Mar 27, 2017
2 parents 0aebb10 + e31a532 commit 3428d92
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ when accessing `/foo`, `X-Foo` will have the value `"foo"` and `X-Bar` will not

### Route Ordering

* HTTPS redirect
* Root Files
* Clean URLs
* Proxies
Expand Down
14 changes: 1 addition & 13 deletions scripts/config/templates/nginx.conf.erb
Original file line number Diff line number Diff line change
Expand Up @@ -80,26 +80,14 @@ http {
}
<% end %>

<% proxies.each do |location, hash| %>
set $<%= hash['name'] %> <%= hash['host'] %>;
location <%= location %> {
rewrite ^<%= location %>/?(.*) <%= hash['path'] %>/$1 break;
proxy_pass $<%= hash['name'] %>;
proxy_ssl_server_name on;
# handle Location rewrites from the proxy properly
<% %w(http https).each do |scheme| %>
proxy_redirect <%= hash["redirect_#{scheme}"] %> <%= location %>;
<% end %>
}
<% end %>

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

# fallback proxy named match
<% proxies.each do |location, hash| %>
set $<%= hash['name'] %> <%= hash['host'] %>;
location @<%= location %> {
rewrite ^<%= location %>/?(.*)$ <%= hash['path'] %>/$1 break;
# can reuse variable set above
Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/redirects_https_only/public_html/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello world
9 changes: 9 additions & 0 deletions spec/fixtures/redirects_https_only/static.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"redirects": {
"/old/gone": {
"url": "/",
"status": 302
}
},
"https_only": true
}
39 changes: 39 additions & 0 deletions spec/simple_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,16 @@
expect(response["location"]).to eq("http://#{RouterRunner::HOST_IP}/interpolation.html")
end
end

context "https_only" do
let(:name) { "redirects_https_only" }

it "should redirect to https first" do
response = app.get("/old/gone")
expect(response.code).to eq("301")
expect(response["location"]).to eq("https://#{RouterRunner::HOST_IP}/old/gone")
end
end
end

describe "https only" do
Expand Down Expand Up @@ -259,6 +269,35 @@
end
end

context "https_only" do
let(:setup_static_json) do
Proc.new do |path|
File.open(static_json_path, "w") do |file|
file.puts <<STATIC_JSON
{
"proxies": {
"/api/": {
"origin": "http://#{@proxy_ip_address}#{path}"
}
},
"https_only": true
}
STATIC_JSON
end
end
end

before do
setup_static_json.call("/")
end

it "should not redirect direct to the proxy" do
response = app.get("/api/bar")
expect(response.code).to eq("301")
expect(response["Location"]).to eq("https://#{RouterRunner::HOST_IP}/api/bar")
end
end

context "env var substitution" do
let(:proxy) do
<<CONFIG_RU
Expand Down

0 comments on commit 3428d92

Please sign in to comment.