在云服务器上部署小程序(通常指微信小程序,但此处按通用 Web 前端 + Node.js 后端架构理解),Nginx 和 Node.js 需要协同工作:Nginx 作为反向X_X和静态资源服务器,Node.js 负责运行后端 API 服务。以下是标准配置方案:
一、整体架构
用户请求 → Nginx (80/443)
├─ 静态文件(HTML/CSS/JS/图片) → 直接返回
└─ API 请求 (/api/*) → 转发到 Node.js 进程(如 localhost:3000)
二、Node.js 后端配置示例
1. 安装依赖并启动服务
# 假设项目根目录为 /var/www/myapp
cd /var/www/myapp
npm install express
2. server.js 示例(Express 框架)
const express = require('express');
const app = express();
const PORT = 3000; // 内部端口,不对外暴露
// 中间件
app.use(express.json());
app.use('/api', require('./routes/api')); // 路由模块
// 健康检查
app.get('/health', (req, res) => {
res.json({ status: 'ok' });
});
app.listen(PORT, () => {
console.log(`Node.js server running on http://localhost:${PORT}`);
});
✅ 关键:Node.js 只监听
localhost:3000,不要绑定0.0.0.0,避免安全风险。
三、Nginx 配置示例
1. 创建配置文件(如 /etc/nginx/sites-available/myapp.conf)
server {
listen 80;
server_name your-domain.com www.your-domain.com; # 替换为你的域名
# 静态资源(前端构建产物,如 dist/ 或 build/)
location / {
root /var/www/myapp/dist; # 指向前端打包输出目录
try_files $uri $uri/ /index.html; # SPA 路由支持
expires 7d;
add_header Cache-Control "public, immutable";
}
# API 接口转发到 Node.js
location /api {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
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_cache_bypass $http_upgrade;
# 超时设置(根据业务调整)
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
}
# 可选:WebSocket 支持(如需实时通信)
location /ws {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
}
# 安全头
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
}
2. 启用配置并重启 Nginx
sudo ln -s /etc/nginx/sites-available/myapp.conf /etc/nginx/sites-enabled/
sudo nginx -t # 测试配置语法
sudo systemctl restart nginx
四、Node.js 进程管理(推荐 PM2)
使用 PM2 守护 Node 进程,实现自动重启、日志管理等:
# 安装 PM2
npm install -g pm2
# 启动应用
cd /var/www/myapp
pm2 start server.js --name myapp-api
# 开机自启
pm2 startup
pm2 save
🔒 建议将
server.js中的PORT改为环境变量:const PORT = process.env.PORT || 3000;并通过
.env或 systemd 控制。
五、HTTPS 配置(生产环境必需)
使用 Let’s Encrypt 免费证书:
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d your-domain.com -d www.your-domain.com
Certbot 会自动更新 Nginx 配置并启用 HTTPS。
六、常见注意事项
| 问题 | 解决方案 |
|---|---|
| 小程序无法访问 API | 检查 Nginx 的 proxy_pass 是否指向正确 IP/端口;确认防火墙放行 80/443 |
| 前端路由刷新 404 | 确保 try_files $uri $uri/ /index.html; 存在 |
| Node.js 崩溃后未恢复 | 必须用 PM2 或 systemd 守护进程 |
| 跨域问题 | 若前后端分离且同源(通过 Nginx X_X),通常无需 CORS;否则在 Express 中配置 cors 中间件 |
| 微信回调地址失效 | 确保服务器域名已备案,且 HTTPS 有效;回调 URL 需与小程序后台一致 |
七、验证步骤
- 访问
http://your-domain.com→ 应加载前端页面 - 访问
http://your-domain.com/api/health→ 应返回{ "status": "ok" } - 使用浏览器开发者工具 Network 面板,确认 API 请求被 Nginx 转发而非直接访问 Node.js
- 模拟小程序调用(使用 Postman 或真机调试),验证完整链路
如需进一步支持(如 Docker 部署、CI/CD 流水线、数据库连接优化等),可补充具体技术栈(如 Vue/React、MySQL/MongoDB 等),我可提供定制化方案。
CLOUD云计算