baicai

白菜

一个勤奋的代码搬运工!

Setting up a reverse proxy service: Building a reverse proxy for Telegram Bot API.

The previous article was about setting up a reverse proxy using nginx and cloudflare workers.

This article will show you how to set it up with simpler code.

Setting up Reverse Proxy Service with Cloudflare Workers#

The steps to create Cloudflare workers are the same, only the code is different.

Create a Worker#

Select Workers on the homepage, if you haven't created one before, initialize it, choose the free plan, and then create a Worker.

Edit Worker Content#

Go to the worker, click on Quick Edit, change the code to the content below, replace the hostname with your own, and then click Save and Deploy. You can rename it to, for example, cdn worker.

// This is the URL that needs to be proxied
const hostname = "https://example.domain"
// const hostname = "http://192.168.0.1"
// const hostname = "https://your.domain"
// const hostname = "https://your.domain/api/path"

function handleRequest(request) {
    let url = new URL(request.url);
    return fetch(new Request(hostname + url.pathname,request))
}

addEventListener("fetch", event => {
  event.respondWith(handleRequest(event.request))
})

Add DNS for the Domain#

Add a route and configure it to point to the Worker created in step 1, with the following configuration:

Wait for the DNS to take effect.

Setting up Reverse Proxy Service with Vercel#

Create a new project in GitHub.
Create a file named vercel.json with the following content:

{
  "routes": [
    { "src": "/.*", "dest": "https://example.domain"}
  ]
}

Deploy this project in the Vercel dashboard and configure the custom domain. Then you can use it.

For general proxy JSON content, refer to:

{
  "routes": [
    { "src": "/redirect", "status": 308, "headers": { "Location": "https://example.domain/" } },
    { "src": "/custom-page", "headers": {"cache-control": "s-maxage=1000"}, "dest": "/index.html" },
    { "src": "/api", "dest": "/my-api.js" },
    { "src": "/users", "methods": ["POST"], "dest": "/users-api.js" },
    { "src": "/users/(?<id>[^/]*)", "dest": "/users-api.js?id=$id" },
    { "src": "/legacy", "status": 404},
    { "src": "/.*", "dest": "https://example.domain/"}
  ]
}

Setting up Reverse Proxy with Nginx for a Website#

Create Configuration File#

nano /etc/nginx/conf.d/tgapi.conf

Enter the following content and save it:

server {
    listen 80;
    server_name tgapi.domain;
    location / {
       return 444;
    }
    location ~* ^/bot {
        resolver 8.8.8.8;
        proxy_buffering off;
        proxy_pass      https://example.domain$request_uri;
    }
}
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.