之前使用过 HAProxy 配置过一次,略显繁琐,现在带来了更加便捷的方法。

利用 OpenWrt 自带的 uhttpd 服务,暴露一个接口。

/www/cgi-bin/ 目录下新增一个文件,例如 healthcheck,内容如下:

#!/bin/sh

uclient-fetch --timeout=5 --no-check-certificate -O - https://www.gstatic.com/generate_204 >/dev/null 2>&1

if [ $? -eq 0 ]; then
    echo "Status: 200 OK"
    echo "Content-Type: text/plain"
    echo ""
    echo "Network OK"
else
    echo "Status: 503 Service Unavailable"
    echo "Content-Type: text/plain"
    echo ""
    echo "Network Failed"
fi

设置文件权限。

chmod a+x healthcheck

新增一个 uhttpd 实例,监听在 8081 端口,并且默认访问 / 时定向到 /cgi-bin/healthcheck

uci set uhttpd.healthcheck=uhttpd
uci add_list uhttpd.healthcheck.listen_http='0.0.0.0:8081'
uci add_list uhttpd.healthcheck.listen_http='[::]:8081'
uci set uhttpd.healthcheck.redirect_https='0'
uci set uhttpd.healthcheck.rfc1918_filter='1'
uci set uhttpd.healthcheck.home='/www'
uci set uhttpd.healthcheck.tcp_keepalive='1'
uci add_list uhttpd.healthcheck.alias='/=/cgi-bin/healthcheck'
uci set uhttpd.healthcheck.cgi_prefix='/cgi-bin'
uci set uhttpd.healthcheck.script_timeout='60'
uci set uhttpd.healthcheck.network_timeout='30'
uci set uhttpd.healthcheck.http_keepalive='20'
uci set uhttpd.healthcheck.max_connections='100'
uci set uhttpd.healthcheck.max_requests='3'

uci commit uhttpd

/etc/init.d/uhttpd restart

在 RouterOS 上新建一个监控服务。

add comment="passwall healthcheck" name=passwall_healthcheck \
  up-script=enable_passwall down-script=disable_passwall \
  type=http-get interval=15s timeout=5s \
  host=192.168.88.2 port=8081
最后修改:2026 年 01 月 09 日
如果觉得我的文章对你有用,请随意赞赏