Featured image of post Windows 自托管服务实战:Nginx、Caddy、Redis、MinIO、Elasticsearch 与 WinSW 注册服务

Windows 自托管服务实战:Nginx、Caddy、Redis、MinIO、Elasticsearch 与 WinSW 注册服务

在 Windows 上跑 Web/缓存/对象存储/搜索——Nginx 1.24/1.27 反代 + Caddy 自动 HTTPS + Redis 7.4 + MinIO RELEASES 2023 + Elasticsearch 8 + WinSW 把 exe 注册成系统服务

一、为什么要在 Windows 上自托管服务

Linux 是服务器的主流,但以下场景 Windows 是更好的选择:

  • 本机开发调试:不想用 Docker 跑 Redis / MinIO,直接在 Windows 起一个更轻;
  • 边缘设备:工控机、收银终端、POS 机多数是 Windows;
  • 客户现场:客户系统是 Windows Server,部署要符合现场环境;
  • 个人 NAS / 家用服务器:Windows 桌面系统易用,存储/相册/笔记都靠它。

本文给出 Nginx / Caddy / Redis / MinIO / Elasticsearch 五个常用自托管服务的 Windows 绿色版部署方案,统一用 WinSW 把 exe 注册成系统服务,重启自启。

二、Nginx for Windows

2.1 下载与目录

nginx.org/en/download.html 下载 nginx-1.24.0.zip(mainline 1.25.x 也可以)。

1
2
3
4
5
6
7
D:\portable\dev\nginx-1.24.0\
├── conf\
│   ├── nginx.conf
│   └── conf.d\        # 自定义配置
├── html\
├── logs\
└── nginx.exe

2.2 配置环境变量

1
PATH=D:\portable\dev\nginx-1.24.0
1
2
nginx -v
# nginx version: nginx/1.24.0

2.3 启停命令

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 启动(前台)
start nginx

# 优雅停止
nginx -s stop
nginx -s quit
nginx -s reload       # 重载配置(不中断连接)
nginx -s reopen       # 重开日志文件

# 暴力杀进程(兜底)
taskkill /f /t /im nginx.exe

2.4 反代示例

