如何在Apache中使用两个子域?

时间:2018-02-21 11:17:28

标签: apache virtualhost

我在name.com上有域名,我从DNS记录中创建了两个子域名

Type, Host,                   Answer,               TTL
A,    subdomain1.domain.com , EC2 IP,               300
A,    subdomain2.domain.com , EC2 IP same as above, 300


<VirtualHost *:80>
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
        ServerName subdmain1.domain.com
        ServerAlias www.subdmain1.domain.com

        Project related Configuration....

</VirtualHost>
<VirtualHost *:80>
        ServerName subdomain2.domain.com
        ServerAlias www.subdomain2.domain.com
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
        Second project code ....
</VirtualHost>

我在我的EC2服务器的apache2中进行了上述配置,但是当我访问任何子域时,自动总是转到第一个VirtualHost,第二个Virtualhost无法正常工作。

请帮我配置ec2

上的两个子域名
  

注意:我在name.com网站上使用主域名

1 个答案:

答案 0 :(得分:1)

可能你应该删除&#39; *&#39;从virtualhost提供respactive域名,并在respactive设置文件的allowedhost中添加域名,它对我有用。

<VirtualHost subdmain1.domain.com:80>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    ServerName subdmain1.domain.com
    ServerAlias www.subdmain1.domain.com
    Project related Configuration....
</VirtualHost>

<VirtualHost subdomain2.domain.com:80>
    ServerName subdomain2.domain.com
    ServerAlias www.subdomain2.domain.com
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    Second project code ...
</VirtualHost>