我现在一直在寻找,但找不到任何答案。
我正在使用httpd 2.2.15和Centos 6.2。
我已经configuration了大量虚拟主机的Apache。 即:
UseCanonicalName off VirtualDocumentRoot /var/www/html/%0
我将有相同的“主”域与不同的子域指向虚拟主机。 我创build了一个自签名的证书,用于通用名称* .mydomain.com的testing目的。 整个服务器有一个IP。
我怎样才能configurationApache为我的虚拟主机使用SSL?
如果可能,添加到上面我也想实现这一点:
我可以定义一个目录,或更好的一些文件(如login页面),应该从ssl中排除? 所有的虚拟主机都是同一个应用程序的基本不同的实例(除了我在下面的2中提到的那些)。
我可以定义一些不应该使用ssl的虚拟主机(我完全控制这些虚拟主机的子域名)。 这将是两个应用程序,我的主页(www)和一些pipe理应用程序。 如果不能做出例外,我想我会把它们放在另一台服务器上。
除了我在上面提到的2以外,所有的虚拟主机都会根据用户请求自动创build。
基于@Shanes评论,我更新 :如果用户使用https://时,他们不应该,这是很好的,如果他们被redirect到http://。 如果这是不可能的,我想这是可以的,如果他们得到一个错误消息。 当然,如果http和https都起作用,只要http能够用于不受保护的文件(实际上这可能是首选)。
我可以find如何使用mod-rewrite来做这个例子,除了它不适用于大众域(即使用<VirtualHost>)。
有什么诀窍实现这一目标?
如果不可能的话,我会很高兴得到一些关于如何做到这一点的提示。
首先,您需要确保您的当前configuration已准备好添加SSL侦听器。您尚未指定是否使用主服务器或<VirtualHost> ,但是如果您使用的是主服务器那么你需要切换到<VirtualHost> 。
<VirtualHost *:80> ServerName everything.example.com ServerAlias *.example.com VirtualDocumentRoot /var/www/html/%0 # insert logging config, anything else you need.. <Directory /var/www/html/> Order Allow,Deny Allow from all # Get rid of this if you need to allow htaccess files: AllowOverride None </Directory> RewriteEngine On # We're going to insert some Rewrite configuration here in a minute. </VirtualHost>
然后,我们将为您添加一个运行SSL的新VirtualHost。
# Add this if you don't already have it: Listen 443 <VirtualHost *:443> ServerName everything.example.com ServerAlias *.example.com VirtualDocumentRoot /var/www/html/%0 SSLEngine On SSLCertificateFile /path/to/cert.pem SSLCertificateKeyFile /path/to/private.key # insert logging config, anything else you need.. <Directory /var/www/html/> Order Allow,Deny Allow from all # Get rid of this if you need to allow htaccess files: AllowOverride None </Directory> # if you want to kick someone back to HTTP if they're using HTTPS, # do that with Rewrite configuration here. For example: #RewriteRule ^/(non/sensitive/content.*\.html)$ http://%{HTTP_HOST}/$1 [R=301,L] </VirtualHost>
所以,这让我们到了通过HTTP和HTTPS都可以提供内容的地步。 现在,为了强制某些域的HTTPS,我们可以使用mod_rewrite。
重要的安全信息! 从安全angular度来看,您需要非常小心。 如果您只是简单地将HTTP上的所有内容redirect到与HTTPS相同的内容,那么由于硬编码的资源位置,您可能会“隐藏”通过HTTP发送请求而不是HTTPS的情况,并且如果请求中包含敏感数据,通过互联网发送,未encryption。 您需要权衡这种风险与您纠正这些问题的能力,错误页面的用户不友好状态(如果出现问题)以及数据的敏感性。
为了强制SSL在某些位置,你需要插入mod_rewriteconfiguration到80端口(我已经在上面的configuration中进行了评论)。 就目录或域而言,您可以根据需要构build任何types的行为; 我将举几个例子:
# Exclude the domain "static.example.com" RewriteCond %{HTTP_HOST} !^static\.example\.com$ # Exclude the directory /images RewriteCond %{REQUEST_URI} !^/images/ # Exclude requests to .css files RewriteCond %{REQUEST_URI} !\.css$ # This is the more secure but less user friendly version - block requests to the non-secured port. RewriteRule ^ - [F,L] # This is the user friendly version, where you need to be especially careful that # your site never sends sensitive data to http accidentally: #RewriteRule ^/(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
或者,如果您想仅对基本目录的请求进行友好的redirect,并且对其他人的错误行为:
# Exclude the domain "static.example.com" RewriteCond %{HTTP_HOST} !^static\.example\.com$ # ..insert other exclusion conditions here.. RewriteRule ^/$ https://%{HTTP_HOST}/ [R=301,L] # Exclude the domain "static.example.com" RewriteCond %{HTTP_HOST} !^static\.example\.com$ # ..insert other exclusion conditions here.. RewriteRule ^ - [F,L]
如果这些例子不符合您的需求,请告诉我。