Apache VirtualHost 查找速度慢

发布于 2024-12-06 04:46:51 字数 918 浏览 1 评论 0原文

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

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

做个ˇ局外人 2024-12-13 04:46:51

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

127.0.0.1       localhost test.local work.local yii.local

并删除最后一行。

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

Add your virtual hosts to the first line:

127.0.0.1       localhost test.local work.local yii.local

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.

荒岛晴空 2024-12-13 04:46:51

对我来说解决这个问题的方法是编辑 httpd-vhosts.conf 并将以下所有实例更改

<VirtualHost *:80>

为:

<VirtualHost 0.0.0.0:80>

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

What fixed it for me was editing httpd-vhosts.conf and changing all instances of:

<VirtualHost *:80>

to:

<VirtualHost 0.0.0.0:80>

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.

俯瞰星空 2024-12-13 04:46:51

对于使用 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. change test.local to test.dev.

Explanation and source here: http://bencrowder.net/blog/2012/10/slow-localhost-in-chrome/

忆沫 2024-12-13 04:46:51

您还应该在 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

你是年少的欢喜 2024-12-13 04:46:51

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

ServerName 127.0.0.1:80

Also setting the ip for ServerName in httpd.conf file worked for me

ServerName 127.0.0.1:80
薄荷→糖丶微凉 2024-12-13 04:46:51

我解决了这个问题。
此配置适用于 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 证书

切勿 切勿定义任何在 vhost 文件中,我们将在 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>

3- 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  
    SSLCertificateChainFile conf/ssl_hh/server-ca.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  
    SSLCertificateChainFile conf/ssl_hh/server2-ca.key 
</VirtualHost>

仅此而已。

编辑 :

!!!不要忘记添加 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  
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

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:

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

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:

<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>

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.

(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  
    SSLCertificateChainFile conf/ssl_hh/server-ca.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  
    SSLCertificateChainFile conf/ssl_hh/server2-ca.key 
</VirtualHost>

That is all.

Edit :

!!! Do not forget to add SSLCertificateChainFile CA file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文