2024-05-15 04:32:59 +02:00

86 lines
2.4 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 反向代理 Telegram Bot API
如果你的 Dashboard 服务器无法访问 Telegram Bot API但你依然想使用 Telegram 来推送通知,你可以尝试使用反向代理的方式解决这个问题。
## 准备工作
**这里介绍使用你自己的服务器进行反代的方法。你也可以选择使用 Cloudflare 的 Workers 进行反代,但可能对于中国大陆的用户来说网络连通性依然不佳。**
要搭建一个 Telegram Bot API 反代,你需要准备以下内容:
1. 一个可以连接 Telegram Bot API 服务器(并安装好 Nginx
2. 一个域名(提前申请 SSL 证书)。
## NGINX 配置
编辑 Nginx 配置文件,在 `http{}` 中添加如下配置:
```nginx
# HTTP 强制跳转到 HTTPS
server {
listen 80;
listen [::]:80;
server_name <yourDomainName>;
# 强制 HTTPS
return 301 https://$server_name$request_uri;
}
# HTTPS 配置
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name <yourDomainName>;
# SSL 证书路径
ssl_certificate </path/to/your/server.pem>;
ssl_certificate_key </path/to/your/server.key>;
# Root 非必要
root /var/www/tgbot/;
# 必须配置 DNS否则会报 502 错误
resolver 8.8.8.8;
# 以 /bot 开头的请求会被正则匹配
location ~* ^/bot {
proxy_buffering off;
proxy_pass https://api.telegram.org$request_uri;
proxy_http_version 1.1;
}
# Root 非必要,主要用于确认服务器状态。也可以改为 return 403
location / {
try_files $uri $uri /index.html;
}
# 错误日志
error_log /var/log/tg.log error;
}
```
- `yourDomainName`:你准备的域名
- `ssl_certificate`SSL 证书路径
- `ssl_certificate_key`SSL 证书路径
## 使用方式
执行 `systemctl restart nginx` 重启 Nginx。然后在 Nezha 中将原来的 `https://api.telegram.org/` 替换为 `https://<yourDomainName>/`,即可正常推送消息。
## 防止盗用
配置防火墙以防止他人盗用你的反代服务:
- `serverIp`Agent 的 IP 地址。根据你的系统选择适用的命令,`ufw``iptables` 均可。
```bash
# Ubuntu
ufw allow proto tcp from <serverIp> to any port 443
# CentOS
iptables -I INPUT -p tcp --dport 443 -j DROP
iptables -I INPUT -s <serverIp> -p tcp --dport 443 -j ACCEPT
```
通过以上配置,可以有效防止未经授权的访问。