Home  >  Q&A  >  body text

centos绑定多个域名

正在使用aliyun主机,运行centos6.4 64位系统,安装lamp环境后进行域名绑定。配置文件如下:

<VirtualHost *:80>
DocumentRoot /var/www/html
ServerName 120.24.54.xxx
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot /var/www/html/domain1
    ServerName domain.com
    ServerAlias *.domain.com
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot /var/www/html/domain2
    ServerName domain.com
    ServerAlias *.domain.com
</VirtualHost>

然后通过www.domain1.com可以访问指向/var/www/html/domain1目录的网站,但是通过www.domain1.com访问同样是指向/var/www/html/domain1,通过IP 120.24.54.xxx访问也是指向domai1目录,配置文件其他地方没动过。我想要通过IP访问就指向html目录,通过域名访问就指向对应的目录。

大家讲道理大家讲道理2757 days ago630

reply all(3)I'll reply

  • 黄舟

    黄舟2017-04-25 09:03:16

    Already found the answer by looking at the official documentation:
    # Ensure that Apache listens on port 80
    Listen 80

    # Listen for virtual host requests on all IP addresses
    NameVirtualHost *:80
    
    <VirtualHost *:80>
    DocumentRoot /www/example1
    ServerName www.example.com
    
    # Other directives here
    
    </VirtualHost>
    
    <VirtualHost *:80>
    DocumentRoot /www/example2
    ServerName www.example.org
    
    # Other directives here
    
    </VirtualHost>
    

    Must be turned onNameVirtualHost *:80, otherwise no matter how many domain names are bound, they will all point to the documentroot of the first virtualhost

    reply
    0
  • PHPz

    PHPz2017-04-25 09:03:16

    Have you restarted apache after modifying the configuration?
    Are you sure your vhost mod is enabled?

    reply
    0
  • 怪我咯

    怪我咯2017-04-25 09:03:16


    DocumentRoot /var/www/html/domain1
    ServerName domain.com
    ServerAlias ​​*.domain.com


    DocumentRoot /var/www/html/domain2
    ServerName domain.com
    ServerAlias ​​*.domain.com

    Has this been changed?

    reply
    0
  • Cancelreply