在 CentOS 上“转发域名”通常有以下几种常见场景,我分别说明,你可对号入座:
如果你说的是让某个域名访问到 CentOS 机器:
在域名服务商处:
@ 或 www例如:
example.com -> 1.2.3.4
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
例如:访问 a.com 跳转到 b.com
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
例如: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;
}
}
如果是把域名对应 IP 的端口转发到另一台机器:
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
如果是内网把某域名指向某 IP:
修改:
vi /etc/hosts
192.168.1.100 example.com
我可以直接给你一份可复制的配置。