Syncthing 部署与配置指南

Syncthing 部署与配置指南

周四 6月 11 2026
569 字 · 4 分钟

封面

Syncthing 部署与配置指南

Syncthing 是一款开源的连续文件同步工具,P2P 架构,无需中心服务器。本文记录在云服务器上部署 Syncthing 并配置 Nginx 反代的全过程。

GUI 访问

GUI 默认监听 127.0.0.1:8384(仅本地)。不要改为 0.0.0.0——走 Nginx 反代更安全。

临时:SSH 隧道

BASH
ssh -L 8384:127.0.0.1:8384 root@服务器IP -N

浏览器 http://127.0.0.1:8384

长期:Nginx 反代 + HTTPS

BASH
sudo nano /etc/nginx/sites-available/syncthing
NGINX
server {
    listen 443 ssl;
    server_name sync.example.com;

    ssl_certificate     /etc/letsencrypt/live/sync.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/sync.example.com/privkey.pem;

    location / {
        proxy_pass http://127.0.0.1:8384;
        proxy_http_version 1.1;
        proxy_set_header Host 127.0.0.1:8384;
        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_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}
BASH
sudo ln -s /etc/nginx/sites-available/syncthing /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx

DNS 添加 A 记录 sync → 203.0.113.1,certbot 申请证书:

BASH
sudo certbot --nginx -d sync.example.com

踩坑:403 Forbidden

直接访问 127.0.0.1:8384 返回 200,但通过域名返回 403。两个原因叠加:

序号原因现象修复
1Certbot 生成的 server block 是静态文件服务器root + try_files),不是反代nginx 尝试从 /var/www/html 找文件,找不到返回 403改为 proxy_pass http://127.0.0.1:8384
2Syncthing 的 CSRF Host 校验拒绝域名头改了反代后依然 403proxy_set_header Host 127.0.0.1:8384(传 Syncthing 自己监听的地址)

关键:Host 头不能传 $host(域名),必须传 127.0.0.1:8384,Syncthing 才认。

同步端口

防火墙

BASH
sudo ufw allow 22000/tcp
sudo ufw allow 21027/udp

阿里云安全组

端口协议授权
22000TCP0.0.0.0/0
21027UDP0.0.0.0/0

监听地址

GUI 仅本地,同步端口监听所有接口:

XML
<!-- ~/.local/state/syncthing/config.xml -->
<gui enabled="true" tls="false">
    <address>127.0.0.1:8384</address>
</gui>
<options>
    <listenAddress>tcp://0.0.0.0:22000</listenAddress>
</options>

NAT 穿透验证

云服务器无 UPnP(Detected NAT services: count=0 正常)。开端口后确认直连:

BASH
# 看日志:不应再有 "Joined relay"
journalctl -u syncthing -f | grep -i relay
# 应出现 quic://公网IP:22000,不再走 relay

设置访问密码

XML
<gui enabled="true" tls="false">
    <address>127.0.0.1:8384</address>
    <user>admin</user>
    <password>example-password</password>
</gui>

密码会在下次启动时自动哈希。

开机自启

BASH
mkdir -p ~/.config/systemd/user
INI
# ~/.config/systemd/user/syncthing.service
[Unit]
Description=Syncthing - Open Source Continuous File Synchronization
Documentation=man:syncthing(1)
After=network.target

[Service]
ExecStart=/usr/local/bin/syncthing -no-browser -gui-address=127.0.0.1:8384 -logflags=0
Restart=on-failure
RestartSec=10
UMask=0027
Environment=STNORESTART=1

[Install]
WantedBy=default.target
BASH
systemctl --user daemon-reload
systemctl --user enable syncthing.service
systemctl --user start syncthing.service
systemctl --user status syncthing.service

诊断命令

BASH
# GUI 监听
ss -tlnp | grep 8384

# 同步端口
ss -tlnp | grep 22000

# 外部可达性
nc -zv 203.0.113.1 22000

# GUI 可达性
curl -s -o /dev/null -w "%{http_code}" http://127.0.0.1:8384
curl -s -o /dev/null -w "%{http_code}" https://sync.example.com

# 实时日志
journalctl -u syncthing -f

# 检查 relay 状态(无输出=直连成功)
journalctl -u syncthing --since "5 min ago" | grep "Joined relay"

Thanks for reading!

Syncthing 部署与配置指南

周四 6月 11 2026
569 字 · 4 分钟
-
-

© Nywerya | CC BY-NC-SA 4.0

Comments

cover

Ref:rain

Aimer