Appearance
Nginx 相关配置
使用 Nginx 处理 HTTPS,并把页面和 API 请求统一反向代理给 Node 服务。
推荐方案
推荐使用“全部反代 Node”:
text
浏览器
-> HTTPS 访问 Nginx
-> Nginx 反代到 127.0.0.1:3000
-> Node 同时处理静态页面和 /api/这种方式不需要配置 Nginx root,也不容易把源码目录暴露出去。
HTTP 跳转 HTTPS
nginx
server {
listen 80;
server_name example.com www.example.com;
return 301 https://$host$request_uri;
}HTTPS 反代 Node
新版 Nginx 推荐这样写 HTTP/2:
nginx
server {
listen 443 ssl;
http2 on;
server_name example.com www.example.com;
ssl_certificate /path/to/fullchain.pem;
ssl_certificate_key /path/to/privkey.pem;
location / {
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 https;
}
}如果你写的是:
nginx
listen 443 ssl http2;在新版 Nginx 中可能会被编辑器标红或提示过时。只要 nginx -t 通过,配置语法就是可用的;更推荐改成 listen 443 ssl; 加 http2 on;。
检查并重载
bash
sudo nginx -t
sudo systemctl reload nginx不要暴露项目根目录
不要这样配置:
nginx
root /var/www/example.com;这可能暴露源码、配置和本地文件。
如果你选择静态文件方案,root 只能指向:
nginx
root /var/www/example.com/.vitepress/dist;并且 /api/ 仍然要反代到 Node。
验证微信回调
手动访问没有微信签名,返回 403 是正常的:
bash
curl -i https://example.com/api/wechat/callback如果微信后台提交服务器配置成功,说明 GET /api/wechat/callback 的签名校验已经通过。
查看微信是否真的把消息推给服务器:
bash
sudo tail -f /var/log/nginx/access.log给公众号发送验证码后,应该看到类似:
text
POST /api/wechat/callback?... HTTP/1.1" 200