Linux下Node.js如何进行网络配置
导读:Linux下Node.js网络配置实用指南 一 代理配置 使用环境变量(最简便,适用于大多数HTTP/HTTPS客户端与npm) 在shell配置中导出变量(如:~/.bashrc、~/.zshrc 或 /etc/profile):...
Linux下Node.js网络配置实用指南
一 代理配置
- 使用环境变量(最简便,适用于大多数HTTP/HTTPS客户端与npm)
- 在shell配置中导出变量(如:~/.bashrc、~/.zshrc 或 /etc/profile):
- 示例:
- export HTTP_PROXY=http://proxyserver:port
- export HTTPS_PROXY=http://proxyserver:port
- export NO_PROXY=localhost,127.0.0.1
- 使配置生效:source ~/.bashrc(或 source /etc/profile)。Node.js 与 npm 会自动读取这些变量。
- 示例:
- 在shell配置中导出变量(如:~/.bashrc、~/.zshrc 或 /etc/profile):
- 在代码里为 http/https 请求显式指定代理(适用于精细控制)
- 示例(全局 Agent,注意不同版本/库的 API 差异):
- const https = require(‘https’); const HttpsProxyAgent = require(‘https-proxy-agent’); const agent = new HttpsProxyAgent(‘http://user:pass@proxyserver:port’); https.get(‘https://example.com’, { agent } , res => { /* … */ } );
- 如需 Proxy-Authorization,可按 Base64 编码放入请求头:
- headers: { ‘Proxy-Authorization’: 'Basic ’ + Buffer.from(‘user:pass’).toString(‘base64’) } 。
- 示例(全局 Agent,注意不同版本/库的 API 差异):
二 证书与 HTTPS 配置
- 信任自签名或私有 CA 证书
- 将 CA 证书加入 Node.js 的信任链(以 https 模块为例):
- const https = require(‘https’); const fs = require(‘fs’); const options = { hostname: ‘example.com’, port: 443, path: ‘/’, method: ‘GET’, ca: fs.readFileSync(‘/path/to/ca-cert.pem’, ‘utf8’) } ; https.request(options, res => { /* … */ } ).end();
- 将 CA 证书加入 Node.js 的信任链(以 https 模块为例):
- 双向 TLS(mTLS,客户端证书)
- 在请求选项中同时提供 key/cert:
- key: fs.readFileSync(‘/path/to/client-key.pem’, ‘utf8’),
- cert: fs.readFileSync(‘/path/to/client-cert.pem’, ‘utf8’)。
- 在请求选项中同时提供 key/cert:
三 域名与反向代理部署
- 使用 Nginx 作为反向代理(便于域名、端口统一、静态资源与压缩、负载均衡、HTTP/2 等)
- 安装与启用:
- sudo yum install -y nginx & & sudo systemctl start nginx & & sudo systemctl enable nginx
- 配置示例(/etc/nginx/conf.d/myapp.conf):
- server { listen 80; server_name example.com www.example.com; location / { proxy_pass http://localhost:3000; proxy_http_version 1.1; 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; } }
- 检查并重载:
- sudo nginx -t & & sudo systemctl reload nginx
- 安装与启用:
- 域名解析
- 在 DNS 提供商处为 example.com 添加 A 记录 指向服务器公网 IP。
四 系统层面网络优化
- 文件描述符限制
- 查看:ulimit -n
- 临时提升:ulimit -n 65535
- 永久提升:在 /etc/security/limits.conf 增加
-
- soft nofile 65535
-
- hard nofile 65535
-
- TCP/IP 内核参数(/etc/sysctl.conf)
- 示例:
- net.core.somaxconn = 65535
- net.ipv4.tcp_max_syn_backlog = 65535
- net.ipv4.ip_local_port_range = 1024 65535
- net.ipv4.tcp_tw_reuse = 1
- net.ipv4.tcp_fin_timeout = 30
- 使生效:sudo sysctl -p
- 示例:
- 应用层优化建议
- 启用 HTTP/2(TLS 下性能与多路复用更好)
- 使用 集群模式(cluster) 充分利用多核
- 静态资源与压缩交由 Nginx,Node.js 专注业务。
五 常见问题排查
- 代理不生效
- 确认变量名与大小写:HTTP_PROXY/HTTPS_PROXY/NO_PROXY;命令行与 systemd 服务环境是否一致(必要时在 service 文件中显式声明)。
- 证书校验失败
- 自签名证书需将 CA 加入 Node 的 ca 选项;或临时(仅测试)设置环境变量 NODE_TLS_REJECT_UNAUTHORIZED=0(生产禁用)。
- 端口与防火墙
- 确认 Node 监听 0.0.0.0(而非 127.0.0.1),并在云安全组/防火墙放行对应端口(如 80/443/3000)。
- 高并发连接异常
- 检查并提升 ulimit -n 与内核 somaxconn;按需开启 tcp_tw_reuse、优化 keepalive 与连接池。
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: Linux下Node.js如何进行网络配置
本文地址: https://pptw.com/jishu/766446.html
