2

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.

hitchhiker
  • 1,099
  • 5
  • 19
  • 44
  • Could you provide some more information about your Elastic Beanstalk configuration? Is this a single instance or load-balanced environment? Where did you configure a DNS record for example.com to point at myid.elasticbeanstalk.com? Could you provide some more general info for a better overview of your scenario? – Martin Löper Sep 16 '20 at 12:36
  • 1
    I added the answers to your questions in the OP. – hitchhiker Sep 16 '20 at 14:19
  • example.com is a CNAME-Record or an alias record pointing at myid.elasticbeanstalk.com? "appears in serverside requests only" means that you are logging the host header in the docker container? As you are writing about SSL... Where do you terminate the SSL connection? Do you currently use SSL or do you plan to use it and are concerned about the wrong host headers being logged? – Martin Löper Sep 17 '20 at 00:12
  • I added the answers to your questions in the OP. Regarding the SSL currently there're no issues but I just want to understand why some requests have different hosts. – hitchhiker Sep 17 '20 at 08:51
  • 1
    Thanks! What I basically try to understand here is how many services / servers are between your client and the nextjs backend in order to figure out which component is setting the host header you are logging. – Martin Löper Sep 17 '20 at 18:40
  • try request.headers.host instead – thatcoder Sep 22 '20 at 06:40

1 Answers1

1

To set custom HTTP headers you can use the headers key in next.config.js:

module.exports = {
  async headers() {
    return [
      {
        source: '/about',
        headers: [
          {
            key: 'x-custom-header',
            value: 'my custom header value',
          },
          {
            key: 'x-another-custom-header',
            value: 'my other custom header value',
          },
        ],
      },
    ]
  },
}

headers is an async function that expects an array to be returned holding objects with source and headers properties:

source is the incoming request path pattern.

headers is an array of header objects with the key and value properties.

pycoder
  • 11
  • 3