如何创建一个既支持http又支持https的虚拟主机?

13

我的配置是:

Listen 443 http
NameVirtualHost *:80
NameVirtualHost *:443
ServerName *:80

<VirtualHost *:80> 
  [some non-ssl stuff]
  ServerName account.example.com
</VirtualHost>

<VirtualHost *:443> 
  [some non-ssl stuff(directory, docroot)] 
  ServerName account.example.com
  SSLEngine on
  SSLCertificateFile /Users/myusername/certs/server.crt
  SSLCertificateKeyFile /Users/myusername/certs/server.key
</VirtualHost>

我的网站的http版本无法访问,但ssl版本可以正常使用。我想同时使用两个虚拟主机,http(80)和https(443),通过mod_rewrite将http重写为https URL。

uname -a
Linux 3.4.62-53.42.amzn1.x86_64 GNU/Linux

httpd -v
Server version: Apache/2.2.25 (Unix)

请帮忙理解我做错了什么。


无法访问您网站的非SSL版本是什么意思?这是否意味着当您输入URL时,会收到其他内容,例如默认内容或其他情况? - Martin Strejc
因此,这意味着我收到了一条消息,上面写着“Mozilla无法与example.com建立连接”。然而,我通过netstat发现apache只侦听443端口。如果我添加Listen 80指令,apache就无法启动。 - Clyde
1
error_log 中: 服务器应该支持 SSL,但没有配置证书 [提示:SSLCertificateFile] ((null):0) - Clyde
问题出在绑定端口80上。当另一个进程已经在监听端口80并且你启动apache也监听端口80时,apache在启动时会失败。通过命令"netstat -nltp"检查已经在端口80上运行的内容(要查看PID,您必须以root身份登录)。可能另一个实例的apache或nginx或其他正在端口80上运行。 - Martin Strejc
不,端口80上没有任何监听进程。 它无法启动,因为Apache提示记录: “服务器应该具备SSL意识,但没有配置证书[提示:SSLCertificateFile] ((null):0)” - Clyde
无法回答我的问题,但我找到了解决方案。我添加了一个关闭SSLEngine的http虚拟主机。稍后会发布我的答案。谢谢! - Clyde
1个回答

10

因此,我的配置现在是:

Listen 443 http
Listen 80
NameVirtualHost *:80
NameVirtualHost *:443
ServerName *:80

<VirtualHost *:443> 
  [some non-ssl stuff(directory, docroot)] 
  ServerName account.example.com
  SSLEngine on
  SSLCertificateFile /Users/myusername/certs/server.crt
  SSLCertificateKeyFile /Users/myusername/certs/server.key
</VirtualHost>

<VirtualHost *:80>
  SSLEngine off
  [other stuff like docroot]
</VirtualHost>

我不太确定 SSLEngine off 的作用是什么,但现在它可以工作了。所以,我在 http vhost 的 .htaccess 文件中添加了重写规则来将 http 重定向到 https:

#Redirrect from http to https
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

2
我们需要在哪个配置文件中进行更改?虚拟主机配置文件还是HTTPD/SSL配置文件? - Suresh Karia

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接