3

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)

1
  • I have the same issue and cannot find any solution. It's a bit surprising this is not solved by NextJS. Commented Aug 27, 2024 at 17:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.