axsuul / rails-reverse-proxy

A reverse proxy for Ruby on Rails
MIT License
237 stars 59 forks source link

feature request: default headers to match proxy url for host and request headers for accept #59

Open richardwan opened 2 years ago

richardwan commented 2 years ago

in order to successfully proxy, i had to do a workaround

    headers = {
      'Host' => host,
      'Accept' => request.headers.to_h['HTTP_ACCEPT'] 
    }
    reverse_proxy "https://#{host}", headers: headers

instead of reverse_proxy "https://#{host}"

is there any appetite for either making the workaround the default behavior in the gem or an option that the gem recognizes to do this? If so, I can make a fork and submit a PR.

pduey commented 1 year ago

The 'Host' header and the "host" arg (1st param in call to reverse_proxy) are two different things. But I agree, this gem should add it if the caller did not set it. E.g., in client.rb:

      # Define headers
      target_request_headers = extract_http_request_headers(source_request.env).merge(options[:headers])
+     target_request_headers["Host"] ||= URI.parse(uri).host

Scenario: When running 2 "pure" rails (i.e., Puma or whatever web server you bundle in development) apps, you can reverse proxy from one to the other. But when you add another web server like nginx (typical in a production-like environment), it does not work without the "Host" header.

If the repo owner agrees, I can create a PR.

axsuul commented 1 year ago

Sure that sounds great, PR welcome 😊 Also please add a comment with this context