How to add custom request header to Rewrites in Next.js

223 views Asked by At

I have a reverse proxy setup using the API router in Next.js. Currently, my code for the proxy configuration looks like this:

  async rewrites() {
    return [
      {
        source: '/api/:path*',
        destination: 'https://my-backend-host/:path*' // Proxy to Backend
      }
    ]
  }

Now, I want to enhance it by adding a runtime environment variable to the request headers before sending them to the backend. This would be similar to the functionality of the proxy_add_header directive in nginx. I would like to achieve something like this:

  async rewrites() {
    return [
      {
        source: '/api/:path*',
        destination: 'https://my-backend-host/:path*', // Proxy to Backend
        proxySetHeader: {
          'my-env-var': process.env['my-env-var']
        }
      }
    ]
  }

However, I couldn't find any information about this specific feature in the Next.js documentation (https://nextjs.org/docs/pages/api-reference/next-config-js/rewrites)

0

There are 0 answers