A lightweight reverse proxy built for Cloudflare Workers that selectively routes specific paths to a target domain while passing through all other requests to the original domain.
This reverse proxy acts as a smart router that:
- Intercepts requests to specific configured routes
- Forwards those requests to a target domain
- Passes all other requests through unchanged to the original domain
- Preserves all request headers, methods, query parameters, and body content
The proxy uses pattern matching to determine which requests to forward:
- Request Interception: Every incoming request is analyzed
- Route Matching: The pathname is checked against configured proxy routes
- Conditional Forwarding:
- Matching routes → forwarded to
PROXY_TARGET - Non-matching routes → passed through to original domain
- Matching routes → forwarded to
- Response Relay: The target server's response is returned to the client
The destination domain where specific routes will be forwarded.
const PROXY_TARGET = 'new.polygon.technology';An array of path patterns that should be proxied to the target domain.
const PROXY_ROUTES = ['/', '/about', '/contactUs'];The proxy matches routes using exact path matching and prefix matching:
- Exact Match:
/aboutmatches exactly/about - Prefix Match:
/aboutalso matches/about/team,/about/history, etc.
Example with current configuration:
polygon.technology/→ forwarded tonew.polygon.technology/polygon.technology/about→ forwarded tonew.polygon.technology/aboutpolygon.technology/contactUs→ forwarded tonew.polygon.technology/contactUspolygon.technology/products→ passed through topolygon.technology/products
The proxy maintains complete request integrity:
- Method: GET, POST, PUT, DELETE, etc.
- Headers: All original headers are forwarded
- Body: Request body is passed through unchanged
- Query Parameters: All query strings are preserved
Example:
Original: POST https://polygon.technology/contactUs?source=landing
Proxied: POST https://new.polygon.technology/contactUs?source=landing
Follow these steps to deploy the reverse proxy from scratch:
- Log in to your Cloudflare Dashboard
- Navigate to Workers & Pages in the left sidebar
- Click Create Application
- Select Create Worker
- Give your worker a name (e.g.,
polygon-proxy) - Click Deploy
- After deployment, click Edit Code to open the code editor
- Delete the default code
- Copy and paste the entire content of
reverse-proxy.js - Update the configuration:
- Set
PROXY_TARGETto your target domain (e.g.,polygon.technology) - Configure
PROXY_ROUTESwith the paths you want to proxy (e.g.,['/', '/about', '/contactUs'])
- Set
- Click Save and Deploy
- In Cloudflare Dashboard, go to your domain's DNS settings
- Configure DNS records for both domains:
For the base/original domain (example.com):
- If an existing DNS record exists:
- Click Edit on the existing record
- Update the Target to:
cdn.webflow.com - Ensure Proxy status: ☁️ Proxied (Orange cloud enabled)
- TTL: Auto
- If no record exists:
- Click Add record
- Type:
CNAME - Name:
@(for root domain) - Target:
cdn.webflow.com - Proxy status: ☁️ Proxied (Orange cloud enabled)
- TTL: Auto
For the new target subdomain (e.g., new.example.com):
- Click Add record
- Type:
CNAME - Name:
new(or your subdomain name) - Target:
cdn.webflow.com - Proxy status: ☁️ Proxied (Orange cloud enabled)
- TTL: Auto
- Go back to Workers & Pages in Cloudflare Dashboard
- Select your worker (e.g.,
polygon-proxy) - Go to Settings → Triggers
- Under Routes, click Add route
- Configure the route:
- Route:
example.com/*(replaceexample.comwith your actual domain) - Worker: Select your deployed worker
- Zone: Select your domain
- Route:
- Click Add route
- Wait 1-2 minutes for DNS and route propagation
- Visit your domain in a browser
- Check the Cloudflare Workers logs:
- Go to your worker in the dashboard
- Navigate to Observability tab to view real-time logs
- Access your domain in a browser to generate log entries
- Verify that:
- Proxied routes (e.g.,
/,/about,/contactUs) are forwarded toPROXY_TARGET - Non-proxied routes are passed through to the original domain
- Proxied routes (e.g.,
- Cloudflare Worker created and named
- Proxy code deployed with correct
PROXY_TARGETandPROXY_ROUTES - Base domain DNS record updated/added pointing to
cdn.webflow.com(Proxied/Orange cloud) - New subdomain DNS record added pointing to
cdn.webflow.com(Proxied/Orange cloud) - Worker route configured for your domain pattern (e.g.,
polygon.technology/*) - Observability logs verified showing correct routing behaviour
- The proxy preserves the original request's pathname and search parameters
- Response status codes, headers, and body are all forwarded from the target
- Console logs are available in the Cloudflare Workers dashboard for debugging