I need to do SSR on my web app. And since it's an international website, I need to know the domain (hostname) that is requesting the website so I can render in the appropriate language for the user.
Example:
https://www.mydomain.co.uk // WILL RENDER IN ENGLISH
https://www.mydomain.es // WILL RENDER IN SPANISH
Here is the setup
Here is how Firebase rewrites the traffic:
firebase.json
"rewrites": [
{
"source": "**",
"run": {
"serviceId": "my-server-id",
"region": "us-central1"
}
}
]
It's working fine. But I've noticed that all requests reach my Cloud Run server with the following hostname:
req.hostname === "https://my-server-id-on-cloud-run.run.app"
, which is default Cloud Run url for my server service.I've noticed that Firebase Hosting adds my actual domain name to the following header:
x-forwarded-host: "https://www.mydomain.co.uk" OR "https://www.mydomain.es
Is this the correct header to read my domain from? I didn't find any documentation on this subject.
If a user hits my website either using my app's Firebase default urls (web.app OR firebaseapp.com
) or through a connected custom domain, I can count on the fact that the x-forwarded-host
header will always be there, correct?
Could there be a situation where x-forwarded-host
would contain more than one URL? Like it happens in the x-forwarded-for
with IPs?
References:
PS: I've noticed that if I hit my cloud run server directly from the Cloud Run default url .run.app
, the x-forwarded-for
header is not present, which makes sense, given the fact that it is a direct request, and not a rewritten one.
According to this doc,
X-Forwarded-Host
header is useful to determine which Host was originally used.X-Forwarded-Host: <host>
, and the Directive is:<host>
The domain name of the forwarded server.
This means that you can't have moew than 1 value in that header.