A simple library for creating reverse proxies with Cloudflare Workers.
npm install workers-proxy
import { createProxy } from "workers-proxy";
export default {
fetch: createProxy({
origin: "https://example.com",
rewrites: [
{
source: "/blog/:path*",
destination: "http://blog.example.com",
},
],
}),
};
The origin
option is the URL of the target server you want to proxy requests to. Traffic that doesn't match a rewrite or redirect rule will be proxied to this URL.
The rewrites
option is an array of rewrite rules that will be applied to the requested URL before it is proxied to the target server. Each rule is an object with the following properties:
-
source - The pattern to match against the requested URL pathname. Patterns can contain named parameters prefixed with a colon (e.g.
:path
) and include a zero-or-more operator to match the rest of the path (e.g.:path*
). Parameters are accessible in thedestination
URL using the same name. Learn more about formatting patterns in the path-to-regexp documentation. -
destination - The URL to proxy the request to. Parameters from the
source
pattern can be used in thedestination
URL.
The redirects
option is an array of redirect rules that will will result in the proxy returning a redirect response to the client. Each rule is an object with the following properties:
-
source - The pattern to match against the requested URL pathname. Patterns can contain named parameters prefixed with a colon (e.g.
:path*
) and include a zero-or-more operator to match the rest of the path (e.g.:path*
). Parameters are accessible in thedestination
URL using the same name. Learn more about formatting patterns in the path-to-regexp documentation. -
destination - The URL to redirect the request to. Parameters from the
source
pattern can be used in thedestination
URL. Must be an absolute URL. - permanent - A boolean value indicating whether the redirect should be permanent (308) or temporary (307).