ntfy 服务器配置详解:从基础到高级设置

ntfy 服务器配置详解:从基础到高级设置

ntfy Send push notifications to your phone or desktop using PUT/POST ntfy 项目地址: https://gitcode.com/gh_mirrors/nt/ntfy

概述

ntfy 是一个轻量级的实时消息通知服务,支持跨平台的消息推送和订阅。本文将全面介绍如何配置 ntfy 服务器,包括基础设置、消息缓存、附件管理、访问控制等核心功能。

快速入门

ntfy 开箱即用,只需简单命令即可启动服务:

ntfy serve

默认监听 80 端口,无需额外配置即可开始使用。启动后可通过多种方式发布和订阅消息。

配置方式

ntfy 支持三种配置方式:

  1. 配置文件(通常位于 /etc/ntfy/server.yml
  2. 命令行参数
  3. 环境变量

基础配置示例

仅 HTTP 配置

base-url: "http://ntfy.example.com"
cache-file: "/var/cache/ntfy/cache.db"
attachment-cache-dir: "/var/cache/ntfy/attachments"

HTTP+HTTPS 配置

base-url: "http://ntfy.example.com"
listen-http: ":80"
listen-https: ":443"
key-file: "/etc/letsencrypt/live/ntfy.example.com.key"
cert-file: "/etc/letsencrypt/live/ntfy.example.com.crt"
cache-file: "/var/cache/ntfy/cache.db"
attachment-cache-dir: "/var/cache/ntfy/attachments"

代理后方配置

base-url: "http://ntfy.example.com"
listen-http: ":2586"
cache-file: "/var/cache/ntfy/cache.db"
attachment-cache-dir: "/var/cache/ntfy/attachments"
behind-proxy: true

消息缓存机制

ntfy 提供消息缓存功能,确保设备在网络不稳定时不会丢失通知:

  • 内存缓存:默认缓存 12 小时(重启后丢失)
  • 磁盘缓存:通过 cache-file 配置 SQLite 数据库路径
  • 缓存时长:通过 cache-duration 调整(默认 12h)

禁用缓存只需设置 cache-duration: 0

附件管理

启用附件功能需配置:

base-url: "https://ntfy.sh"
attachment-cache-dir: "/var/cache/ntfy/attachments"
attachment-total-size-limit: "5G"  # 总附件大小限制
attachment-file-size-limit: "15M"  # 单个文件大小限制
attachment-expiry-duration: "3h"   # 附件过期时间

访问控制

ntfy 提供灵活的权限管理系统:

用户管理

ntfy user add --role=admin phil  # 添加管理员
ntfy user change-pass phil      # 修改密码

权限控制

ntfy access phil mytopic rw       # 授予读写权限
ntfy access everyone mytopic ro   # 匿名用户只读权限

访问令牌

ntfy token add --expires=30d --label="backups" phil

安全最佳实践

  1. 对于私有实例,设置 auth-default-access: deny-all
  2. 始终使用 HTTPS 加密通信
  3. 定期轮换访问令牌
  4. 限制附件大小和带宽

高级配置

对于企业级部署,可考虑配置:

  • 防火墙规则
  • 负载均衡
  • 数据库备份策略
  • 监控告警系统

总结

ntfy 的配置系统既简单又强大,从单机部署到企业级应用都能灵活应对。通过合理的缓存、附件和权限配置,可以构建出安全可靠的通知服务系统。建议根据实际需求逐步启用各项功能,并在生产环境中进行充分测试。

ntfy Send push notifications to your phone or desktop using PUT/POST ntfy 项目地址: https://gitcode.com/gh_mirrors/nt/ntfy

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

农爱宜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值