<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: WFH Alert</title>
    <description>The latest articles on Forem by WFH Alert (@wfhalert).</description>
    <link>https://forem.com/wfhalert</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3065353%2Fdb9eb1fd-03ab-4dde-a6ef-e84a65ab9692.jpg</url>
      <title>Forem: WFH Alert</title>
      <link>https://forem.com/wfhalert</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/wfhalert"/>
    <language>en</language>
    <item>
      <title>How to Set Up a Simple Reverse Proxy Using Cloudflare Workers</title>
      <dc:creator>WFH Alert</dc:creator>
      <pubDate>Sat, 19 Apr 2025 10:44:58 +0000</pubDate>
      <link>https://forem.com/wfhalert/how-to-set-up-a-simple-reverse-proxy-using-cloudflare-workers-l6d</link>
      <guid>https://forem.com/wfhalert/how-to-set-up-a-simple-reverse-proxy-using-cloudflare-workers-l6d</guid>
      <description>&lt;p&gt;Reverse proxies are incredibly handy for efficiently managing web traffic, especially when you want to separate your frontend domain from backend services. This article walks through setting up a quick reverse proxy using Cloudflare Workers, with a practical example from WFH Alert.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Cloudflare Workers?
&lt;/h2&gt;

&lt;p&gt;Cloudflare Workers let you run serverless JavaScript functions directly at the network edge, offering ultra-low latency and scalability without managing your own infrastructure. Perfect for quickly routing requests without complexity.&lt;/p&gt;

&lt;h2&gt;
  
  
  Real-World Example
&lt;/h2&gt;

&lt;p&gt;At &lt;a href="https://www.wfhalert.com" rel="noopener noreferrer"&gt;WFH Alert&lt;/a&gt;, we faced limitations with Beehiiv (our email management platform) regarding custom hosting of interactive tools like our &lt;a href="https://www.wfhalert.com/tools/cover-letter-generator/" rel="noopener noreferrer"&gt;Cover Letter Generator&lt;/a&gt;. Beehiiv doesn't support custom backend scripts or dynamic tool hosting, so we used Cloudflare Workers to seamlessly proxy our tool hosted elsewhere, maintaining a consistent domain structure for SEO and usability.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step-by-Step Guide
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Step 1: Create a Cloudflare Worker
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Log in to your &lt;a href="https://dash.cloudflare.com/" rel="noopener noreferrer"&gt;Cloudflare Dashboard&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Navigate to &lt;strong&gt;Workers&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;Create Worker&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Step 2: Configure the Worker Script
&lt;/h3&gt;

&lt;p&gt;Replace the default worker script with the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;fetch&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;respondWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;handleRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;URL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// Change the hostname to your backend service&lt;/span&gt;
  &lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hostname&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;your-backend-domain.com&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Replace with your backend domain&lt;/span&gt;
  &lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;protocol&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Ensures secure connection&lt;/span&gt;

  &lt;span class="c1"&gt;// Create a new request preserving method and headers&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;newRequest&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// Fetch and return the response from your backend&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newRequest&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replace &lt;code&gt;your-backend-domain.com&lt;/code&gt; with your backend domain.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3: Deploy and Test Your Worker
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Click &lt;strong&gt;Save and Deploy&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Test your worker directly in the Cloudflare dashboard or by visiting the provided Workers URL.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Step 4: Configure Route for the Worker
&lt;/h3&gt;

&lt;p&gt;Now you'll want your worker to listen on specific paths:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In your Cloudflare Dashboard, go to &lt;strong&gt;Workers &amp;gt; Routes&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Add a new route, for example:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;*.yourdomain.com/tools/example-tool*
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make sure to adjust paths appropriately based on your needs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 5: Verify
&lt;/h3&gt;

&lt;p&gt;Navigate to your configured route, e.g., &lt;code&gt;yourdomain.com/tools/example-tool&lt;/code&gt;. You should now see responses directly from your backend server, seamlessly routed via Cloudflare Workers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Practical Use Cases
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Decoupling frontend and backend services.&lt;/li&gt;
&lt;li&gt;Easily integrating external APIs without dealing with CORS.&lt;/li&gt;
&lt;li&gt;Adding security and caching layers effortlessly.&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
  </channel>
</rss>
