Apache VirtualHost 查找速度慢
我终于设法根据我的虚拟主机需求正确配置 httpd.conf。这是httpd.conf 文件的相关部分
NameVirtualHost *:80
<VirtualHost *:80>
ServerName localhost
DocumentRoot /Applications/XAMPP/htdocs/
</VirtualHost>
<VirtualHost *:80>
ServerName test.local
DocumentRoot /Applications/XAMPP/htdocs/test/
</VirtualHost>
<VirtualHost *:80>
ServerName work.local
DocumentRoot /Applications/XAMPP/htdocs/work/
</VirtualHost>
当我访问本地主机上的任何内容(即http://localhost/phpmyadmin)时,一切都非常快。 每当我访问 test.local 或 work.local (或我配置的其他内容)时,它都会花费 10-15 秒进行查找。以下请求得到正确处理,并且速度非常快,但在一分钟左右不活动后,它必须再次查找。
这是我的 /etc/hosts 文件
127.0.0.1 localhost
255.255.255.255 broadcasthost
#::1 localhost
fe80::1%lo0 localhost
# Virtualhosts
127.0.0.1 test.local work.local yii.local
我怎样才能解决这个恼人的问题?
I finally managed how to configure properly httpd.conf for my virtualhost needings. This is the relevant part of httpd.conf file
NameVirtualHost *:80
<VirtualHost *:80>
ServerName localhost
DocumentRoot /Applications/XAMPP/htdocs/
</VirtualHost>
<VirtualHost *:80>
ServerName test.local
DocumentRoot /Applications/XAMPP/htdocs/test/
</VirtualHost>
<VirtualHost *:80>
ServerName work.local
DocumentRoot /Applications/XAMPP/htdocs/work/
</VirtualHost>
When I access anything on localhost (i.e. http://localhost/phpmyadmin) everything is very fast.
Whenever I access test.local or work.local (or others I configured) it spends 10-15 seconds on lookup. The following requests are handled correctly and it's very fast but after a minute or so of inactivity, it has to lookup again.
This is my /etc/hosts file
127.0.0.1 localhost
255.255.255.255 broadcasthost
#::1 localhost
fe80::1%lo0 localhost
# Virtualhosts
127.0.0.1 test.local work.local yii.local
How could I fix this annoying issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
将您的虚拟主机添加到第一行:
并删除最后一行。
那应该可以解决问题。您的虚拟主机现在是本地主机的别名。在多行中使用相同的 IP 地址并不是一个好主意。这只会混淆 DNS 缓存。
Add your virtual hosts to the first line:
And remove the last line.
That should do the trick. Your vhosts are now an alias for localhost. It's not a good idea to have the same IP-address in multiple lines. This just confuses the DNS-cache.
对我来说解决这个问题的方法是编辑 httpd-vhosts.conf 并将以下所有实例更改
为:
解析主机大约需要 2-5 秒,现在是即时的。我不必修改主机文件中站点的顺序。这只会让它使用 ipv4 而不是 ipv6,我敢打赌你无论如何也不会使用它。
What fixed it for me was editing httpd-vhosts.conf and changing all instances of:
to:
It was taking about 2-5 seconds to resolve the host, now it is instant. I did not have to modify the order of my sites in my hosts file. This just makes it use ipv4 instead of ipv6 which I'd bet you don't use anyway.
对于使用 Chrome 且虚拟主机查找速度仍然缓慢的用户,您需要将虚拟主机名更改为
.local
以外的名称,例如。将test.local
更改为test.dev
。说明和来源:http://bencrowder.net/blog/2012 /10/slow-localhost-in-chrome/
For anyone who is using Chrome and still gets slow virtual host lookup, you need to change the virtual host name to something else than
.local
, eg. changetest.local
totest.dev
.Explanation and source here: http://bencrowder.net/blog/2012/10/slow-localhost-in-chrome/
您还应该在 vhosts 文件中实现其他参数,例如单独的错误日志和服务器别名
DocumentRoot "D:/xampp/htdocs/asd"
服务器名称 asd.com.br
服务器别名 asd.com.br
错误日志“logs/asd.log”
CustomLog“logs/asd.log”组合
You should as well implement other parameters to your vhosts file, like separate error logs and server alias
DocumentRoot "D:/xampp/htdocs/asd"
ServerName asd.com.br
ServerAlias asd.com.br
ErrorLog "logs/asd.log"
CustomLog "logs/asd.log" combined
另外在 httpd.conf 文件中设置 ServerName 的 ip 对我有用
Also setting the ip for ServerName in httpd.conf file worked for me
我解决了这个问题。
此配置适用于 Windows,但我认为它也可以在 Linux 上运行。
您需要配置3个文件。
1-httpd.conf
切勿定义任何在httpd.conf中,我们将在httpd-vhost.conf中定义它,
永远不要定义任何;在 httpd.conf 中,我们将在 httpd-ssl.conf 中定义它
切勿在 httpd.conf 中定义任何 ssl 证书,
否则系统会很混乱,让您定期等待 5 秒。
像这样定义每个虚拟主机的目录标签:
示例:
2- httpd-vhost.conf
切勿在 httpd-vhost.conf 中定义任何 ssl 证书
切勿 切勿定义任何在 vhost 文件中,我们将在 httpd-ssl.conf 中定义它,
否则您必须定期等待 5sn。
仅定义 80 端口,不要在标签等中使用 0.0.0.0:80,保持原始状态,如下所示
示例:
3- httpd-ssl.conf
Apache 开始在此文件中侦听 443 端口,因此在侦听 443 后或在底部添加自然而然地跟随。
仅此而已。
编辑 :
!!!不要忘记添加 SSLCertificateChainFile CA 文件。
I solved the problem.
This configuration is for window, but I think it will have been working on Linux too.
You need to configure 3 files.
1-httpd.conf
Never define any <VirtualHost *:80 > in httpd.conf, we will define it at httpd-vhost.conf
Never define any <VirtualHost *:443 > in httpd.conf, we will define it at httpd-ssl.conf
Never define any ssl certificate in httpd.conf
otherwise the system is confused make you wait 5s periodically..
Define Directory tag of each vhost like this:
Example:
2- httpd-vhost.conf
Never define any ssl certifiacte in httpd-vhost.conf
Never Never define any <VirtualHost *:443 > in vhost file, we will define it at httpd-ssl.conf
otherwise you have to wait 5sn periodically.
Define only for 80 port and dont use 0.0.0.0:80 in tags etc, keep original like below
Example:
3- httpd-ssl.conf
Apache is starting to listen 443 port in this file, so after listen 443 or at the bottom add following naturally.
That is all.
Edit :
!!! Do not forget to add SSLCertificateChainFile CA file.