Apache VirtualHost 查找速度慢

问题描述 投票:0回答:6

我终于设法根据我的虚拟主机需求正确配置 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

我该如何解决这个恼人的问题?

apache virtualhost httpd.conf
6个回答
84
投票

将您的虚拟主机添加到第一行:

127.0.0.1       localhost test.local work.local yii.local

并删除最后一行。

这应该可以解决问题。您的虚拟主机现在是本地主机的别名。在多行中使用相同的 IP 地址并不是一个好主意。这只会混淆 DNS 缓存。


6
投票

为我解决的问题是编辑 httpd-vhosts.conf 并更改以下所有实例:

<VirtualHost *:80>

至:

<VirtualHost 0.0.0.0:80>

解决主机需要大约 2-5 秒,现在是即时的。我不必修改主机文件中站点的顺序。这只会让它使用 ipv4 而不是 ipv6,我敢打赌你无论如何也不会使用。


5
投票

对于使用 Chrome 且虚拟主机查找速度仍然缓慢的任何人,您需要将虚拟主机名更改为

.local
之外的其他名称,例如。将
test.local
更改为
test.dev

解释和来源在这里:http://bencrowder.net/blog/2012/10/slow-localhost-in-chrome/


0
投票

您还应该在虚拟主机文件中实现其他参数,例如单独的错误日志和服务器别名


文档根目录“D:/xampp/htdocs/asd”
服务器名称 asd.com.br
服务器别名 asd.com.br
错误日志“logs/asd.log”
CustomLog“logs/asd.log”组合


0
投票

还在 httpd.conf 文件中设置 ServerName 的 ip 对我有用

ServerName 127.0.0.1:80

0
投票

我是这样解决这个问题的。 此配置适用于 Windows,但我认为它也可以在 Linux 上运行。

您需要配置3个文件。

1-httpd.conf  
2-httpd-vhost.conf  
3-httpd-ssl.conf

General rules
do not use 0.0.0.0:80
do not use 0.0.0.0:443
or 
[::]:80
[::]:443

Keep all original like *:80 *:443

1-httpd.conf

(keep original)
Listen *:80 or Listen 80

永远不要在 httpd.conf 中定义任何 ,我们将在 httpd-vhost.conf 中定义它

永远不要在httpd.conf中定义任何,我们将在httpd-ssl.conf中定义它

切勿在 httpd.conf 中定义任何 ssl 证书

否则系统会混乱让你定期等待5秒.. 像这样定义每个虚拟主机的目录标签:

示例:

DocumentRoot "C:/xampp/htdocs"
<Directory "C:/xampp/htdocs">
      Options Indexes FollowSymLinks Includes
      AllowOverride All
      Require all granted
</Directory>

<Directory "C:/xampp/htdocs2">
      Options Indexes FollowSymLinks Includes
      AllowOverride All
      Require all granted
</Directory>

2-httpd-vhost.conf

切勿在 httpd-vhost.conf 中定义任何 ssl 证书

永远不要在虚拟主机文件中定义任何,我们将在httpd-ssl.conf中定义它

否则你必须定期等待5sn。

仅定义80端口,不要在标签等中使用0.0.0.0:80,保持原始如下

示例:

<VirtualHost *:80  >
    ServerName domain1.com
    DocumentRoot "C:/xampp/htdocs/htdocs"
    ErrorLog  "logs/error-hh2.log"
</VirtualHost>

<VirtualHost *:80  >
    ServerName domain2.com
    DocumentRoot "C:/xampp/htdocs/htdocs2"
    ErrorLog  "logs/error-hh2.log"
</VirtualHost>

2-httpd-ssl.conf Apache 开始在此文件中侦听 443 端口,因此在侦听 443 后或在底部自然添加以下内容。

(keep original)
Listen 443


#edit existing
<VirtualHost *:443 >
    ServerName domain1.com
    DocumentRoot "C:/xampp/htdocs/htdocs"
    ErrorLog  "logs/error-hh.log"
    SSLEngine On
    SSLCertificateFile conf/ssl_hh/server.crt
    SSLCertificateKeyFile conf/ssl_hh/server.key  

 #keep FilesMatch or BrowserMatch parameter as original if not needed  
    
</VirtualHost>

#add for second domain
<VirtualHost *:443 >
    ServerName domain2.com
    DocumentRoot "C:/xampp/htdocs/htdocs2"
    ErrorLog  "logs/error-hh2.log"
    SSLEngine On
    SSLCertificateFile conf/ssl_hh/server2.crt
    SSLCertificateKeyFile conf/ssl_hh/server2.key  
</VirtualHost>

仅此而已。

© www.soinside.com 2019 - 2024. All rights reserved.