IIS與APACHE實(shí)現(xiàn)HTTP重定向到HTTPS
IIS7
從微軟的官方網(wǎng)站下載HTTP重寫(xiě)模塊,安裝完畢之后重啟IIS服務(wù),之后打開(kāi)IIS控制臺(tái),發(fā)現(xiàn)多了一個(gè)組件,雙擊“URL重寫(xiě)”,在右邊窗體中選擇“添加規(guī)則”,并添加一個(gè)空白規(guī)則,給規(guī)則自定義一個(gè)名字(名稱自便),比如我這里叫“redirect to HTTPS”,模式為:(.*),添加一個(gè)條件,條件輸入為 {HTTPS},與模式匹配,模式為 ^OFF$,然后配置操作,操作類型為:重定向,重定向到URL為:https://{HTTP_HOST}/{R:1},重定向類型:永久301。
設(shè)置完畢后點(diǎn)擊右側(cè)的“應(yīng)用”,這個(gè) URL 重寫(xiě)就配置完畢了。
配置后,根目錄下的web.config文件的內(nèi)容如下:
<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <rewrite> <rules> <rule name="redirect to HTTPS" enabled="true" stopProcessing="true"> <match url="(.*)" /> <conditions> <add input="{HTTPS}" pattern="^OFF$" /> </conditions> <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" /> </rule> </rules> </rewrite> </system.webServer> </configuration>
Apache http跳轉(zhuǎn)https配置
修改.htaccess文件,在文件里增加如下幾行:
RewriteEngine On RewriteBase / RewriteCond %{HTTPS} !=on RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
另一種寫(xiě)法是:
RewriteEngine on RewriteBase / RewriteCond %{SERVER_PORT} !^443$ RewriteRule (.*) https://%{SERVER_NAME}/$1 [R=301,L]
nginx配置
nginx的rewrite方法
思路
這應(yīng)該是大家最容易想到的方法,將所有的http請(qǐng)求通過(guò)rewrite重寫(xiě)到https上即可
配置
server { listen 192.168.1.111:80; server_name test.com; rewrite ^(.*)$ https://$host$1 permanent; }
搭建此虛擬主機(jī)完成后,就可以將http://test.com的請(qǐng)求全部重寫(xiě)到https://test.com上了
nginx的497狀態(tài)碼
error code 497
497 - normal request was sent to HTTPS
解釋:當(dāng)此虛擬站點(diǎn)只允許https訪問(wèn)時(shí),當(dāng)用http訪問(wèn)時(shí)nginx會(huì)報(bào)出497錯(cuò)誤碼
思路
利用error_page命令將497狀態(tài)碼的鏈接重定向到https://test.com這個(gè)域名上
配置
server {
listen 192.168.1.11:443; #ssl端口
listen 192.168.1.11:80; #用戶習(xí)慣用http訪問(wèn),加上80,后面通過(guò)497狀態(tài)碼讓它自動(dòng)跳到443端口
server_name test.com;
#為一個(gè)server{......}開(kāi)啟ssl支持
ssl on;
#指定PEM格式的證書(shū)文件
ssl_certificate /etc/nginx/test.pem;
#指定PEM格式的私鑰文件
ssl_certificate_key /etc/nginx/test.key;
#讓http請(qǐng)求重定向到https請(qǐng)求
error_page 497 https://$host$uri?$args;
}
index.html刷新網(wǎng)頁(yè)
思路
上述兩種方法均會(huì)耗費(fèi)服務(wù)器的資源,我們用curl訪問(wèn)baidu.com試一下,看百度的公司是如何實(shí)現(xiàn)baidu.com向www.baidu.com的跳轉(zhuǎn)
可以看到百度很巧妙的利用meta的刷新作用,將baidu.com跳轉(zhuǎn)到www.baidu.com.因此我們可以基于http://test.com的虛擬主機(jī)路徑下也寫(xiě)一個(gè)index.html,內(nèi)容就是http向https的跳轉(zhuǎn)
index.html
<html>
<meta http-equiv="refresh" content="0;url=https://test.com/">
</html>
nginx虛擬主機(jī)配置
server {
listen 192.168.1.11:80;
server_name test.com;
location / {
#index.html放在虛擬主機(jī)監(jiān)聽(tīng)的根目錄下
root /srv/www/http.test.com/;
}
#將404的頁(yè)面重定向到https的首頁(yè)
error_page 404 https://test.com/;
}
后記
上述三種方法均可以實(shí)現(xiàn)基于nginx強(qiáng)制將http請(qǐng)求跳轉(zhuǎn)到https請(qǐng)求,大家可以評(píng)價(jià)一下優(yōu)劣或者根據(jù)實(shí)際需求進(jìn)行選擇。
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。