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

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

问题描述

可能有人问过这个问题,但我找不到直接的答案,或者我发现的答案不起作用.

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 设置还使用 A 记录将两个子域指向该 IP 地址.它们是 dev.mydomain.comstaging.mydomain.com.两者都有指向 8.8.8.8 的 A 记录.

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>

问题在于:

无论我是访问 http://staging.mydomain.com 还是http://dev.mydomain.com,我总是登台.mydomain.com(Apache 提供这些文件).

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 文件的顺序,使 dev 排在第一位,我总是会看到这一点.任何建议将不胜感激.谢谢!

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

当虚拟主机的 apache 日志 文件不存在或不可写时,我也发现此问题存在.

I've also found this issue to exist when the apache log files for the virtual host don't exist, or aren't writable.

推荐答案

听起来你需要添加 NameVirtualHost 指令到您的配置中.

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

NameVirtualHost         *:80

在某些情况下,Apache 可能无法正确处理 *:80 VirtualHosts.在这些情况下,您应该将 VirtualHosts 直接映射到特定 IP.

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 如何解析 VirtualHost 配置.

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>

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

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