自建推送通知服务:Docker 部署 ntfy 替代 Pushover

1432 字
7 分钟
自建推送通知服务:Docker 部署 ntfy 替代 Pushover

痛点:服务挂了,你在睡觉#

凌晨 3 点,你的服务器磁盘满了,Gitea 挂了,Navidrome 打不开,Caddy 证书续签失败。

第二天早上你才发现。

这不是段子——这是每个自部署玩家都经历过的场景。服务器上的服务不会因为你睡着了就不出问题。

传统的解决方案

  • ❌ 邮件通知 → 容易被丢进垃圾箱,配置 SMTP 麻烦
  • ❌ 短信告警 → 要花钱买服务
  • ❌ Pushover → 好用,但要每月订阅费,闭源,数据走第三方

ntfy 的方案很简单:一个 HTTP 请求,手机就响了。


什么是 ntfy?#

ntfy 界面截图
ntfy 界面截图

ntfy(读作 “notify”)是一个开源的推送通知服务,Go 语言编写,MIT 协议。核心设计:

发布/订阅模式。向一个 topic 发消息,所有订阅了该 topic 的设备都会收到通知。

截至 2026 年 6 月,ntfy 拥有 31k GitHub Stars,活跃维护,每月都有更新。

ntfy vs 其他推送方案#

特性ntfyPushoverGotifyBark
开源✅ MIT❌ 闭源✅ MIT✅ MIT
自部署
Docker 镜像大小10MBN/A~20MB~15MB
编程语言Go-GoGo
手机 App✅ Android/iOS✅ Android❌ 仅 iOS
Web 端支持✅ 自带
桌面推送✅ Web/原生
邮件通知✅ 可配置
消息优先级✅ 1~5 级
附件支持✅ 图片/文件
身份验证✅ 用户名密码/Token✅ API Key✅ Token
付费免费$5/月免费免费
生态集成Uptime Kuma / Grafana / Prometheus / Shell 脚本有限中等仅 iOS

为什么 ntfy 值得部署#

  1. 极轻量 — 10MB Docker 镜像,跑在 256MB 内存的机器上毫无压力
  2. 协议简单 — 核心就是个 HTTP POST 请求,任何语言/脚本都能发
  3. 双端 App — Android 和 iOS 都有官方 App,Web 端也行
  4. 消息持久化 — 默认保留 7 天消息,离线设备上线后能收到
  5. 附件支持 — 可以发图片、文件(比如监控截图)
  6. 生态好 — Uptime Kuma、Grafana Alerting、Prometheus Alertmanager 都原生支持

Docker 部署 ntfy#

环境要求#

  • Docker Engine ≥ 20.10
  • 任意 Linux 服务器,256MB 内存足够
  • 一个域名(推荐,用于 HTTPS)

快速部署#

创建 docker-compose.yml

version: '3.8'
services:
ntfy:
image: binwiederhier/ntfy:latest
container_name: ntfy
ports:
- "8080:80"
volumes:
- ./ntfy_data:/etc/ntfy
- ./ntfy_cache:/var/cache/ntfy
restart: unless-stopped

启动:

Terminal window
mkdir -p ntfy_data ntfy_cache
docker compose up -d

打开 http://你的IP:8080 就能看到 ntfy 的 Web 界面。

配置 HTTPS(推荐)#

用 Caddy 一键反代:

ntfy.yourdomain.com {
reverse_proxy 127.0.0.1:8080
}

用 Nginx Proxy Manager:

  1. 添加 Proxy Host
  2. Domain Names: ntfy.yourdomain.com
  3. Forward to 127.0.0.1:8080
  4. 开启 SSL → Let’s Encrypt

高级配置#

创建 ntfy_data/server.yml

# 基础配置
base-url: "https://ntfy.yourdomain.com"
# 身份验证(可选,建议开启)
auth-file: "/etc/ntfy/auth.db"
auth-default-access: "deny-all"
# 消息保留时间(默认 3 天)
cache-duration: "72h"
# 附件大小限制(默认 15MB)
attachment-size: "25M"
# 开启邮件通知(可选)
smtp-sender-addr: "smtp.gmail.com:587"
smtp-sender-user: "your@gmail.com"
smtp-sender-pass: "app-password"
smtp-sender-from: "ntfy@yourdomain.com"

设置管理员账号:

Terminal window
docker exec ntfy ntfy user add --role=admin admin

使用教程#

1. 安装手机 App#

  • Android: Google Play / F-Droid 搜索 “ntfy”
  • iOS: App Store 搜索 “ntfy”

打开 App → 设置 → 添加服务器 → 输入 https://ntfy.yourdomain.com → 订阅 topic

2. 从命令行发通知#

