aws-amplify / aws-amplify/amplify-hosting

NextJS i18n cannot redirect based on domain, Amplify should forward "X-Forwarded-Host" header

Open
#2,340 7 comments 10 reactions 0 assignees View on GitHub
feature-request
Dominant language
Dockerfile
Stars
481
Forks
123
PR merge metrics
No merged PRs in 30d

Description

**Is your feature request related to a problem? Please describe.**
We would like to have our website using i18n feature from NextJS based on [Domain Routing](https://nextjs.org/docs/advanced-features/i18n-routing#domain-routing).

The way Amplify was designed, it creates an internal `Cloudfront` to manage DNS and certificates, and forward the request to another `Cloudfront` attached to the branch.

The problem is that, by adding a rewrite rule, no information is propagated about the `Host` and then NextJS can not redirect the request based on the domain. Check the diagram bellow:

![image](https://user-images.githubusercontent.com/17620824/138834993-ad9774ae-34cb-4d77-a1b2-53a16a69182a.png)

If I intercept the request between internal CloudFront and the one rewritten to Serverless these are the only headers propagated:

```
{
"user-agent": "Amazon CloudFront",
"x-amz-cf-id": "...",
"cookie": "...",
"x-forwarded-for": "...",
"via": "...",
"accept-encoding": "gzip",
"pragma": "no-cache",
"sec-ch-ua": "\"Chromium\";v=\"94\", \"Google Chrome\";v=\"94\", \";Not A Brand\";v=\"99\"",
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": "\"Linux\"",
"upgrade-insecure-requests": "1",
"sec-fetch-site": "none",
"sec-fetch-mode": "navigate",
"sec-fetch-user": "?1",
"sec-fetch-dest": "document",
"cloudfront-viewer-country": "DE",
"cache-control": "no-cache",
"x-amplify-isreverseproxy": "true"
}
```

As you can see, no `Host` information, and since it is working as a reverse proxy, `x-forwarded-host` should be added in order to make it consistent and possible to integrate it better into the other layers.

**Describe the solution you'd like**
Internal Cloudfront created by Amplify to manage DNS and Certificates should forward `x-forwarded-host` header, so nextjs can forward the request to the correct domain according to the request origin.

![image](https://user-images.githubusercontent.com/17620824/138837162-0743fa57-01b7-44b9-83b4-f201b186b063.png)

Contributor guide

Open the contributing guide

Research direction

Start by tracing the internal CloudFront rewrite path described in the issue and checking how request headers are forwarded to the branch CloudFront. Reproduce the Next.js domain-routing request and verify the original host is available through X-Forwarded-Host; done means the correct domain can be selected without relying on the missing Host header.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, next.js
Domain
cloud, infrastructure, web-dev
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.