ntfy 2.32.0 配置参数详解

ntfy 2.32.0 服务端配置参数详解:server.yml 查看修改方式、全参数分组总览、监听/缓存/认证/附件/限流/日志逐组实测验证、生产配置模板与常见故障,Ubuntu 22.04 环境 2026-10-21 全量实测。

ntfyntfy即时通信2026/10/21

ntfy 2.32.0 配置参数详解

目标:系统梳理 ntfy 2.32.0 服务端配置文件 /etc/ntfy/server.yml 的参数体系——查看与修改方式、全参数分组总览、核心参数逐组详解(每组配本机实测回显)、生产配置模板与常见故障,帮助把一台默认配置的 ntfy 调成可上线的状态。全部实测在 Ubuntu 22.04.5 LTS + ntfy 2.32.0(systemd 托管,HTTP 监听 8144)环境完成,安装步骤见本专栏《Ubuntu 22.04 安装 ntfy 2.32.0 指南》。

一、环境信息

本章列出实测环境,正文所有命令与回显均来自该环境。

  • 操作系统:Ubuntu 22.04.5 LTS,x86_64/amd64;
  • ntfy 版本:2.32.0(ntfy version 2.32.0,安装方式为 GitHub release 二进制 + systemd,二进制位于 /usr/local/bin/ntfy);
  • 实测时间:2026-10-21;
  • 服务托管:systemd 单元 ntfy.service,配置文件 /etc/ntfy/server.yml;
  • HTTP 监听:8144(listen-http: ":8144",已避开默认 :80);
  • 已启用的功能配置(实测基线):消息缓存 cache-file、认证 auth-file + auth-default-access: deny-all、附件 attachment-cache-dir、base-url;
  • 实测账号:普通用户 phil(密码 q1w2e3r4,演示用途)及其访问令牌。

二、前置条件

已按《Ubuntu 22.04 安装 ntfy 2.32.0 指南》完成安装并验证服务 active、健康检查通过:

systemctl is-active ntfy && curl -s http://127.0.0.1:8144/v1/health

实测输出:

active
{"healthy":true,"role":"active"}

同时确认配置文件在位(约 550 行、注释齐全,全部选项默认注释、使用内置默认值运行):

wc -l /etc/ntfy/server.yml
551 /etc/ntfy/server.yml

三、参数查看与修改方式

ntfy 2.32.0 的服务端参数有三条查看/修改路径,优先级从低到高:文件配置 → 环境变量 → 命令行覆盖。

3.1 配置文件 /etc/ntfy/server.yml(主路径)

服务端唯一默认配置文件,YAML 格式,所有选项支持中划线与下划线两种写法(文件头注释原文:「All options also support underscores (_) instead of dashes (-) to comply with the YAML spec」)。修改后重启生效:

sudo nano /etc/ntfy/server.yml      # 编辑(本例改 listen-http)
sudo systemctl restart ntfy

验证生效(实测改 8144 后):

grep -n '^listen-http' /etc/ntfy/server.yml && ss -ltn | grep 8144
22:listen-http: ":8144"
LISTEN 0      4096               *:8144            *:*

💡 说明:log-level 与 log-level-overrides 两项支持不重启热加载——systemctl reload ntfy 或向进程发 kill -HUP(来源:server.yml 内 logging 节注释原文「Some options (log-level and log-level-overrides) can be hot reloaded by calling kill -HUP $pid or systemctl reload ntfy」)。其余参数均需重启。

3.2 命令行参数覆盖(临时实验/多实例)

ntfy serve 的命令行参数可逐项覆盖配置文件(--help 原文:「Config options can be overridden using the command line options」),且每个参数都带 $NTFY_* 环境变量形态。实测:配置文件监听 8144(systemd 服务运行中),用 CLI 覆盖为 8145 临时再起一个实例:

timeout 6 ntfy serve --listen-http :8145 --cache-file /tmp/tc.db --no-log-dates

实测输出(后台启动后另开会话探测):

INFO Listening on :8145[http], ntfy 2.32.0, log level is INFO (tag=startup)
ss -ltn | grep -E '814[45]'
LISTEN 0      4096               *:8144            *:*
LISTEN 0      4096               *:8145            *:*

两个实例同机并存,证明 CLI 参数覆盖优先于配置文件。完整覆盖参数清单查看:

ntfy serve --help

