如何创建适用于http和https的虚拟主机?


8

这就是我设置虚拟主机的方式:

<VirtualHost mysite> 
  <Directory "/Users/myusername/sitefolder"> 
    Options +FollowSymlinks
    AllowOverride All 
    Order Allow,Deny
    Allow from all
  </Directory> 
  DocumentRoot "/Users/myusername/sitefolder"
  ServerName mysite
  SSLEngine on
  SSLCertificateFile /Users/myusername/certs/server.crt
  SSLCertificateKeyFile /Users/myusername/certs/server.key
</VirtualHost>

使用此配置,我只能使用https查看我的网站,但不能查看http。当我关闭SSLEngine然后我无法用https查看我的网站,但http工作正常。

如何调整以上行,以便能够使用http和https查看我的网站?

我使用的是OSX Lion,但我认为这并不重要。

谢谢。

Answers:


7

您需要创建两个虚拟主机:

<VirtualHost mysite:80> 
  <Directory "/Users/myusername/sitefolder"> 
    Options +FollowSymlinks
    AllowOverride All 
    Order Allow,Deny
    Allow from all
  </Directory> 
  DocumentRoot "/Users/myusername/sitefolder"
  ServerName mysite
</VirtualHost>


<VirtualHost mysite:443> 
  <Directory "/Users/myusername/sitefolder"> 
    Options +FollowSymlinks
    AllowOverride All 
    Order Allow,Deny
    Allow from all
  </Directory> 
  DocumentRoot "/Users/myusername/sitefolder"
  ServerName mysite
  SSLEngine on
  SSLCertificateFile /Users/myusername/certs/server.crt
  SSLCertificateKeyFile /Users/myusername/certs/server.key
</VirtualHost>

第一个是常规HTTP主机,第二个是处理您的HTTPS流量。


有没有办法将公共代码移动到另一个文件中并#include它以使维护更容易?
Ponkadoodle

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.