在 CentOS 上“绑定多个域名”通常是指:让一台服务器(一个公网 IP)通过 Web 服务器(如 Nginx / Apache) 同时对外提供多个域名访问。下面按常见场景说明。
域名已解析到服务器 IP
a.com → 1.2.3.4
b.com → 1.2.3.4
防火墙放行 80 / 443
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload
yum install nginx -y
systemctl enable nginx
systemctl start nginx
mkdir -p /usr/share/nginx/html/a.com
mkdir -p /usr/share/nginx/html/b.com
/etc/nginx/conf.d/a.com.conf
server {
listen 80;
server_name a.com www.a.com;
root /usr/share/nginx/html/a.com;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
/etc/nginx/conf.d/b.com.conf
server {
listen 80;
server_name b.com www.b.com;
root /usr/share/nginx/html/b.com;
index index.html;
}
nginx -t
systemctl restart nginx
✅ 现在访问 http://a.com 和 http://b.com 会进入不同目录。
yum install httpd -y
systemctl enable httpd
systemctl start httpd
mkdir -p /var/www/a.com
mkdir -p /var/www/b.com
/etc/httpd/conf.d/a.com.conf
<VirtualHost *:80>
ServerName a.com
DocumentRoot /var/www/a.com
</VirtualHost>
/etc/httpd/conf.d/b.com.conf
<VirtualHost *:80>
ServerName b.com
DocumentRoot /var/www/b.com
</VirtualHost>
systemctl restart httpd
使用 Let’s Encrypt:
yum install certbot
certbot --nginx # 或 certbot --apache
可自动为多个域名签发证书。
例如:
www.a.com
api.a.com
只需在 server_name 或 ServerName 中添加即可。
如果你能说明:
我可以给你更精确的配置示例。