输出节选(实测首尾,含关键默认值):

OPTIONS:
   --debug, -d                                     enable debug logging (default: false) [$NTFY_DEBUG]
   ...
   --config value, -c value                        config file (default: "/etc/ntfy/server.yml") [$NTFY_CONFIG_FILE]
   --base-url value, --base_url value, -B value    externally visible base URL for this host (e.g. https://ntfy.sh) [$NTFY_BASE_URL]
   --listen-http value, --listen_http value, -l value   ip:port used as HTTP listen address (default: ":80") [$NTFY_LISTEN_HTTP]
   --cache-file value, --cache_file value, -C value     cache file used for message caching [$NTFY_CACHE_FILE]
   --cache-duration since, --cache_duration since, -b since   buffer messages for this time to allow since requests (default: "12h") [$NTFY_CACHE_DURATION]
   --auth-file value, --auth_file value, -H value       auth database file used for access control [$NTFY_AUTH_FILE]
   --auth-default-access value, --auth_default_access value, -p value   default permissions if no matching entries in the auth database are found (default: "read-write") [$NTFY_AUTH_DEFAULT_ACCESS]
   --attachment-file-size-limit value, ... (default: "15M")
   --keepalive-interval value, ... (default: "45s")
   --manager-interval value, ... (default: "1m")
   --web-root value, ... (default: "/")
   ...

⚠️ 注意:--cache-duration 的默认值 12h、--listen-http 默认 :80、附件三限默认 5G/15M/3h、keepalive-interval 默认 45s、manager-interval 默认 1m、web-root 默认 /,均取自实测 --help 回显——配置文件不写这些项时即按上述默认运行。

3.3 当前生效状态核对

参数修改后,用三类手段核对「真生效」:

  • 监听类 → ss -ltn;
  • 行为类 → 对应请求实测(认证 401/403、限流 429、消息 expires 字段等,见第五章各组实测);
  • 功能类 → 端点探测(如 /metrics)。

四、参数总览

server.yml 全部参数按功能分为 11 组(分组依据:官方配置文件注释结构与 docs.ntfy.sh/docs/config 章节布局,2026-10-21 核对):

分组代表参数作用
监听与 TLSlisten-http、listen-https、listen-unix、key-file、cert-fileHTTP/HTTPS/Unix socket 监听
对外地址base-url附件下载 URL、邮件页脚、iOS upstream 等功能的对外基准
消息缓存cache-file、cache-duration、cache-startup-queries、cache-batch-*消息持久化与保留时长
数据库扩展database-url(2.32 新增)消息/用户/Web Push 整体迁 PostgreSQL
认证与 ACLauth-file、auth-default-access、auth-users/access/tokens、enable-login 等用户、令牌、主题级读写控制
附件attachment-cache-dir、attachment-*-limit、attachment-expiry-duration附件存储与配额
邮件与电话smtp-server-*(入)、smtp-sender-*(出)、twilio-*邮件发布/转发、语音外呼
推送上游firebase-key-file、upstream-base-url、web-push-*FCM、iOS 中转、浏览器 Web Push
限流与防滥用visitor-request-limit-*、visitor-message-daily-limit、visitor-topic-creation-limit-*、ban-file 等按访客(IP)多维限流与 fail2ban 联动
Web 与功能开关web-root、enable-signup、enable-login、require-login、enable-reservations、enable-metrics、profile-listen-httpWeb App 行为与可观测端点
日志log-level、log-format、log-file、log-level-overrides日志级别/格式/落盘/热调级

以下第五章对每组核心参数详解;未展开的小众参数(Stripe 计费、实验性集群 experimental-cluster-*、Twilio 细项)在表内定位,配置时参考 server.yml 内注释即可。

五、核心参数分类详解

本章每节按「参数 → 默认值 → 实测验证」展开,实测回显均来自当前环境。

5.1 监听与对外地址(listen-http / base-url)

listen-http——HTTP 监听地址,默认 :80,格式 [<ip>]:<port>,设 - 禁用 HTTP(仅 HTTPS 场景):

listen-http: ":8144"

实测验证见 3.1 节(ss -ltn 见 8144 LISTEN)。

listen-https + key-file/cert-file——内建 HTTPS 监听;配置 listen-https 时必须同时给证书与私钥,否则启动失败。官方建议生产由反代终结 TLS 或配置证书。

base-url——对外可见基准 URL,默认空。以下功能必须配置(server.yml 注释原文列举):附件下载 URL、邮件页脚主题链接、自托管 iOS 及时推送(upstream poll_request 计算)、Matrix Push Gateway 校验:

base-url: "http://ntfy.example.com:8144"

⚠️ 注意:base-url 必须与客户端实际访问地址完全一致(含端口),否则附件下载 URL 会指向错误地址。本文实测环境填 http://127.0.0.1:8144(附件 URL 生成实测见 5.4 节)。

5.2 消息缓存(cache-file / cache-duration)

ntfy 消息默认仅内存缓冲,重启即失;cache-file 启用 SQLite 持久化,支持重启后 since= 回放与 poll=1 拉取。追加配置并重启:

cache-file: /var/cache/ntfy/cache.db
cache-duration: "12h"

实测验证——重启后缓存库自动创建:

ls -la /var/cache/ntfy/
total 80
drwxr-xr-x 3 ntfy ntfy  4096 Oct 21 13:23 .
drwxr-xr-x 1 root root  4096 Oct 21 13:23 ..
drwxr-xr-x 2 ntfy ntfy  4096 Oct 21 13:23 attachments
-rw-r--r-- 1 ntfy ntfy 65536 Oct 21 13:23 cache.db

发布若干消息后带 token 回放全部缓存(poll=1&since=all):

curl -s -H 'Authorization: Bearer tk_l8dn95bsajk63328gex7trg3817wq' 'http://127.0.0.1:8144/phil-topic/json?poll=1&since=all' | python3 -c 'import sys,json
for line in sys.stdin:
    d=json.loads(line)
    print(d.get("id"), d.get("event"), d.get("topic"), repr(d.get("title")), repr(d.get("message"))[:60], d.get("priority",""))'

实测输出:

YJEqUqLkvapB message phil-topic None 'Backup finished successfully'
qGCtfQQlcTtf message phil-topic None 'msg via CLI url form'
k4cW9Lbtx5bC message phil-topic 'Backup report' 'Disk usage 92% on /var' 4
NazLJG4n7l7Q message phil-topic None 'report attached'
Iv6C1oSakrUq message phil-topic None 'delayed message via CLI'

cache-duration——消息保留时长,默认 12h,到期的消息由后台 manager 清理;设 0 完全禁用缓存。实测改 1m 观察(同时把 manager-interval 临时改 1m 加速清理):

curl -s -u phil:q1w2e3r4 -d 'expire-test-msg' http://127.0.0.1:8144/phil-topic | head -c 150
{"id":"1mvgwrenB4jW","time":1792589911,"expires":1792589971,"event":"message","topic":"phil-topic","message":"expire-tes

关键观察:expires = time + 60,缓存时长立即体现在消息元数据上。等待约 2.5 分钟(超过 cache-duration + manager 周期)后回查:

curl -s -u phil:q1w2e3r4 'http://127.0.0.1:8144/phil-topic/json?poll=1&since=all' | grep -c 1mvgwrenB4jW
0

(0 命中 = 已被清理;同一时刻 manager 日志记录了清理与统计:)

Oct 21 13:39:29 12c75995d5e1 ntfy[5061]: INFO Server stats (emails_received=0, emails_received_failure=0, emails_received_success=0, emails_sent=0, emails_sent_failure=0, emails_sent_success=0, messages_cached=11, messages_published=10, subscribers=0, tag=manager, topics_active=1, users=2, visitors=1)
Oct 21 13:40:29 12c75995d5e1 ntfy[5061]: INFO Server stats (... messages_cached=10, ...)

期间还出现一条与附件联动的 WARN(attachment-expiry-duration 3h 大于 cache-duration 1m 时附件实际随消息 1m 删除):

Oct 21 13:38:29 12c75995d5e1 ntfy[5061]: WARN attachment-expiry-duration (3h) is longer than cache-duration (1m); attachments never outlive their message, so they will be deleted after 1m

⚠️ 注意:cache-duration 与 attachment-expiry-duration 存在联动约束——附件存活时间不能超过消息缓存时长,调小 cache-duration 前先核对附件配置,否则告警图片等附件会比预期早删。

database-url(2.32 新增)——设置后消息缓存、用户库、Web Push 订阅三库整体迁 PostgreSQL,且隐式启用认证(auth-file 与 cache-file 不得再设),URL 支持 sslmode、pool_max_conns(默认 10)等参数(server.yml 注释口径)。本文未部署 PostgreSQL,此项以官方文档为准。

5.3 认证与访问控制(auth-file / auth-default-access)

默认匿名全开。启用认证的最小配置:

auth-file: /var/lib/ntfy/user.db
auth-default-access: deny-all
  • auth-file:用户/令牌/授权 SQLite 库,不存在自动创建(实测重启后生成 118784 字节);
  • auth-default-access:未匹配 ACL 条目时的兜底权限,可选 read-write(默认)/read-only/write-only/deny-all。

实测重启后库文件生成:

ls -la /var/lib/ntfy/
total 128
drwxr-xr-x 2 ntfy ntfy   4096 Oct 21 13:24 .
drwxr-xr-x 1 root root   4096 Oct 21 13:24 ..
-rw-r--r-- 1 ntfy ntfy 118784 Oct 21 13:24 user.db

deny-all 立即生效(匿名发布被拒,错误码 40301):

curl -s -d 'test' http://127.0.0.1:8144/mytopic
{"code":40301,"http":403,"error":"forbidden","link":"https://ntfy.sh/docs/publish/#authentication"}

配套管理命令三件套(读 auth-file,无需 --config,默认即读 /etc/ntfy/server.yml):

ntfy user add phil        # 交互输入两次密码(实测输入 q1w2e3r4)
user phil added with role user
ntfy user list
user phil (role: user, tier: none)
- no topic-specific permissions
user * (role: anonymous, tier: none)
- no topic-specific permissions
- no access to any (other) topics (server config)

签发访问令牌(tk_ 前缀,仅此一次展示,脚本/无状态客户端用它而非密码):

ntfy token add phil
token tk_l8dn95bsajk63328gex7trg3817wq created for user phil, never expires

授予用户对特定主题的读写(模式支持 phil-* 这类通配):

ntfy access phil phil-topic rw
granted read-write access to topic phil-topic

授权后的三种合法发布形态实测:

curl -s -H 'Authorization: Bearer tk_l8dn95bsajk63328gex7trg3817wq' -d 'Backup finished successfully' http://127.0.0.1:8144/phil-topic
{"id":"YJEqUqLkvapB","time":1792589129,"expires":1792632329,"event":"message","topic":"phil-topic","message":"Backup finished successfully"}
curl -s -u phil:q1w2e3r4 -d 'msg via basic auth' http://127.0.0.1:8144/phil-topic
{"id":"3aajgBBpG8z1","time":1792589892,"expires":1792633092,"event":"message","topic":"phil-topic","message":"msg via basic auth"}

错误凭据行为(错密码 → 40101 unauthorized,与无权限的 40301 forbidden 相区分):

curl -s -u phil:wrongpass -d 'x' http://127.0.0.1:8144/phil-topic
{"code":40101,"http":401,"error":"unauthorized","link":"https://ntfy.sh/docs/publish/#authentication"}

声明式预置(auth-users/auth-access/auth-tokens)——可直接在 server.yml 写入用户(bcrypt 哈希由 ntfy user hash 生成)、授权与令牌,适合基础设施即代码;另有 auth-access-cache(默认 false,内存 ACL 快照,官方标注 high-volume servers only)。

5.4 附件(attachment-cache-dir)

启用附件需两项:attachment-cache-dir(存储目录,2.32 起也可填 S3 URL)与 base-url(生成下载 URL)。默认单文件 15M、总量 5G、过期 3h(实测 --help 回显):

attachment-cache-dir: /var/cache/ntfy/attachments
base-url: http://127.0.0.1:8144

实测上传附件(CLI --file):

echo 'test attachment content 2026-10-21' > /tmp/report.txt
ntfy publish --token tk_l8dn95bsajk63328gex7trg3817wq --file=/tmp/report.txt http://127.0.0.1:8144/phil-topic 'report attached'
{"id":"NazLJG4n7l7Q","time":1792589171,"expires":1792632371,"event":"message","topic":"phil-topic","message":"report attached","attachment":{"name":"report.txt","type":"text/plain; charset=utf-8","size":35,"expires":1792599971,"url":"http://127.0.0.1:8144/file/NazLJG4n7l7Q.txt"}}

attachment.url 由 base-url 拼出,下载验证内容一致:

curl -s http://127.0.0.1:8144/file/NazLJG4n7l7Q.txt
test attachment content 2026-10-21

超限消息自动转附件:message-size-limit 默认 4k,在启用附件的前提下发送 5001 字节正文,服务端不拒绝而是把正文转为附件(实测):

python3 -c "print('x'*5000)" | curl -s -H 'Authorization: Bearer tk_l8dn95bsajk63328gex7trg3817wq' --data-binary @- http://127.0.0.1:8144/phil-topic | head -c 220
{"id":"iLSkOiwZHASt","time":1792589218,"expires":1792632418,"event":"message","topic":"phil-topic","message":"You received a file: attachment.txt","attachment":{"name":"attachment.txt","type":"text/plain; charset=utf-8","size":5001,"expires":1792600018,"url":"http://127.0.0.1:8144/file/iLSkOiwZHASt.

💡 说明:未启用附件缓存时,超 4k 消息按官方口径返回错误(官方不建议调大 message-size-limit,FCM/APNS 载荷上限约 4K)。需要发长文本时优先「正文转附件」这条内建路径。

5.5 访客限流与防滥用(visitor-* 系列)

ntfy 的限流对象是「访客(visitor)」= 单个 IP(或按前缀聚合的子网)。核心参数(默认值取自实测 serve --help 与 server.yml 注释):

参数默认值含义
visitor-request-limit-burst60每访客请求令牌桶初始容量
visitor-request-limit-replenish5s桶补充速率(每 5s 回 1 个令牌)
visitor-message-daily-limit0(不启用)每访客每日消息硬上限
visitor-subscription-limit30每访客订阅数
visitor-attachment-total-size-limit100M每访客附件存储配额
visitor-attachment-daily-bandwidth-limit500M每访客附件+回放日流量
visitor-email-limit-burst / replenish16 / 1h每访客邮件转发配额
visitor-topic-creation-limit-burst / replenish100 / 1m新主题创建速率(防主题枚举/抢占)
visitor-prefix-bits-ipv4 / ipv632 / 64限流聚合前缀(32=按完整 IP)
global-topic-limit15000全服务器主题总数上限
behind-proxyfalse反代后必须开启,从 X-Forwarded-For 取真实访客 IP

实测限流收紧触发:临时配 visitor-request-limit-burst: 5、visitor-request-limit-replenish: "1m" 重启,连续发布 7 条:

for i in 1 2 3 4 5 6 7; do curl -s -o /dev/null -w "%{http_code}\n" -u phil:q1w2e3r4 -d "flood $i" http://127.0.0.1:8144/phil-topic; done

实测输出:

200
200
200
200
200
429
429

第 6 条起被拒,完整响应体(ntfy 层限流码 42901):

{"code":42901,"http":429,"error":"limit reached: too many requests","link":"https://ntfy.sh/docs/publish/#limitations"}

ban-file——把访客的 403/429 类拒绝按固定格式追加到文件(每行 <RFC3339-UTC时间> <ip> <掩码前缀> <http码> <ntfy码>),供 fail2ban 直接 tail 封禁;ban-weights 可对不同 ntfy 码配权重(如 42909:10 认证爆破快速封禁、42908:0 正常配额豁免)。该体系为 2.3x 新增防滥用机制,参数细节见 server.yml 注释与官方文档。

5.6 邮件与电话(smtp-* / twilio-*,未实测组)

本组参数依赖外部 SMTP/Twilio 服务,本文环境未配置,以下为参数含义与默认口径(来源:server.yml 内置注释与 docs.ntfy.sh/docs/config,2026-10-21 核对;使用时按官方文档接线):

  • 邮件发布入口:smtp-server-listen(如 :25)+ smtp-server-domain(如 ntfy.example.com)+ 可选 smtp-server-addr-prefix(防垃圾:只收 ntfy-<topic>@域名)。启用后向 <topic>@<域名> 发邮件即发布消息;
  • 邮件转发出口:smtp-sender-addr(SMTP 服务器)、smtp-sender-from(发件人)、smtp-sender-user/pass(明文认证或 STARTTLS)、smtp-sender-verify(收件人验证开关)。启用后消息带 X-Email 头即同时发邮件;配额走 visitor-email-limit-*;
  • 语音外呼:twilio-account/auth-token/phone-number(+ 可选 twilio-verify-service、twilio-call-format)。启用后消息带 X-Call 头触发电话播报;日配额 ntfy 码 42910。

5.7 推送上游(firebase / upstream / web-push)

  • firebase-key-file:配置 FCM 凭据 JSON 后,消息同步投递到 FCM 供 Android App 省电接收(自托管可选,不配则 Android 走长连接);
  • upstream-base-url:自托管服务器的 iOS 及时推送必需——填 https://ntfy.sh,本服务器只向官方发「轮询请求」(仅含消息 ID,不含内容),iOS App 收到 APNS 唤醒后回源拉取(server.yml 注释原文:「If you use the iOS ntfy app, you MUST configure this to receive timely notifications」);
  • Web Push:web-push-public-key/web-push-private-key(ntfy webpush keys 生成 VAPID 密钥对)+ web-push-file(订阅登记库,PostgreSQL 模式免配)+ web-push-email-address(向推送商报备的联系方式)。启用后 Web App 关闭也能收推送;默认告警时长 55d 警告、60d 过期。

5.8 Web App 与可观测开关(web-root / enable-* / metrics)

参数默认说明
web-root/Web App 根路径;/app、/ntfy 可改路径,disable 整体关闭
enable-signupfalse允许自助注册(开启需谨慎)
enable-loginfalse允许 Web App/API 登录(启用认证后建议开)
require-loginfalseWeb App 未登录强制跳登录页
enable-reservationsfalse允许用户预留主题(tier 配额内)
enable-metricsfalse暴露 /metrics(Prometheus 格式)
metrics-listen-http空独立 metrics 监听端口(隐式启用 metrics,主端口不再暴露)
profile-listen-http空Go pprof 调试端点(排障临时用)

实测 enable-metrics:默认关闭时 /metrics 404:

curl -sI http://127.0.0.1:8144/metrics | head -1
HTTP/1.1 404 Not Found

追加 enable-metrics: true 重启后返回 Prometheus 文本:

curl -s http://127.0.0.1:8144/metrics | head -8
# HELP go_gc_duration_seconds A summary of the wall-time pause (stop-the-world) duration in garbage collection cycles.
# TYPE go_gc_duration_seconds summary
go_gc_duration_seconds{quantile="0"} 8.011e-05
go_gc_duration_seconds{quantile="0.25"} 8.3818e-05
go_gc_duration_seconds{quantile="0.5"} 0.000135739
go_gc_duration_seconds{quantile="0.75"} 0.000170899
go_gc_duration_seconds{quantile="1"} 0.000170899
go_gc_duration_seconds_sum 0.000470566

⚠️ 注意:官方注释明示「Metrics may be considered sensitive information」——对外暴露前用 metrics-listen-http 把它挪到独立内网端口,或在反代层加访问控制。

5.9 日志(log-level / log-format / log-file)

默认 INFO 文本格式输出到 stderr(journalctl 可见)。实测切 JSON:

echo 'log-format: json' >> /etc/ntfy/server.yml && sudo systemctl restart ntfy
journalctl -u ntfy --no-pager -n 1 | tail -1
Oct 21 13:38:17 12c75995d5e1 ntfy[5020]: {"time":"2026-10-21T13:38:17.081Z","level":"INFO","message":"Listening on :8144[http], ntfy 2.32.0, log level is INFO","tag":"startup"}

要点:

  • log-level 五级 trace/debug/info/warn/error(默认 INFO;debug/trace 官方警告 very chatty,临时用);
  • log-file 落盘指定文件(注意 chown ntfy:ntfy);
  • log-level-overrides 按字段热调级(如 "visitor_ip=1.2.3.4 -> debug" 只对单访客开 debug),有性能开销,官方建议仅临时排障;
  • log-level 与 log-level-overrides 支持 systemctl reload ntfy 热加载,其余参数必须 restart。

六、生产配置模板

以下模板面向「4GB 内存、40GB+ 磁盘、个人/小团队告警中枢」的 Ubuntu 22.04 服务器场景(自建反代 HTTPS 由外部 Nginx/Caddy 承担,ntfy 只监听本机回环),可直接作为 /etc/ntfy/server.yml 的实际值段落:

# --- 监听:只听本机,对外走反代 ---
listen-http: "127.0.0.1:8144"
base-url: "https://ntfy.example.com"

# --- 消息缓存:持久化 + 保留 3 天 ---
cache-file: /var/cache/ntfy/cache.db
cache-duration: "72h"

# --- 认证:全收紧 ---
auth-file: /var/lib/ntfy/user.db
auth-default-access: deny-all
enable-login: true

# --- 附件:个人场景 2G 够用 ---
attachment-cache-dir: /var/cache/ntfy/attachments
attachment-file-size-limit: "15M"
attachment-total-size-limit: "2G"
attachment-expiry-duration: "12h"

# --- 限流:小团队宽松够用,防脚本失控 ---
visitor-request-limit-burst: 60
visitor-request-limit-replenish: "5s"
visitor-message-daily-limit: 0

# --- iOS 用户必备 ---
upstream-base-url: "https://ntfy.sh"

# --- 可观测 ---
enable-metrics: true

# --- 日志:文本 INFO 即可,接日志平台时切 json ---
log-level: info
log-format: text

逐项说明与内存/磁盘口径:

  • listen-http 绑 127.0.0.1:配合反代终结 TLS,ntfy 进程不直接暴露公网;无反代的裸机场景改回 ":8144" 并防火墙放行;
  • cache-duration: 72h:消息缓存 SQLite 单文件,按每条消息 <1KB 估算,10 万条/天 × 3 天 ≈ 300MB 量级磁盘,4GB 内存服务器无压力;
  • auth-default-access: deny-all + enable-login:先收紧再放行(ntfy user/ntfy access 逐个授权),Web App 可登录管理;
  • 附件三限按「告警截图为主」给 15M/2G/12h:磁盘紧张就砍总量;
  • upstream-base-url:有 iOS 用户必须配(见 5.7 节);纯 Android/CLI 团队可删;
  • enable-metrics:接 Prometheus 时保留,注意 5.8 节的暴露面提醒。

修改后统一验证:

sudo systemctl restart ntfy && systemctl is-active ntfy && curl -s http://127.0.0.1:8144/v1/health
active
{"healthy":true,"role":"active"}

七、常见问题(FAQ)

Q1:改了 server.yml 不生效? 三步排查:① 是否 systemctl restart ntfy(仅 log-level/overrides 支持 reload 热加载);② 语法是否正确——YAML 冒号后要有空格,且该行没有行首空格错位;③ 是否被 systemd unit 的 ExecStart 参数或 $NTFY_* 环境变量覆盖(见 3.2 节,CLI 优先级最高)。

Q2:启用 auth-file 后启动报 user database directory ... does not exist? 目录缺失或属主不对:sudo mkdir -p /var/lib/ntfy && sudo chown ntfy:ntfy /var/lib/ntfy 后重启(二进制安装方式的典型坑,apt 包已预建目录,实录见《Ubuntu 22.04 安装 ntfy 2.32.0 指南》5.5 节)。

Q3:发布返回 40301 与 40101 有什么区别? 40101 unauthorized = 凭据本身错误(没带 token、密码错);40301 forbidden = 认证通过但该身份对该主题无权限。前者查 token/密码,后者查 ntfy access 授权与 auth-default-access(实测对照见 5.3 节)。

Q4:消息为什么会变成附件? 正文超过 message-size-limit(默认 4k)且已启用附件缓存时,服务端把正文自动转存为 attachment.txt 并在通知里附下载 URL(实测见 5.4 节)。不想要该行为就控制正文长度,或接受附件化。

Q5:cache-duration 调小后附件丢了? 附件存活时间不能超过消息缓存时长(实测 WARN:attachment-expiry-duration (3h) is longer than cache-duration (1m); attachments ... will be deleted after 1m)。二者联动调整,保持 attachment-expiry-duration ≤ cache-duration 或接受附件随消息删。

Q6:限流 42901 误伤了内部监控机怎么办? 用 visitor-request-limit-exempt-hosts(逗号分隔 IP/网段/主机名,启动时解析)把内网监控源加入豁免;或调大 burst/replenish。若服务在反代之后,务必先配 behind-proxy: true,否则全体访客共享反代 IP 的限流桶,一激灵全 429。

Q7:怎么把配置迁到 PostgreSQL? 2.32 起设 database-url 即整体切换(消息/用户/Web Push 三库合一,隐式启用认证),同时必须删除 cache-file、auth-file、web-push-file(互斥),细节按 server.yml 注释与官方文档操作。本文以 SQLite 路线实测为准。

八、参考链接