baicai

白菜

一个勤奋的代码搬运工!

Setting up a reverse proxy service: Setting up 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 explain a simpler way to set it up using different code.

Setting up Reverse Proxy Service with Cloudflare Workers#

The steps for creating Cloudflare workers are the same, only the code is different.

Create a Worker#

On the homepage, select Workers. 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 and click on "Quick Edit". Change the code to the following content, replacing the hostname with your own. Then click on "Save and Deploy" and rename it to something like "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, using the following pattern:

Wait for the DNS to propagate.

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. It will be ready to use.

For a general proxy JSON configuration, refer to the following example:

{
  "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#

Create Configuration File#

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

Enter the following content and save:

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.