安装NaiveProxy
在项目目录中安装NaiveProxy:
npm install naive-proxy
初始化配置文件
创建或编辑config.json:
{
"server": {
"bind": "...",
"port": 800
},
"reverse_proxy": [
{
"path": "/",
"target": "http://backend.com",
"method": ["GET", "POST"]
}
],
"routes": [
{
"method": ["GET"],
"path": "/api/v1/*",
"target": "http://backend2.com"
}
],
"middlewares": [
{
"name": "BasicAuth",
"secret": "your-basic-secret"
}
],
"secure": {
"enabled": true,
"server": {
"key": "your-ssl-key",
"cert": "your-ssl-cert"
}
},
"logs": {
"path": "./log",
"level": "info",
"request": {
"path": true,
"timestamp": true
}
},
"filters": [
{
"name": "add-access-control-allow-origin",
"value": "http://localhost:300"
}
],
"env": {
"NODE_ENV": "production"
}
}
启动NaiveProxy
在项目目录中运行:
naive-proxy start
测试配置
访问http://localhost:800,确认反向代理正常工作,检查日志和响应头是否正确。
使用示例
使用curl测试:
curl http://localhost:800/api/v1/test
部署注意事项
- 防火墙:确保800端口开放。
- 环境变量:根据环境调整配置,如
NODE_ENV为development或production。 - 负载均衡:在生产环境使用Nginx或其他负载均衡解决方案。
故障排除
- 配置错误:检查
config.json语法是否正确。 - 权限问题:确保NaiveProxy和后端服务有足够权限访问所需资源。
通过以上步骤,NaiveProxy可以作为简单而有效的反向代理,帮助你管理内部服务并安全地将它们暴露在外部。









