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 #61 from heroku/proxy_redirect
Browse files Browse the repository at this point in the history
handle proxy redirects even if the scheme doesn't match
  • Loading branch information
hone authored Mar 15, 2017
2 parents a2e3a02 + 7ff0ea2 commit fb0a495
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
3 changes: 3 additions & 0 deletions scripts/config/lib/nginx_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ def initialize(json_file)
json["proxies"][loc]["path"] = uri.path
uri.path = ""
json["proxies"][loc]["host"] = uri.to_s
redirect_scheme = uri.scheme == "https" ? "http" : "https"
json["proxies"][loc]["redirect"] = uri.dup.tap {|u| u.scheme = redirect_scheme }.to_s
json["proxies"][loc]["redirect"] += "/" if !uri.to_s.end_with?("/")
end

json["clean_urls"] ||= DEFAULT[:clean_urls]
Expand Down
4 changes: 4 additions & 0 deletions scripts/config/templates/nginx.conf.erb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ http {
location <%= location %> {
proxy_pass <%= hash['origin'] %>;
proxy_ssl_server_name on;
proxy_redirect default;
proxy_redirect <%= hash["redirect"] %> <%= location %>;
}
<% end %>

Expand All @@ -95,6 +97,8 @@ http {
rewrite ^<%= location %>(.*)$ <%= hash['path'] %>/$1 break;
proxy_pass <%= hash['host'] %>;
proxy_ssl_server_name on;
proxy_redirect default;
proxy_redirect <%= hash["redirect"] %> <%= location %>;
}
<% end %>

Expand Down
27 changes: 25 additions & 2 deletions spec/simple_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@
include PathHelper

let(:name) { "proxies" }
let(:proxy_scheme) { "http" }
let(:static_json_path) { fixtures_path("proxies/static.json") }
let(:proxy) do
<<PROXY
Expand All @@ -393,6 +394,16 @@
get "/foo/baz/" do
"baz"
end
get "/foo/http_redirect/" do
uri = URI("http://\#{request.host}/foo/redirect")
redirect URI(uri), 307
end
get "/foo/https_redirect/" do
uri = URI("https://\#{request.host}/foo/redirect")
redirect URI(uri), 307
end
PROXY
end
let(:setup_static_json) do
Expand All @@ -402,7 +413,7 @@
{
"proxies": {
"/api/": {
"origin": "http://#{@proxy_ip_address}#{path}"
"origin": "#{proxy_scheme}://#{@proxy_ip_address}#{path}"
}
},
"headers": {
Expand All @@ -419,7 +430,7 @@

before do
@proxy_ip_address = app.proxy.ip_address
setup_static_json.call("/foo/")
setup_static_json.call("/foo")
end

after do
Expand All @@ -439,6 +450,18 @@
expect(response["X-Header"]).to be_nil
end
end

it "should hanadle redirects regardless of scheme" do
app.run do
response = app.get("/api/http_redirect/")
expect(response.code).to eq("307")
expect(response["Location"]).not_to include(@proxy_ip_address)

response = app.get("/api/https_redirect/")
expect(response.code).to eq("307")
expect(response["Location"]).not_to include(@proxy_ip_address)
end
end
end
end

Expand Down

0 comments on commit fb0a495

Please sign in to comment.