Workers-Proxy is a lightweight Javascript application that retrieves resource as a client from other servers.
Deploying on Cloudflare Workers, which is an influential platform for building serverless applications, you could build customized reverse proxy without purchasing virtual machines and configuring web servers such as Nginx or Apache.
Moreover, crucial performance such as latency and availability will be optimized, since your serverless application will be deployed on Cloudflare's global network of data centers across 200 cities in 90 countries.
By configuring Geolocation and IP address filters, you could directly suspend your reverse proxy service in specific countries or regions based on their regulations. Taking advantage of the mobile redirector, you could distribute various webpages based on users' devices.
Reverse-Proxy Project (This demo may not be available in specific regions.)
-
Generate a new project.
wrangler generate my-workers-proxy https://github.com/Siujoeng-Lau/Workers-Proxy
- Configure your project's
wrangler.toml
file to prepare your project for deployment.
wrangler config
- Build and deploy on Cloudflare Workers.
wrangler build
wrangler publish
-
Navigate to Cloudflare Workers, register or sign in your Cloudflare account, and set custom subdomain for workers, and create a new Worker.
-
Customize 'src/index.js', paste the code into Cloudflare online editor to replace the default one.
-
Change name of your Worker, save and deploy it, and check whether its performance fulfills your demand.
-
Check whether your domain is currently under Cloudflare's protection.
-
Navigate to the dashboard of your domain, select 'Workers' page, and click on 'Add Route'.
-
Type
https://<domain-name>/*
inRoute
and select the Worker you created previously. -
Add a CNAME DNS record for your custom domain. Concretely, enter the subdomain (or '@' for root) in the 'Name' field, enter the second level domain of your workers in the 'Target' field, and set 'Proxy status' to 'Proxied'.
Basically, there are a few constants on the top of the 'index.js' file.
To customize your own Workers-Proxy Service, you should edit these constants according to your demands.
// Website you intended to retrieve for users.
const upstream = 'www.google.com'
// Custom pathname for the upstream website.
const upstream_path = '/'
// Website you intended to retrieve for users using mobile devices.
const upstream_mobile = 'www.google.com'
// Countries and regions where you wish to suspend your service.
const blocked_region = ['CN', 'KP', 'SY', 'PK', 'CU']
// IP addresses which you wish to block from using your service.
const blocked_ip_address = ['0.0.0.0', '127.0.0.1']
// Whether to use HTTPS protocol for upstream address.
const https = true
// Replace texts.
const replace_dict = {
'$upstream': '$custom_domain',
'//google.com': ''
}