Terminal window
# 最简单的方式
curl -d "服务器磁盘空间不足!" https://ntfy.yourdomain.com/alert
# 带标题和优先级
curl -H "Title: 磁盘告警" \
-H "Priority: urgent" \
-H "Tags: warning,disk" \
-d "根分区使用率 95%,请立即处理" \
https://ntfy.yourdomain.com/alert
# 发送图片(监控截图)
curl -H "Title: 服务器状态" \
-F "file=@screenshot.png" \
https://ntfy.yourdomain.com/server-status

3. 从脚本发通知#

Shell 脚本

#!/bin/bash
send_alert() {
curl -H "Title: $1" \
-H "Priority: high" \
-d "$2" \
https://ntfy.yourdomain.com/alert
}
# 检查磁盘
disk_usage=$(df / | awk 'NR==2 {print $5}' | tr -d '%')
if [ "$disk_usage" -gt 90 ]; then
send_alert "磁盘告警" "根分区使用率 ${disk_usage}%"
fi

Python 脚本

import requests
def send_notification(topic, title, message, priority=3):
requests.post(
f"https://ntfy.yourdomain.com/{topic}",
headers={
"Title": title,
"Priority": str(priority),
"Tags": "computer"
},
data=message.encode()
)
send_notification("alert", "服务重启", "Caddy 容器已自动重启", priority=4)

4. 集成监控工具#

Uptime Kuma 集成:

  1. 进入 Uptime Kuma → 设置 → 通知
  2. 选择 “ntfy”
  3. 填入服务器 URL 和 topic
  4. 测试 → 手机立刻收到!

Grafana Alerting 集成:

# 在 Grafana 中配置 Contact Point
type: webhook
url: https://ntfy.yourdomain.com/grafana-alert
http method: POST

Prometheus Alertmanager 集成:

receivers:
- name: 'ntfy'
webhook_configs:
- url: 'https://ntfy.yourdomain.com/prometheus-alert'

常见问题 FAQ#

Q1: ntfy 的消息会丢失吗?#

消息默认在服务端保留 3 天(可配置)。如果你的手机离线,上线后会收到所有未读消息。超过保留期的消息会被自动清理。

Q2: 可以自定义通知铃声吗?#

可以。ntfy App 支持为不同 topic 设置不同的铃声、震动模式、LED 颜色。比如把 “critical” topic 设为高优先级震动,把 “info” topic 设为静音通知。

Q3: 安全性如何?#

ntfy 支持可选的身份验证(用户名密码或 Token)。建议:

  • 开启 auth-default-access: "deny-all" 限制匿名访问
  • 用 Caddy/NPM 配置 HTTPS
  • 每个服务使用独立的 topic 名称

Q4: 免费的 ntfy.sh 和自部署有什么区别?#

ntfy 官方提供了免费的 ntfy.sh 公共服务器,无需部署就能用。但注意:

  • 公共服务器上的消息对所有知道 topic 名的人可见
  • 没有身份验证保护
  • 受限于官方配额

自部署是自己的地盘,更安全、更可控。

Q5: 跟 Gotify 比怎么选?#

特性ntfyGotify
iOS App✅ 官方❌ 社区版
消息保留✅ 可配置
附件支持
优先级✅ 1~5 级
镜像大小10MB~20MB
GitHub Stars31k12k

如果你只用 Android,两个都行。如果你有 iOS 设备,ntfy 是唯一选择


总结#

ntfy 是自部署推送通知的首选方案。10MB 的镜像、一条 curl 命令就能发通知、App 双端支持、生态集成完善——从 Uptime Kuma 到 Prometheus,从 Shell 脚本到 Python 应用,ntfy 都能接入。

一句话总结:你的服务可以没有花哨的仪表盘,但不能没有 ntfy。因为服务挂了没人通知,等于没挂——只是你还没发现而已。

支持与分享

如果这篇文章对你有帮助,欢迎分享给更多人或赞助支持!

赞助
自建推送通知服务:Docker 部署 ntfy 替代 Pushover
https://www.kshare.top/posts/2026-ntfy-自建推送通知服务docker-部署替代-pushover/
作者
Kshare
发布于
2026-06-28
许可协议
CC BY-NC-SA 4.0
Profile Image of the Author
Kshare
Hello, I'm Kshare.
公告
站点已开启广告(类型:弹窗广告),给您带来的不便敬请谅解。如有更好的广告建议,欢迎发送邮件至 t9uzrz6u@anonaddy.me,感谢您的支持!
音乐
封面

音乐

暂未播放

0:00 0:00
暂无歌词
分类
标签
站点统计
文章
187
分类
9
标签
190
总字数
490,175
运行时长
0
最后活动
0 天前

文章目录