温馨提示×

CentOS与Node.js的网络配置

小樊
36
2025-11-30 02:35:43
栏目: 编程语言

CentOS 与 Node.js 网络配置实战指南

一 系统网络配置

  • 使用 NetworkManager 的 nmcli 配置静态 IP(推荐)
    • 查看连接名:nmcli con show;假设为“eth0
    • 配置静态地址与 DNS:
      • sudo nmcli con mod “eth0” ipv4.method manual
      • sudo nmcli con mod “eth0” ipv4.addresses 192.168.1.100/24
      • sudo nmcli con mod “eth0” ipv4.gateway 192.168.1.1
      • sudo nmcli con mod “eth0” ipv4.dns “8.8.8.8 8.8.4.4
      • sudo nmcli con up “eth0
  • 传统 ifcfg 文件方式(CentOS 7)
    • 编辑文件:/etc/sysconfig/network-scripts/ifcfg-eth0
    • 关键参数:
      • BOOTPROTO=static,ONBOOT=yes
      • IPADDR=192.168.1.10,NETMASK=255.255.255.0,GATEWAY=192.168.1.1
      • DNS1=8.8.8.8,DNS2=8.8.4.4
    • 使配置生效:systemctl restart network 或 nmcli con reload
  • 验证
    • ip addr;ip route;cat /etc/resolv.conf;ping -c 4 8.8.8.8;nslookup example.com
  • 防火墙放行示例(firewalld)
    • 放行 HTTP/HTTPS:sudo firewall-cmd --permanent --add-service=http --add-service=https && sudo firewall-cmd --reload
    • 放行自定义端口(如 3000):sudo firewall-cmd --permanent --add-port=3000/tcp && sudo firewall-cmd --reload

二 Node.js 应用发布与反向代理

  • 以进程方式运行并放行端口
    • 启动:node app.js(监听 0.0.0.0:3000
    • 放行:firewall-cmd --permanent --add-port=3000/tcp && sudo firewall-cmd --reload
  • 使用 Nginx 反向代理(推荐)
    • 安装与启动: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://127.0.0.1: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;
          • proxy_set_header Upgrade $http_upgrade;
          • proxy_set_header Connection “Upgrade”;
        • }
      • }
    • 检查并重载:sudo nginx -t && sudo systemctl reload nginx
  • 可选:启用 HTTPS/HTTP/2(由 Nginx 终止 TLS)
    • 使用 certbot 获取证书后,监听 443 ssl http2 并配置证书路径,反向代理到 http://127.0.0.1:3000

三 连接与安全加固

  • 文件描述符与内核网络参数(高并发场景)
    • 文件描述符限制
      • 查看:ulimit -n;临时提升:ulimit -n 65535
      • 永久生效:在 /etc/security/limits.conf 增加
          • soft nofile 65535
          • hard nofile 65535
    • sysctl 调优(/etc/sysctl.conf,执行 sysctl -p 生效)
      • 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
      • net.core.rmem_max = 16777216;net.core.wmem_max = 16777216
      • net.ipv4.tcp_rmem = 4096 87380 16777216;net.ipv4.tcp_wmem = 4096 65536 16777216
      • net.ipv4.tcp_fastopen = 3
  • 进程管理与多核利用
    • 使用 cluster 模块按 CPU 核数 fork 工作进程,提高吞吐与容错
  • 传输与协议
    • 启用 HTTP/2(Nginx 或 Node.js http2 模块),开启 Gzip 压缩,静态资源由 Nginx 直接服务
  • 代理与安全
    • 反向代理可统一处理 TLS、压缩、缓存、限流与静态资源,降低 Node.js 进程压力并提升安全性

四 常见问题排查清单

  • 端口占用:ss -ltnp | grep 3000;EADDRINUSE 需更换端口或结束占用进程
  • 外部无法访问:云安全组/本机防火墙是否放行 80/443/3000;服务是否监听 0.0.0.0 而非 127.0.0.1
  • 反向代理失败:检查 proxy_pass 地址、请求头转发、Nginx 配置语法(nginx -t)与重载
  • DNS 解析异常:cat /etc/resolv.conf;nslookup 检查域名解析是否可达
  • 高并发连接失败:检查文件描述符限制(ulimit -n / limits.conf)、内核参数(somaxconn、tcp_tw_reuse 等)与应用并发模型(是否充分利用集群与异步 I/O)

0