首页主机资讯Node.js在CentOS中的网络配置怎么做

Node.js在CentOS中的网络配置怎么做

时间2025-12-18 17:11:04发布访客分类主机资讯浏览1047
导读:Node.js 在 CentOS 的网络配置全流程 一 系统网络与防火墙 设置静态 IP(以 CentOS 7 为例) 确认网卡名:ip addr(常见如 ens33 或 eth0) 编辑配置文件:/etc/sysconfig/ne...

Node.js 在 CentOS 的网络配置全流程


一 系统网络与防火墙

  • 设置静态 IP(以 CentOS 7 为例)

    • 确认网卡名:ip addr(常见如 ens33eth0
    • 编辑配置文件:/etc/sysconfig/network-scripts/ifcfg-ens33(或 ifcfg-eth0)
    • 关键字段示例:
      • BOOTPROTO=static
      • ONBOOT=yes
      • IPADDR=192.168.1.100
      • NETMASK=255.255.255.0
      • GATEWAY=192.168.1.1
      • DNS1=192.168.1.1
    • 使配置生效:systemctl restart network
    • 验证:ip addr、route -n、cat /etc/resolv.conf
  • 开放防火墙端口(以 firewalld 为例)

    • 启动并开机自启:systemctl start firewalld;systemctl enable firewalld
    • 开放端口(示例为 3000/tcp):firewall-cmd --zone=public --add-port=3000/tcp --permanent
    • 重新加载:firewall-cmd --reload
    • 验证:firewall-cmd --list-all

二 域名与反向代理

  • 使用 Nginx 反向代理到 Node.js
    • 安装:yum install -y nginx
    • 启动并开机自启:systemctl start nginx;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;
        • }
      • }
    • 检查并重载:nginx -t;systemctl reload nginx
    • DNS 将域名 A 记录 指向服务器公网 IP

三 Node.js 应用与监听配置

  • 监听正确地址与端口
    • 生产环境建议监听 0.0.0.0(而非 127.0.0.1),例如:app.listen(3000, ‘0.0.0.0’)
    • 如经 Nginx 反向代理,保持应用监听 3000 即可,由 Nginx 处理端口 80/443
  • 启用 HTTPS/HTTP/2(推荐在 Nginx 终止 TLS)
    • 使用 Nginx 配置 SSL 证书并开启 HTTP/2,Node.js 侧使用 HTTP/1.1 与 X-Forwarded-* 头
  • WebSocket 支持
    • Nginx 需开启升级头:proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection “Upgrade”;
  • 进程与多核
    • 使用 cluster 模块或进程管理工具(如 PM2)提升并发能力

四 性能与安全加固

  • 系统资源与内核参数
    • 文件描述符限制:/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
  • 传输与连接优化
    • 启用 HTTP/2、静态资源由 Nginx 缓存与压缩
    • 长连接与 KeepAlive 合理设置(应用与反向代理两端)

五 快速验证清单

  • 网络与端口
    • 本机:ss -lntp | grep 3000;curl -I http://127.0.0.1:3000
    • 外部:curl -I http://服务器IP:3000 或 http://域名
  • 防火墙与路由
    • firewall-cmd --list-ports;ip route;ping 网关
  • Nginx
    • nginx -t;systemctl status nginx;curl -I http://域名
  • DNS
    • dig 域名 +short 应返回服务器公网 IP

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: Node.js在CentOS中的网络配置怎么做
本文地址: https://pptw.com/jishu/775259.html
反汇编指令如何帮助分析恶意代码 CentOS上Node.js模块如何管理

游客 回复需填写必要信息