AWS Amplify throws 502 Error when using a reverse proxy via EC2

0

I am trying to serve custom domains with SSL using Caddy for a Nextjs app hosted using AWS Amplify. So the custom domains do a reverse proxy to the Nextjs site. The caddy server itself is hosted on AWS EC2

(Here brand.example.com is hosted on AWS Amplify)

The SSL certificates were issued successfully but AWS Amplify shows an error in the browser with 502 error:

Enter image description here

And the following is the configuration of the Caddyfile (reverse proxy file)

{
        debug
        order rewrite after forward_auth
        admin off
        on_demand_tls {
                ask {env.DOMAIN_SERVICE_ENDPOINT}
        }

        storage_clean_interval 90d

        log
}

:80 {
        respond /health "Im healthy" 200

        log
}

:443 {
        tls {env.EMAIL_ADDRESS} {
                on_demand
        }

        forward_auth {env.MIDDLEWARE_ENDPOINT} {
                uri /?sourceHost={host}&extraUri={uri}
                header_up Host {upstream_hostport}
                copy_headers Pathroute Domainroute Domainhttpsroute Shouldredirect

        }

        rewrite * {header.Pathroute}

        reverse_proxy {header.Domainroute}:443 {
                header_up Host {http.reverse_proxy.upstream.host}

                header_down Cache-Control "max-age=5184000"
                header_down Access-Control-Allow-Origin {host}

                header_up Access-Control-Allow-Origin {host}


                health_timeout 5s

                transport http {
                        tls
                }
        }

        log
}

And here is the JS file for the forward_auth endpoint:

exports.handler = async (event) => {
    let statusCode;

    let pathRoute = '';
  
    if (event.queryStringParameters && event.queryStringParameters.hasOwnProperty('sourceHost')) {
     
     
      const extraUri = event.queryStringParameters.extraUri;
      if(extraUri==='/' || !extraUri || extraUri===''){
        pathRoute = '/person1';
      }
      else{
        pathRoute = extraUri
      }

      statusCode = 200;
    } else {
      statusCode = 400;
    }

  
    return {
        headers: {
          'Pathroute': pathRoute,
          'Domainroute': 'brand.example.com',
          'Domainhttpsroute': 'https://brand.example.com',
        },
        statusCode: 200,
      };
  };

So the proxy request works fine in Postman but shows the 502 - forbidden error when opened in browser. Any solution available for this?

No Answers

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions