I host my Next.js app on Amazon Web Services in Elastic Beanstalk inside a docker container. I recently noticed by printing req.host
from Express.js that whenever a request is sent to /_next*
route the host is myid.elasticebeanstalk.com
. In all other requests (e.g. for the actual page or request to API server) the host is example.com
both client-side and server-side (supposing that my app is hosted at that address).
I'm wondering why is it that way? How can I set the host header even to /_next*
routes to be example.com
? I suspect that sometime this could result in SSL error because the host will not be correct.
The elastic beanstalk config is single instance. The DNS records are configures in AWS Route 53. The host header with myid.elasticebeanstalk.com
appears in serverside requests only. example.com
is a CNAME, not an alias. The logs are made inside the docker container. In general the SSL certificate is supplied by AWS in Cloudfront I think.
EDIT: it looks like the hostname should be set in the host operating system in Elastic Beanstalk which should be Linux. This AWS doc explains this.
To set custom HTTP headers you can use the headers key in next.config.js:
headers
is an async function that expects an array to be returned holding objects withsource
andheaders
properties:source
is the incoming request path pattern.headers
is an array of header objects with thekey
andvalue
properties.