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

CentOS Node.js网络配置怎么做

时间2026-01-19 19:02:04发布访客分类主机资讯浏览1505
导读:CentOS 上 Node.js 的网络配置与发布实践 一 系统网络基础配置 确认网卡名称:执行 ip a,常见名称如 eth0、ens33、enp0s3。 方式一 NetworkManager nmcli(推荐) 查看连接名:nmc...

CentOS 上 Node.js 的网络配置与发布实践


一 系统网络基础配置

  • 确认网卡名称:执行 ip a,常见名称如 eth0、ens33、enp0s3
  • 方式一 NetworkManager nmcli(推荐)
    1. 查看连接名:nmcli con show;2) 配置静态 IPv4:
      sudo nmcli con mod “连接名” ipv4.method manual ipv4.addresses 192.168.1.100/24 ipv4.gateway 192.168.1.1 ipv4.dns “8.8.8.8 114.114.114.114” autoconnect yes
    2. 使生效:sudo nmcli con up “连接名”(或 nmcli con reload)。
  • 方式二 修改网卡配置文件(ifcfg,CentOS 7 常见)
    1. 编辑:sudo vi /etc/sysconfig/network-scripts/ifcfg-eth0(文件名与网卡一致);
    2. 关键项:BOOTPROTO=static、ONBOOT=yes、IPADDR=192.168.1.100、NETMASK=255.255.255.0(或 PREFIX=24)、GATEWAY=192.168.1.1、DNS1=8.8.8.8、DNS2=114.114.114.114;
    3. 使生效:sudo systemctl restart NetworkManager 或 sudo systemctl restart network。
  • 验证:ip a 查看地址;ping 网关;cat /etc/resolv.conf 看 DNS;nslookup www.example.com 测试解析。
  • 防火墙放行(示例为 firewalld):sudo firewall-cmd --permanent --add-service=http;sudo firewall-cmd --permanent --add-service=https;sudo firewall-cmd --reload。

二 发布 Node.js 应用的两种方式

  • 方式A 直接监听公网端口
    1. 应用监听:app.listen(3000, ‘0.0.0.0’, () => console.log(‘Listening on 0.0.0.0:3000’));
    2. 放行端口:sudo firewall-cmd --permanent --add-port=3000/tcp & & sudo firewall-cmd --reload;
    3. 云服务器需在控制台安全组放行 TCP 3000
  • 方式B Nginx 反向代理(推荐,便于 TLS/HTTP2/压缩/缓存/负载均衡)
    1. 安装与启动:sudo yum install -y nginx & & sudo systemctl enable --now nginx;
    2. 配置 /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;
      }
      }
    3. 校验并重载:sudo nginx -t & & sudo systemctl reload nginx;
    4. 如需 HTTPS,使用 certbot 获取证书并启用 HTTP/2。

三 域名与本地解析

  • 公网域名:在 DNS 服务商添加 A 记录 指向服务器公网 IP;生效后即可用域名访问。
  • 本地测试或内网域名:编辑 /etc/hosts
    192.168.1.100 example.test
    保存后本机即可解析 example.test 到该 IP。

四 高并发网络优化(可选)

  • 文件描述符限制
    ulimit -n 查看;临时 ulimit -n 65535;永久在 /etc/security/limits.conf 增加:
    • soft nofile 65535
    • hard nofile 65535
  • 内核网络参数 /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
    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
    执行 sysctl -p 生效。
  • 应用层
    使用 HTTP/2集群模式(cluster) 提升吞吐与多核利用。

五 快速验证清单

  • 网络:ip a 有固定 IP;ping 网关与外网域名可达;/etc/resolv.conf 有 DNS
  • 服务:ss -ltnp | grep :3000 或 :80;curl -I http://localhost:3000。
  • 外部:浏览器访问 http://服务器IP:3000 或 http://域名;Nginx 状态 sudo systemctl status nginx。

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


若转载请注明出处: CentOS Node.js网络配置怎么做
本文地址: https://pptw.com/jishu/786092.html
Node.js在CentOS中的错误处理 centos中sqlserver性能监控

游客 回复需填写必要信息