常用配置片段库
反向代理、systemd、防火墙、WireGuard 等 18 个可直接改用的片段。
Nginx 反向代理
nginx
server {
listen 443 ssl http2;
server_name app.example.com;
ssl_certificate /etc/ssl/fullchain.pem;
ssl_certificate_key /etc/ssl/privkey.pem;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_http_version 1.1;
proxy_set_header Host $host;
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_read_timeout 60s;
}
}WebSocket 升级
nginx
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
location /ws {
proxy_pass http://127.0.0.1:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}systemd 服务
ini
[Unit]
Description=My App
After=network-online.target
[Service]
User=app
WorkingDirectory=/srv/app
ExecStart=/srv/app/bin/server
Restart=on-failure
RestartSec=5
EnvironmentFile=/etc/app/env
NoNewPrivileges=true
[Install]
WantedBy=multi-user.targetsystemd 定时器
ini
[Unit]
Description=Backup timer
[Timer]
OnCalendar=*-*-* 03:30:00
Persistent=true
[Install]
WantedBy=timers.targetDocker Compose 基线
yaml
services:
app:
image: app:1.4.2
restart: unless-stopped
ports: ["127.0.0.1:8080:8080"]
volumes: ["./data:/data"]
env_file: [".env"]
healthcheck:
test: ["CMD", "curl", "-fsS", "http://127.0.0.1:8080/health"]
interval: 30s
timeout: 5s
retries: 3nftables 端口转发
bash
nft add table ip nat
nft add chain ip nat prerouting '{ type nat hook prerouting priority -100; }'
nft add rule ip nat prerouting iif "eth0" tcp dport 443 dnat to 192.168.10.20:443WireGuard 服务端
ini
[Interface]
Address = 10.8.0.1/24
ListenPort = 51820
PrivateKey = <server-private-key>
[Peer]
PublicKey = <client-public-key>
AllowedIPs = 10.8.0.2/32SSH 加固
text
PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes
MaxAuthTries 3
ClientAliveInterval 60
AllowUsers deployrsync 备份
bash
rsync -aHAX --delete --numeric-ids \
--exclude='.cache' \
/srv/data/ backup@192.168.10.30:/backup/data/ZFS 快照与发送
bash
zfs snapshot tank/data@2026-03-01
zfs send tank/data@2026-03-01 | ssh backup zfs receive backup/data
zfs list -t snapshot证书签发
bash
certbot certonly --webroot -w /var/www/html -d example.com
certbot renew --dry-run内核参数
bash
sysctl -w net.core.default_qdisc=fq
sysctl -w net.ipv4.tcp_congestion_control=bbr
sysctl -w net.ipv4.tcp_fastopen=3防火墙放行
bash
ufw default deny incoming
ufw allow from 192.168.10.0/24 to any port 22 proto tcp
ufw allow 443/tcp
ufw enableSamba 共享
ini
[share]
path = /srv/share
valid users = @family
read only = no
create mask = 0660
directory mask = 0770NFS 导出
text
/srv/share 192.168.20.0/24(rw,sync,no_subtree_check,root_squash)dnsmasq 上游
ini
server=223.5.5.5
server=/example.com/192.168.10.10
cache-size=1000日志轮转
text
/var/log/app/*.log {
daily
rotate 14
compress
missingok
notifempty
copytruncate
}片段要按环境改
示例中的地址、路径、账号都是占位值。直接复制前先替换,并确认端口没有被占用。
使用说明:速查内容按常见发行版与主流版本整理,命令参数以
man 与官方文档为准;涉及容量与功耗的数字为标称值换算,实际会有差异。