温馨提示×

centos如何转发域名

小樊
36
2026-10-17 10:21:28
栏目: 云计算

在 CentOS 上“转发域名”通常有以下几种常见场景,我分别说明,你可对号入座:


一、把域名解析指向 CentOS 服务器(DNS 层面)

如果你说的是让某个域名访问到 CentOS 机器:

1. 域名解析(A 记录)

在域名服务商处:

  • 添加 A 记录
  • 主机记录:@ 或 www
  • 记录值:CentOS 服务器公网 IP

例如:

example.com -> 1.2.3.4

2. 确认防火墙放开 80/443

firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload

二、CentOS 做 Web 域名转发(HTTP 301/302 跳转)

例如:访问 a.com 跳转到 b.com

使用 Nginx(推荐)

yum install nginx -y
systemctl enable --now nginx

编辑配置:

vi /etc/nginx/conf.d/forward.conf

内容:

server {
    listen 80;
    server_name a.com;
    return 301 https://b.com$request_uri;
}

重载:

nginx -t
systemctl reload nginx

三、CentOS 做反向代理(域名转发到本地/其他服务)

例如:api.example.com → 127.0.0.1:8080

server {
    listen 80;
    server_name api.example.com;

    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header Host $host;
    }
}

四、CentOS 端口转发(iptables / firewalld)

如果是把域名对应 IP 的端口转发到另一台机器:

firewalld 示例

firewall-cmd --permanent --add-masquerade
firewall-cmd --permanent --add-forward-port=port=80:proto=tcp:toaddr=192.168.1.10:toport=80
firewall-cmd --reload

五、内网域名转发(DNS 劫持 / 内网解析)

如果是内网把某域名指向某 IP:

修改:

vi /etc/hosts
192.168.1.100 example.com

你可以告诉我:

  1. 是 公网域名 还是 内网?
  2. 想做的是 跳转 / 反向代理 / 端口转发 / DNS?
  3. 用的是 Nginx / Apache / 纯系统转发?

我可以直接给你一份可复制的配置。

0 踩