conf/conf.d/frontend.conf

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
server {
    listen 8080;
    server_name localhost;

    # 静态站点
    root D:/wwwroot/site;
    index index.html;

    # 反向代理
    location /api/ {
        proxy_pass http://127.0.0.1:3000/;
        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;
    }

    # WebSocket
    location /ws/ {
        proxy_pass http://127.0.0.1:3001/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

    # 大文件上传
    client_max_body_size 100m;
}

conf/nginx.conf 引入:

1
2
3
4
5
6
7
8
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    gzip  on;
    include conf.d/*.conf;
}

2.5 TCP/UDP 四层代理

conf/stream/video.conf

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
stream {
    server {
        listen 1935;
        proxy_pass stream_backend;
    }
    upstream stream_backend {
        server 127.0.0.1:1936;
        server 127.0.0.1:1937;
    }
}

启动时 nginx 会要求主 nginx.conf 引入 stream 块,默认 nginx.conf 不带,要手动加。

三、Caddy:自动 HTTPS 的 HTTP/2 服务器

3.1 下载

caddyserver/caddy/releases 下载 caddy_2.6.4_windows_amd64.zip,解压到 D:\portable\dev\caddy

3.2 Caddyfile 最小配置

1
2
3
4
5
6
example.com {
    root * D:/wwwroot/site
    file_server
    encode gzip
    reverse_proxy /api/* 127.0.0.1:3000
}

3.3 关键命令

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 启动(前台,监视配置改动自动 reload)
.\caddy.exe run --watch

# 启动(守护进程)
.\caddy.exe start

# 格式化配置文件(推荐编辑后立即 fmt)
.\caddy.exe fmt --overwrite Caddyfile

# 自签证书测试
curl https://localhost --ssl-no-revoke

3.4 Caddy vs Nginx

维度CaddyNginx
自动 HTTPS默认开(Let’s Encrypt)需 acme.sh + cron
配置语法极简模块化、灵活
性能单核略低于 Nginx极致优化
生态较新,模块少老牌,几乎所有场景都有现成配置
适合个人/小团队博客、内网工具生产大流量反代、网关

四、Redis for Windows

4.1 下载

官方 Redis 不提供 Windows 版。社区维护两个分支:

下载 Redis-7.4.1-Windows-x64-cygwin-with-Service.zip,解压到 D:\portable\dev\redis

4.2 启动

1
2
3
4
5
6
7
# 启动 redis-server
D:\portable\dev\redis\redis-server.exe redis.conf

# 客户端
D:\portable\dev\redis\redis-cli.exe
127.0.0.1:6379> ping
PONG

4.3 配置 redis.conf 关键项

1
2
3
4
5
6
7
8
9
bind 127.0.0.1
port 6379
daemonize no          # Windows 不支持 daemonize
loglevel notice
logfile "D:/portable/dev/redis/logs/redis.log"
dir D:/portable/dev/redis/data
maxmemory 256mb
maxmemory-policy allkeys-lru
requirepass {{REDACTED}}

4.4 用 Redis Desktop Manager 连

RDM(uglide/RedisDesktopManager)2021.3 起开源。Windows 端推荐用 Another Redis Desktop Manager,绿色解压即用。

1
2
3
host: 127.0.0.1
port: 6379
password: <requirepass>

五、MinIO:对象存储

5.1 下载

dl.minio.io + dl.min.io/client/mc/release/windows-amd64/mc.exe,放到 D:\portable\dev\minio\

5.2 目录

1
2
3
4
D:\portable\dev\minio\
├── data\              # 对象数据
├── minio.exe
└── minio.bat          # 启动脚本

minio.bat

1
2
3
4
5
@echo off
set path=D:\portable\dev\minio
set minPath=D:\portable\dev\minio\data
"%path%\minio.exe" server %minPath% --console-address ":9000" --address ":9090"
pause

5.3 启动

1
2
3
4
5
.\minio.bat
# 输出:
# API: http://127.0.0.1:9090
# WebUI: http://127.0.0.1:9000
# 默认账号: minioadmin / minioadmin   ← 首次启动后立即改密

5.4 mc 客户端

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# 配置 host
.\mc.exe config host add local http://127.0.0.1:9090 minioadmin minioadmin

# 列 bucket
.\mc.exe ls local/

# 创建 bucket
.\mc.exe mb local/test-bucket

# 上传文件
.\mc.exe cp D:/test.zip local/test-bucket/

# 列已配 host
.\mc.exe config host list

安全提示minioadmin/minioadmin 是默认账号,生产环境必须通过 MINIO_ROOT_USER / MINIO_ROOT_PASSWORD 环境变量或 config.json 修改。

六、Elasticsearch

6.1 下载

elastic.co/downloads/past-releases#elasticsearch 选 8.x zip。

6.2 启动

1
2
cd D:\portable\dev\elasticsearch-8.11.0
bin\elasticsearch.bat

6.3 关键配置

config/elasticsearch.yml

1
2
3
4
5
6
cluster.name: dev-cluster
node.name: dev-node-1
network.host: 127.0.0.1
http.port: 9200
discovery.type: single-node
xpack.security.enabled: false

config/jvm.options

1
2
-Xms2g
-Xmx2g

避坑:Windows 上 ES 跑 2GB 堆需要至少 4GB 物理内存,老机器慎用。

6.4 Kibana 配套

KIMB elastic.co/downloads/kibana 下 Windows zip,启动后访问 http://127.0.0.1:5601,Dev Tools Console 是排查 ES 最方便的工具。

七、WinSW:把 exe 注册成系统服务

7.1 简介

winsw/winsw 是 Windows Service Wrapper,可以把任何可执行文件注册成 Windows 系统服务(支持开机自启、失败重启、日志轮转)。

下载 WinSW-x64.exe(注意 64/32 位),改名成与 XML 同名。

7.2 XML 配置

WinSW-x64.exe 复制到 D:\portable\dev\nginx-1.24.0\nginx-service.exe,新建 nginx-service.xml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
<service>
  <id>nginx-server</id>
  <name>nginx-server</name>
  <description>Nginx 1.24 反向代理服务</description>
  <executable>D:\portable\dev\nginx-1.24.0\nginx.exe</executable>
  <startarguments>-p D:\portable\dev\nginx-1.24.0\</startarguments>
  <stopexecutable>D:\portable\dev\nginx-1.24.0\nginx.exe</stopexecutable>
  <stoparguments>-p D:\portable\dev\nginx-1.24.0\ -s stop</stoparguments>
  <logpath>%BASE%\logs</logpath>
  <log mode="rotate">
    <sizeThreshold>10240</sizeThreshold>
    <keepFiles>3</keepFiles>
  </log>
</service>

7.3 安装 / 启动 / 卸载

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# 安装服务(管理员 cmd)
nginx-service.exe install

# 启动
nginx-service.exe start
nginx-service.exe stop
nginx-service.exe restart
nginx-service.exe status

# 卸载
nginx-service.exe uninstall

# 不重装刷新配置
nginx-service.exe refresh

7.4 日志按时间轮转

1
2
3
4
5
6
<log mode="roll-by-size-time">
  <sizeThreshold>10240</sizeThreshold>
  <pattern>yyyyMMdd</pattern>
  <autoRollAtTime>00:00:00</autoRollAtTime>
  <zipOlderThanNumDays>5</zipOlderThanNumDays>
</log>

7.5 实战:MinIO 服务化

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<service>
    <id>minio-server</id>
    <name>minio-server</name>
    <description>MinIO 对象存储服务</description>
    <env name="HOME" value="%BASE%"/>
    <env name="MINIO_ROOT_USER" value="root"/>
    <env name="MINIO_ROOT_PASSWORD" value="{{REDACTED}}"/>
    <executable>%BASE%\minio.exe</executable>
    <arguments>server "%BASE%\data" --console-address ":9090" --address ":9091"</arguments>
    <logpath>%BASE%\logs</logpath>
    <log mode="roll-by-size-time">
      <sizeThreshold>10240</sizeThreshold>
      <pattern>yyyyMMdd</pattern>
    </log>
</service>

7.6 WinSW 完整命令表

命令用途
install安装服务
uninstall卸载服务
start启动
stop停止
restart重启
status查看状态
refresh刷新服务属性(不重装)
customize自定义包装可执行文件
dev ps显示服务进程树
dev kill强制杀掉服务进程
dev list列出本 exe 注册的所有服务

八、把它们装在一起的服务清单

1
2
3
4
5
6
7
8
D:\portable\services\
├── nginx\           # 端口 80 / 8080
├── caddy\           # 端口 443(自动 HTTPS)
├── redis\           # 端口 6379
├── minio\           # 端口 9090 (API) / 9000 (WebUI)
├── elasticsearch\   # 端口 9200
├── kibana\          # 端口 5601
└── nssm\            # 备用服务化工具

每个目录下放 <服务名>.exe(WinSW 改名)和 <服务名>.xml,统一 install 即可。

九、常见坑与对策

现象原因对策
Nginx 启动 403 ForbiddenSELinux / 文件权限Windows 上多数是路径写错
Caddy 启动报 “bind: address already in use”端口被占`netstat -ano
Redis 启动报 “Bad file format”Windows redis 版本错装 Cygwin 版(tporadowski)
MinIO 控制台打不开防火墙首次启动后改 minioadmin 密码
ES 启动 OOMJVM 堆-Xms1g -Xmx1g 调小
WinSW 服务起不来不是管理员用管理员 cmd

十、下一步

  • Caddy 进阶:用 Cloudflare DNS plugin 自动签通配符证书
  • MinIO 进阶:搭建 4 节点分布式集群(参考 2022-09-15《MinIO 对象存储实战》)
  • Redis 进阶:Redis Sentinel 高可用、Redis Cluster 集群
  • ES 进阶:用 Docker Compose 拉起 ES + Kibana 全套(参考 2025-03-15《Prometheus 监控告警体系》)

十一、参考资料

使用 Hugo 构建
主题 StackJimmy 设计