Port forwarding with worker

My desired setup:
request → cloudflare (port forwarding with worker) → dreamhost dns record resolve with port set

My setup is:

  • local webserver on port 8731
  • private router which forwards 8731
  • domain (yyy.xxx) on dreamhost
  • cloudflare worker to forward all request to port 8731:
export default {
  async fetch(request, env, ctx) {
    const url = new URL(request.url);
    url.port = "8731";
    return fetch(url, request);
  },
};

I don’t have any DNS records set on cloudflare since I cannot apply the worker on domain root (yyy.xxx) and apply an additional record on the root. Thus the worker is my only config on cloudflare.

When I set my DNS servers to dreamhost only and set port 8731 manually in a request it works correctly. But when I want to add port forwarding by cloudflare and set therefore the cloudflare DNS servers with the worker on domain root (yyy.xxx) I get HTTP error 522.

When I traceroute my domain (yyy.xxx) I see the last server is a cloudflare server (104.21.51.193). Thus my request/response from my worker is not forwarded to dreamhost but I cannot figure out why.

Can anyone help me with this issue?

Your setup is trying to use a non-standard port (8731) with your Cloudflare Worker. However, Workers can’t fetch with a non-standard port. If possible, consider moving your service to use the standard HTTP/HTTPS ports (80/443) which should solve the issue.

If all you are doing is changing the port, then you should use an origin rule

1 Like

Thank you very much. I changed to origin rules because I only need a simple port change. I assume that I need a proxied A/AAAA/CNAME record thus the origin rule is being executed (similar to page rules). In case I use a proxied record I get now:

Bad Request
This combination of host and port requires TLS.

I have SSL enabled and the universal cloudflare certificate. Can you help me here as well?

Ok I had the origin server certificate (on cloudflare) not installed on my local server. Now it works! Thanks a lot!

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