Apache的多个子域名用一个IP地址 [英] Apache Multiple Sub Domains With One IP Address

查看:161
本文介绍了Apache的多个子域名用一个IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能已经问,但我找不到一个直接的答案,或者我发现那些不工作。

This has probably been asked but I can't find a straight answer, or the ones I found don't work.

我有一个域 mydomain.com ,解析为IP;让我们叫它8.8.8.8。 DNS设置还点二子域到该IP地址与A记录。这些都是 dev.mydomain.com staging.mydomain.com 。这两个有A记录指向8.8.8.8。

I have one domain mydomain.com, resolving to an IP; let's call it 8.8.8.8. The DNS settings also point two subdomains to that IP address with an A record. These are dev.mydomain.com and staging.mydomain.com. Both have an A-record pointing to 8.8.8.8.

在服务器(8.8.8.8),我有两个虚拟主机文件。具体如下:

On the server (8.8.8.8) I have two virtual hosts files. These are as follows:

staging.mydomain.com.conf

<VirtualHost *:80>
    ServerName  staging.mydomain.com
    DocumentRoot /var/www/html/mydomain.com/staging/
</VirtualHost>

和...

dev.mydomain.com.conf

<VirtualHost *:80>
    ServerName  dev.mydomain.com
    DocumentRoot /var/www/html/mydomain.com/dev/
</VirtualHost>

的问题是:

The problem is:

不管我是否参观的 http://staging.mydomain.com 的或的 http://dev.mydomain.com 的,我总是降落在的升级.mydomain.com来的(阿帕奇提供这些文件)。

Regardless of whether I visit http://staging.mydomain.com or http://dev.mydomain.com, I always land on staging.mydomain.com (Apache serves these files).

我已经重新启动Apache和甚至服务器。如果我改变了conf文件的顺序,使开发首先,我总是看到。任何建议将是如此AP preciated。谢谢!

I have restarted Apache and even the server. If I change the order of the .conf files so that dev is first, I always see that. Any suggestions would be so appreciated. Thanks!

我发现自己回到了这个问题了!如果你的知道的你的语法是正确的,你可能有一个坏的符号链接。删除并重新再现,在两者之间重启动Apache。我只是做了这一点,它解决了头部划伤小时。在 CentOS的你可以用的httpd -S

I find myself back at this problem again! If you know that your syntax is correct, you might have a bad symlink. Delete it and recreate again, restarting apache in-between. I just did this and it solved hours of head-scratching. On CentOS you can check your available vhosts with httpd -S

推荐答案

听起来像是你需要添加的 了NameVirtualHost 指令您的配置。

Sounds like you need to add NameVirtualHost directive to your configuration.

NameVirtualHost         *:80

在某些情况下Apache可能无法处理 *:80 VirtualHosts正确。在这种情况下,你应该在特定的IP直接映射VirtualHosts。

Under some circumstances Apache may not be able to handle *:80 VirtualHosts correctly. In those cases you should map VirtualHosts directly on specific IPs.

NameVirtualHost         8.8.8.8:80

<VirtualHost 8.8.8.8:80>
    ServerName  staging.mydomain.com
    ServerAlias stage.mydomain.com
    DocumentRoot /var/www/html/mydomain.com/staging/
</VirtualHost>

<VirtualHost 8.8.8.8:80>
    ServerName  dev.mydomain.com
    ServerAlias development.mydomain.com
    DocumentRoot /var/www/html/mydomain.com/dev/
</VirtualHost>

您还可以运行 apachectl中-t -D DUMP_VHOSTS 来看看Apache如何解析虚拟主机的配置。

You can also run apachectl -t -D DUMP_VHOSTS to see how Apache parses the VirtualHost configuration.

更新:由于在评论中提到,通常你可以只用了NameVirtualHost *:80 。所以大部分的时间,你可以配置虚拟主机如下:

Update: As mentioned in the comments, usually you can just use NameVirtualHost *:80. So most of the time you can configure the virtual hosts as follows.

NameVirtualHost         *:80

<VirtualHost *:80>
    ServerName  staging.mydomain.com
    ServerAlias stage.mydomain.com
    DocumentRoot /var/www/html/mydomain.com/staging/
</VirtualHost>

<VirtualHost *:80>
    ServerName  dev.mydomain.com
    ServerAlias development.mydomain.com
    DocumentRoot /var/www/html/mydomain.com/dev/
</VirtualHost>

这篇关于Apache的多个子域名用一个IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