Skip to content

0x61656c/reverseproxy-cf-worker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

reverseproxy-cf-worker

Here's how to implement.

  1. Create a Cloudflare account if you don't already have one, and log in.
  2. Navigate to the "Workers" section on the Cloudflare dashboard.
  3. Click on "Create a Worker", this will direct you to the Workers editor.
  4. Clear any pre-existing code in the script editor.
  5. Copy and paste the anonymized JavaScript code into the script editor.
  6. Substitute the URL placeholders (https://example.com, https://www.example.com, https://proxy.example.workers.dev, proxy.example.com and https://example.com/subdirectory) with the URLs relevant to your use-case.
  7. After updating the URLs, click "Save and Deploy".
  8. Specify the routes on which the worker will run. Go back to the Workers dashboard and click on "Add Route", then define the URL patterns for which the worker should be activated.

The addEventListener listens for 'fetch' events, which are requests coming into the worker. handleRequest is the function processing these requests.

In handleRequest, the request URL is split into segments to enable detailed processing. The if-else structure checks whether the incoming request is to a specific domain, and whether the URL path contains more than a certain number of segments. If the criteria are met, the function modifies the hostname to 'proxy.example.com' and fetches the response from the new URL. If not, the request is forwarded as it was received.

Be sure to customize the conditions for when to proxy and when to forward as-is, the URL you are proxying to, and any adjustments you might want to make to the request or response. Cloudflare Workers come with a free tier suitable for development and small projects, but be aware of the costs for larger scale implementations.

About

Reverse proxy cf worker to forward tangram domains to friendlier versions of themselves

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors