在腾讯云轻量应用服务器(Lighthouse)上部署后端服务后,微信小程序要安全、稳定地连接该后端,需满足微信的网络与安全规范。以下是完整、可落地的操作指南(含关键注意事项和常见坑):
✅ 一、前提条件(必须满足!)
| 微信小程序强制要求 HTTPS,且域名需在后台配置白名单: | 项目 | 要求 |
|---|---|---|
| 协议 | 必须使用 https://(HTTP 会被拦截) |
|
| 域名 | 必须是备案过的真实域名(不能用 IP 或 localhost) | |
| SSL 证书 | 必须有有效 HTTPS 证书(推荐免费 Let’s Encrypt) | |
| 服务器端口 | 建议使用标准 HTTPS 端口 443(避免非标端口被防火墙/运营商拦截) |
⚠️ 注意:
- ❌ 轻量服务器的公网 IP(如
118.25.xxx.xxx)无法直接用于wx.request;- ❌
http://118.25.xxx.xxx:3000或http://your-domain.com:8080均会失败;- ✅ 正确方式:
https://api.yourdomain.com(域名 + 443 端口,隐式)
✅ 二、部署步骤详解
🔹 步骤 1:绑定并备案域名(腾讯云内完成)
- 在腾讯云 DNS 解析控制台 添加 A 记录:
api.yourdomain.com→ 指向你的轻量服务器公网 IP - 确保域名已完成 ICP 备案(大陆服务器必需!未备案域名即使解析成功也无法访问)
🔹 步骤 2:在轻量服务器上部署后端(以 Node.js Express 为例)
# 登录轻量服务器(SSH)
ssh root@your-server-ip
# 安装 Node.js(示例)
curl -fsSL https://deb.nodesource.com/setup_lts.x | bash
apt-get install -y nodejs
# 创建后端目录 & 启动服务(监听 0.0.0.0:3000)
mkdir /opt/my-api && cd /opt/my-api
npm init -y
npm install express
# app.js 示例(注意:监听 0.0.0.0,而非 127.0.0.1)
const express = require('express');
const app = express();
app.use(express.json());
app.get('/test', (req, res) => res.json({ code: 0, msg: 'Hello from Tencent Lighthouse!' }));
app.listen(3000, '0.0.0.0', () => console.log('API running on port 3000'));
✅ 运行:node app.js &(或用 pm2 守护)
🔹 步骤 3:配置 Nginx 反向X_X(关键!实现 HTTPS + 端口隐藏)
✨ 作用:将
https://api.yourdomain.com的请求转发到http://127.0.0.1:3000,同时提供 HTTPS。
# 安装 Nginx(轻量服务器默认可能未安装)
apt update && apt install nginx -y
# 获取免费 SSL 证书(推荐 certbot)
apt install certbot python3-certbot-nginx -y
certbot --nginx -d api.yourdomain.com # 按提示输入邮箱,自动配置 HTTPS
✅ 编辑 Nginx 配置(/etc/nginx/sites-available/api.yourdomain.com):
server {
listen 443 ssl;
server_name api.yourdomain.com;
ssl_certificate /etc/letsencrypt/live/api.yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/api.yourdomain.com/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 $scheme;
}
}
# HTTP 自动跳转 HTTPS(可选但推荐)
server {
listen 80;
server_name api.yourdomain.com;
return 301 https://$server_name$request_uri;
}
✅ 重启 Nginx:
nginx -t && systemctl reload nginx
🔹 步骤 4:配置腾讯云安全组(开放必要端口)
| 进入轻量服务器控制台 → 安全组 → 编辑规则: | 方向 | 协议类型 | 端口范围 | 授权对象 | 说明 |
|---|---|---|---|---|---|
| 入站 | TCP | 443 |
0.0.0.0/0 |
✅ HTTPS 必开 | |
| 入站 | TCP | 80 |
0.0.0.0/0 |
✅ certbot 验证及跳转所需 | |
| ❌ 入站 | TCP | 3000 |
— | 禁止开放! 后端应仅限内网访问(Nginx X_X即可) |
💡 提示:不要开放 3000 端口给公网!否则存在安全风险。
🔹 步骤 5:微信小程序端调用(wx.request)
在小程序代码中(如 pages/index/index.js):
wx.request({
url: 'https://api.yourdomain.com/test', // ✅ 必须 https + 备案域名
method: 'GET',
success(res) {
console.log('API Success:', res.data); // {code: 0, msg: "Hello..."}
},
fail(err) {
console.error('API Failed:', err);
}
});
🔹 步骤 6:小程序后台配置合法域名(重中之重!)
登录 微信公众平台 → 开发管理 → 开发设置 → 服务器域名 → 填写:
- ✅ request 合法域名:
https://api.yourdomain.com
(⚠️ 不带路径、不带端口、必须是 HTTPS)
📌 注意:
- 每次修改需 保存 + 重新上传小程序 才生效;
- 域名需与 Nginx 中
server_name严格一致(大小写不敏感,但通配符*.xxx.com不支持);- 本地开发时可在开发者工具勾选「不校验合法域名」临时调试(上线前必须关闭!)。
✅ 三、排错清单(高频问题速查)
| 现象 | 可能原因 | 解决方案 |
|---|---|---|
request:fail net::ERR_CONNECTION_REFUSED |
后端没启动 / Nginx 未转发 / 安全组未开 443 | curl -I https://api.yourdomain.com 测试;检查 systemctl status nginx 和 netstat -tuln | grep :3000 |
request:fail url not in domain list |
小程序后台未配置域名 / 配置错误 / 未保存 | 重新检查【服务器域名】设置,确认无空格、协议正确 |
request:fail invalid url |
URL 写成 http://... 或含端口(如 :3000) |
改为 https://api.yourdomain.com/path |
SSL handshake failed |
证书过期 / 域名不匹配 / 未正确安装证书 | openssl s_client -connect api.yourdomain.com:443 -servername api.yourdomain.com 检查 |
| 接口返回 502 Bad Gateway | Nginx 无法连通后端(端口错/后端挂了/防火墙拦截) | curl http://127.0.0.1:3000/test 在服务器本地测试 |
✅ 四、进阶建议(生产环境)
- ✅ 使用
PM2管理 Node.js 进程:pm2 start app.js --name "my-api" - ✅ 添加日志:
pm2 start app.js --name "my-api" --log-date-format "YYYY-MM-DD HH:mm:ss" - ✅ 启用 CORS(后端加中间件):
app.use((req, res, next) => { res.header('Access-Control-Allow-Origin', '*'); // 小程序无需跨域,但调试时有用 res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE'); next(); }); - ✅ 使用腾讯云 API 网关(可选):统一鉴权、限流、监控,适合多服务场景。
✅ 总结一句话:
微信小程序 ←HTTPS→ 备案域名 ←Nginx反向X_X→ 轻量服务器内网后端(如 3000 端口)
只要严格遵循「域名备案 + HTTPS + 白名单配置」三原则,即可稳定连接。
需要我帮你:
- ✅ 生成完整的 Nginx 配置模板?
- ✅ 写一个带 Token 鉴权的 Express 后端示例?
- ✅ 提供腾讯云轻量 + 微信小程序联调 checklist PDF?
欢迎随时告诉我 👇
云小栈