I`ve been build worker to redirect to aws with different port

Hi all,
I`m new here.

So, Ihe been build a worker to redirect url to another hostname with port 81, mas it doesnt work.

I dont know if I have to configure or Ive release something on Cloudflare.
Follow my script:

export default {
  async fetch(request, env, ctx) {
    try {
      // Obter a URL da solicitação
      const url = new URL(request.url)

      // Extrair o caminho após "/nuvemshop" na URL
      const newPath = url.pathname.toLowerCase().split('/nuvemshop').slice(1).join('/nuvemshop');
      console.log(newPath)

      // Construir a URL de redirecionamento com o novo caminho
      const redirectURL = `http://linkawscustom.compute.amazonaws.com:81/v1/application/nuvemshop${newPath}`
      console.log(redirectURL)

      // Construir uma nova solicitação com a URL de redirecionamento
      const newRequest = new Request(redirectURL, {
        method: request.method,
        headers: request.headers,
        body: request.body,
      })

      // Enviar a nova solicitação e aguardar a resposta
      const response = await fetch(newRequest)

      // Devolver a resposta original
      return response
    } catch (error) {
      console.error('Erro:', error)
      // Devolver uma resposta de erro, se necessário
      return new Response('Erro interno', { status: 500 })
    }
  },
}

I'll appreciate your help.

There’s special behavior for fetching with custom ports depending on if they are same zone (same domain), or not. You can find the flowchart here: Worker fetch causes 521 error over HTTPS on port 9200 - #3 by albert
In this instance, it’s just requesting port 80 since it’s not same-zone and you specified http.
Also that wouldn’t be secure, get a certificate for your origin (like Cloudflare Origin CA Cert Origin CA certificates · Cloudflare SSL/TLS docs) and then you could create a custom subdomain on that same domain, allowing you to fetch custom ports.

I think I don’t understand…
To enable a custom port, I’ve to set “linkawscustom.compute.amazonaws” on CNAME?

Hi!
I did follow your comment, I created load balance on aws, I set up target group with port 81 and it works. Thank you.

2 Likes

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.