You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Cloudflare中将两个域名映射到单服务器下的两个站点端点

实现域名映射到多站点的最优方案

一、准备工作

  • 确保example1.com和example2.com已完成Cloudflare域名托管(将域名DNS服务器切换为Cloudflare提供的NS地址)
  • 确认VPS使用的Web服务器是Nginx或Apache(以下分两种主流服务器给出配置步骤)

二、Cloudflare DNS解析配置

登录Cloudflare后台,为两个域名分别添加A记录:

  • 针对example1.com:
    • 类型选择A
    • 名称填写@(代表主域名)
    • 内容填入你的VPS公网IP 30.24.32.545
    • 代理状态开启「橙色云朵」(启用Cloudflare CDN与防护功能)
  • 针对example2.com:
    • 重复上述操作,添加相同IP的A记录,代理状态同样开启橙色云朵

三、VPS Web服务器配置(核心步骤)

方案1:Nginx配置(推荐)

  1. 登录VPS,进入Nginx配置目录(通常为/etc/nginx/conf.d/或/etc/nginx/sites-available/)
  2. 为每个站点创建独立配置文件:
    • 创建example1.com.conf:
      server {
          listen 80;
          server_name example1.com www.example1.com;
      
          root /var/www/example1.com/public_html; # 替换为你的站点实际路径
          index index.php index.html index.htm;
      
          location / {
              try_files $uri $uri/ /index.php?$args;
          }
      
          location ~ \.php$ {
              include snippets/fastcgi-php.conf;
              fastcgi_pass unix:/run/php/php7.4-fpm.sock; # 根据你的PHP版本调整路径
          }
      
          location ~ /\.ht {
              deny all;
          }
      }
      
    • 创建example2.com.conf:
      server {
          listen 80;
          server_name example2.com www.example2.com;
      
          root /var/www/example2.com/public_html; # 替换为第二个站点的实际路径
          index index.php index.html index.htm;
      
          location / {
              try_files $uri $uri/ /index.php?$args;
          }
      
          location ~ \.php$ {
              include snippets/fastcgi-php.conf;
              fastcgi_pass unix:/run/php/php7.4-fpm.sock; # 根据你的PHP版本调整路径
          }
      
          location ~ /\.ht {
              deny all;
          }
      }
      
  3. 启用配置文件(若使用sites-available目录,需软链到sites-enabled):
    sudo ln -s /etc/nginx/sites-available/example1.com.conf /etc/nginx/sites-enabled/
    sudo ln -s /etc/nginx/sites-available/example2.com.conf /etc/nginx/sites-enabled/
    
  4. 测试配置并重启Nginx:
    sudo nginx -t
    sudo systemctl restart nginx
    

方案2:Apache配置

  1. 登录VPS,开启虚拟主机模块:
    sudo a2enmod vhost_alias
    
  2. 进入Apache虚拟主机配置目录(通常为/etc/apache2/sites-available/)
  3. 创建example1.com.conf:
    <VirtualHost *:80>
        ServerName example1.com
        ServerAlias www.example1.com
        DocumentRoot /var/www/example1.com/public_html
    
        <Directory /var/www/example1.com/public_html>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Require all granted
        </Directory>
    
        ErrorLog ${APACHE_LOG_DIR}/example1.com_error.log
        CustomLog ${APACHE_LOG_DIR}/example1.com_access.log combined
    </VirtualHost>
    
  4. 创建example2.com.conf:
    <VirtualHost *:80>
        ServerName example2.com
        ServerAlias www.example2.com
        DocumentRoot /var/www/example2.com/public_html
    
        <Directory /var/www/example2.com/public_html>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Require all granted
        </Directory>
    
        ErrorLog ${APACHE_LOG_DIR}/example2.com_error.log
        CustomLog ${APACHE_LOG_DIR}/example2.com_access.log combined
    </VirtualHost>
    
  5. 启用虚拟主机并重启Apache:
    sudo a2ensite example1.com.conf
    sudo a2ensite example2.com.conf
    sudo systemctl restart apache2
    

四、WordPress站点URL调整

  1. 登录每个WordPress后台,进入「设置」-「常规」:
    • 对example1.com站点:将「WordPress地址(URL)」和「站点地址(URL)」均改为http://example1.com(后续开启HTTPS后可改为https)
    • 对example2.com站点:同样将两个URL改为http://example2.com
  2. 若无法登录后台,可修改站点根目录下的wp-config.php强制设置:
    define('WP_HOME','http://example1.com'); // 替换为对应站点域名
    define('WP_SITEURL','http://example1.com');
    

五、Cloudflare开启HTTPS(可选但推荐)

  1. 在Cloudflare后台进入「SSL/TLS」-「概述」,将SSL模式设置为「灵活」(后续可改为「严格」,需在VPS上配置SSL证书)
  2. 开启「自动HTTPS重定向」和「始终使用HTTPS」,确保用户访问http时自动跳转至https

内容的提问来源于stack exchange,提问作者Zoom1992

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 11:35:31