-
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
so, did you manage to solve the problem? I did but I didn't use load ballancer or anything else |
Beta Was this translation helpful? Give feedback.
-
Found the issue. Doing some investigation on this tonight, I found the reason why that's happening: On Kamal v1 (with Traefik), the following headers are passed to Rails:
On Kamal v2 (with Kamal-proxy), with the exact same application and environment, the following headers are passed to Rails:
Notice that the My best guess is that Traefik is ignoring the forwarded host due to security concerns and need to configured to trust is in order to pass it along (at least that's my understanding based on a quick look on their documentation), whereas Kamal Proxy is simply passing them along as is. So this was something that was affecting my previously, but I wasn't impacted due to the lack of trust from Traefik. My solution for this was to improve my already existing CloudFlare middleware to ignore the X-Forwarded-For header: 🗒️ # frozen_string_literal: true
class CloudflareIp
def initialize(app)
@app = app
end
def call(env)
if env["HTTP_CF_CONNECTING_IP"]
# persist headers before we change them
env["HTTP_REMOTE_ADDR_BEFORE_CF"] = env["REMOTE_ADDR"]
env["HTTP_X_FORWARDED_FOR_BEFORE_CF"] = env["HTTP_X_FORWARDED_FOR"]
# load real values passed along by CloudFlare
env["REMOTE_ADDR"] = env["HTTP_CF_CONNECTING_IP"]
env["HTTP_X_FORWARDED_FOR"] = env["HTTP_CF_CONNECTING_IP"]
end
# CloudFlare Load Balancer sends an internal host on "X-Forwarded-Host", so we need to overwrite with the "Host" header
if env["HTTP_X_FORWARDED_HOST"]
env["HTTP_X_FORWARDED_HOST_BEFORE_OVERWRITE"] = env["HTTP_X_FORWARDED_HOST"]
env["HTTP_X_FORWARDED_HOST"] = env["HTTP_HOST"]
end
@app.call(env)
end
end
Rails.application.config.middleware.insert_before(0, CloudflareIp) unless Rails.env.local? The new section is the |
Beta Was this translation helpful? Give feedback.
Found the issue. Doing some investigation on this tonight, I found the reason why that's happening:
On Kamal v1 (with Traefik), the following headers are passed to Rails:
SERVER_NAME: example.org
HTTP_HOST: example.org
HTTP_X_FORWARDED_HOST: example.org
On Kamal v2 (with Kamal-proxy), with the exact same application and environment, the following headers are passed to Rails:
SERVER_NAME: example.org
HTTP_HOST: example.org
HTTP_X_FORWARDED_HOST: pi1.example.org
⭐️Notice that the
HTTP_X_FORWARDED_HOST
header is different when using Kamal v2, and that is causing the incorrect redirection. Bear in mind that the env is exactly the same, the only moving part is Kamal v1 vs v2.My best gues…