安装 NaiveProxy
确保你已经安装了 NaiveProxy,可以通过以下命令安装:
npm install naive-proxy
生成配置文件
运行以下命令生成 NaiveProxy 的配置文件:
npx naive-proxy generate
这会在当前目录下生成一个 naive-proxy.json 文件,内容如下:
{
"httpProxy": {
"enabled": true,
"listen": "...",
"port": 8888
},
"httpsProxy": {
"enabled": true,
"listen": "...",
"port": 8443,
"certFile": "cert.pem",
"keyFile": "key.pem"
},
"forward": {
"enabled": true,
"fromProxy": true,
"toProxy": true
}
}
修改配置文件(可选)
你可以根据需要修改配置文件中的选项:
-
httpProxy:enabled: 是否开启 HTTP 代理(默认true)listen: 代理监听地址(默认 )port: 代理端口(默认8888)
-
httpsProxy:enabled: 是否开启 HTTPS 代理(默认true)listen: 代理监听地址(默认 )port: 代理端口(默认8443)certFile: 证书文件路径(默认cert.pem)keyFile: 秘钥文件路径(默认key.pem)
-
forward:enabled: 是否开启转发功能(默认true)fromProxy: 是否从代理转发(默认true)toProxy: 是否转发到代理(默认true)
设置环境变量(可选)
如果需要,可以在项目根目录下创建 .env 文件,指定 NaiveProxy 的配置:
NAIVE_PROXY_CONFIG=../naive-proxy.json
启动 NaiveProxy
运行以下命令启动 NaiveProxy:
npx naive-proxy
配置代码中的代理(可选)
在你的代码中,可以通过环境变量指定代理地址:
process.env.NAIVE_PROXY = 'http://localhost:8888'; // 或 HTTPS
示例用法
假设你已经配置好了 NaiveProxy,可以通过浏览器或 curl 测试:
curl -v http://localhost:8888
注意事项
- 确保 NaiveProxy 和目标服务器之间没有防火墙阻止。
- 如果需要 HTTPS 代理,可以自行生成证书(例如使用
openssl工具):openssl req -x509 -newkey -days 365 -keyopt rsa:2048 -out cert.pem -outform PEM openssl rsa -in key.pem -outform PEM
如果有其他问题,可以查看 NaiveProxy 的 GitHub 仓库 或者文档。




