Nginx Proxy Manager 反向代理企业微信机器人,并且不暴露 Webhook 地址

摘要

当你需要在内网向企业微信推送消息,但又不想放行 qyapi.weixin.qq.com 的时候,应该有很多方法,我选择了通过 Nginx Proxy Manager 的容器进行反向代理。互联网有很多跟你将怎么用怎么用的,但最后还是得看后台日志一点一点调试摸索着,最后才成功,现在将方法教你。

本篇文章只适用于我当前工作环境,若正是你需要的,那应该能帮助到你。如果有其他方法,也欢迎大佬在评论区分享自己的方法~


准备阶段

以下所有操作基本在 Linux 下完成。

一台内网服务器: 192.168.1.100

一台反代理服务器:192.168.1.10

一个企业微信机器人的 webhook :

1
https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=0000aaaa-aaaa-aaaa-aaaa-aaaaacccccaa

一个证书(包含密钥),可以自生成

需要的环境:

openssl:任意版本,能生成自签证书就行

docker:可选,你要是动手能力强可以直接安装在宿主机内,docker 都不需要了


原理

graph LR;
内网 --> |curl 192.168.1.10:30021| 反向代理服务器 --> 企业微信机器人

内网服务器使用 CURL 发送消息:

1
2
3
4
5
6
url="http://192.168.1.10:30021/PYMSG"
header="'Content-Type:application/json'"
function send_text(){
textmsg='{"msgtype":"markdown","markdown":{"content":"'$*'","mentioned_list":[""]}}'
curl -s -H $header -d "$textmsg" "$url"
}

反代理服务器转发请求到企业微信的 webhook 接口


安装

Nginx Proxy Manager 官网

https://nginxproxymanager.com/

官网有文档,要是觉得麻烦就用官网给的安装手册,使用 docker compose 进行部署

https://nginxproxymanager.com/setup/

我的是 truenas 上直接部署的,大概是下面 docker-compose.yml 这样子吧

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
services:
app:
image: 'jc21/nginx-proxy-manager:latest'
restart: unless-stopped
ports:
# These ports are in format <host-port>:<container-port>
- '30021:80' # Public HTTP Port
- '30022:443' # Public HTTPS Port
- '30020:81' # Admin Web Port
# Add any other Stream port you want to expose
# - '21:21' # FTP

environment:
# Uncomment this if you want to change the location of
# the SQLite DB file within the container
# DB_SQLITE_FILE: "/data/database.sqlite"

# Uncomment this if IPv6 is not enabled on your host
# DISABLE_IPV6: 'true'

volumes:
- ./data:/data
- ./letsencrypt:/etc/letsencrypt

管理页面的端口是:30020

http 端口是:30021,https 端口是 30022


然后使用:

1
docker compose up -d

启动即可。没有额外的安装步骤,直接这么来就好了。


生成证书

因为企业微信机器人是 https 协议,反代理一般都需要证书。

1
2
3
4
# 私钥生成
openssl genrsa -out ca-private.key 4096
# 根证书生成
openssl req -new -x509 -key ca-private.key -out ca.crt -days 3650

将证书导入 Nginx Proxy Manager

image-20250625112824512 image-20250625113050036

点击 SAVE 保存即可。


配置 proxy

image-20250625114941654 image-20250625114520051

这样就配置好一半了


高级配置

使用 location 创建不通的别名,像我一开始的 http://192.168.1.10:30021/PYMSG 使用了 PYMSG 这个别名,在高级设置里是这个样子的

1
2
3
4
5
6
7
8
9
10
11
12
location / {
return 404;
}

location /PYMSG {
proxy_pass https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=0000aaaa-aaaa-aaaa-aaaa-aaaaacccccaa;
proxy_set_header Host qyapi.weixin.qq.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}

proxy_pass :配置企业微信机器人的 webhook 地址,这样就完成了,点击 save 保存后,在内网就可以直接使用 curl 推送消息了。

注意,一次不能推送超过 4096 字节的消息,超过的话请自行截断。


以此类推,想设置多少个别名随意,基本按照这个方式就可以正常使用了。学会后可以直接去看容器里的 nginx 配置,学学他的配置文件,后续都可以不用这个 Nginx Proxy Manager 容器了,自己下个 nginx 容器,自己做反代理了。