Reverse Proxy cloudflare ip in X-FORWARDED-FOR

For Workers & Pages, what is the name of the domain?

pflabs.net

What is the error number?

no error

What is the error message?

no message

What is the issue or error you’re encountering

unable to get visitor ip

What steps have you taken to resolve the issue?

In a Cloudflare Worker, we are overwriting the X-Forwarded-For header to use the client IP with the following code: headers.set(“X-Forwarded-For”, request.headers.get(“CF-Connecting-IP”));
However, we are still receiving the Cloudflare edge IP instead of the actual client IP. How can we resolve this, and is there a setting in Cloudflare that controls this behavior?

Should work and be used instead while the URL for a Worker is proxied and in-use :orange:

If you bound it to the Worker URL or custom url example.com/my-worker-test and visit in your Web browser, does it show your home IP address or not? :thinking:

Might have to request it first and save before use :thinking:

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request));
})

async function handleRequest(request) {
  const clientIP = request.headers.get('CF-Connecting-IP');

  if (!clientIP) {
    return new Response('Cannot get your IP address', { status: 400 });
  }

  try {
    const res = await myFunction(clientIP);

    if (res.success) {
      return new Response(`Success for IP: ${clientIP}`, { status: 200 });
    } else {
      return new Response(`Error for IP: ${clientIP}`, { status: 500 });
    }
  } catch (error) {
    return new Response(`Error: ${error.message}`, { status: 500 });
  }
}

async function myFunction(ip) {
   ...
}

This topic was automatically closed after 15 days. New replies are no longer allowed.