說明:此教程適合已經(jīng)配置好 WNMP 環(huán)境娩嚼。
實現(xiàn) Https 首先需要向管理機構(gòu)申請證書凯正,而我們此次由于是練習(xí)目的终惑,所以通過 Openssl 自己生成證書刑棵。首先我們需要用到生成證書的 Openssl 軟件。
1. 安裝 Openssl
下載地址:http://slproweb.com/products/Win32OpenSSL.html (根據(jù)系統(tǒng)選擇 32 位或者 64 位版本下載安裝)涵紊。
下載完成后傍妒,進(jìn)行安裝,我安裝在了 C:\wnmp\OpenSSL-Win64 文件夾中摸柄。
2. 安裝 ActivePerl (此軟件目的為了解析 pl 文件颤练,部分系統(tǒng)不安裝也可以實現(xiàn)本教程的功能,安裝該軟件目的為了學(xué)習(xí) perl)驱负。
下載地址:http://www.activestate.com/activeperl/downloads/ (根據(jù)系統(tǒng)選擇 win32 或者 win64 版本下載安裝)嗦玖。
3. 配置環(huán)境變量
在環(huán)境變量中添加環(huán)境變量
在 path 變量結(jié)尾添加如下 :C:\wnmp\OpenSSL-Win64\bin;
4 生成證書
(1) 首先在 nginx 安裝目錄中創(chuàng)建 ssl 文件夾用于存放證書。比如我的文件目錄為 C:\wnmp\nginx\ssl 以管理員身份進(jìn)入命令行模式跃脊,進(jìn)入 ssl 文件夾宇挫。 命令為: cd c:/wnmp/nginx/ssl
(2) 創(chuàng)建私鑰
在命令行中執(zhí)行命令: openssl genrsa -des3 -out lee.key 1024 (lee 文件名可以自定義),如下圖所示:
輸入密碼后酪术,再次重復(fù)輸入確認(rèn)密碼器瘪。記住此密碼,后面會用到绘雁。
(3)創(chuàng)建 csr 證書
在命令行中執(zhí)行命令: openssl req -new -key lee.key -out lee.csr (key 文件為剛才生成的文件橡疼,lee 為自定義文件名)
如上圖所示,執(zhí)行上述命令后庐舟,需要輸入信息欣除。輸入的信息中最重要的為 Common Name,這里輸入的域名即為我們要使用 https 訪問的域名挪略。
以上步驟完成后历帚,ssl 文件夾內(nèi)出現(xiàn)兩個文件:(4)去除密碼。
在加載 SSL 支持的 Nginx 并使用上述私鑰時除去必須的口令杠娱,否則會在啟動 nginx 的時候需要輸入密碼挽牢。
復(fù)制 lee.key 并重命名為 lee.key.org
可以使用此命令行,也可以使用鼠標(biāo)操作 copy lee.key lee.key.org
去除口令墨辛,在命令行中執(zhí)行此命令: **openssl rsa -in lee.key.org -out lee.key **(lee 為自定義文件名)
如下圖所示卓研,此命令需要輸入剛才設(shè)置的密碼。
(5)生成 crt 證書
在命令行中執(zhí)行此命令: **openssl x509 -req -days 365 -in lee.csr -signkey lee.key -out lee.crt **(lee 為自定義文件名)
證書生成完畢,ssl 文件夾中一共生成如下 4 個文件奏赘,我們需要使用到的是 lee.crt 和 lee.key寥闪。
5. 修改 nginx.conf 文件
nginx.conf 文件位于:C:\wnmp\nginx\conf
找到該文件中如下代碼的位置進(jìn)行修改:
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
修改為:
# HTTPS server
#
#modify by lee 20160907 for https -s
server {
listen 443 ssl;
server_name www.lee.com;
ssl_certificate C:/wnmp/nginx/ssl/lee.crt;
ssl_certificate_key C:/wnmp/nginx/ssl/lee.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root C:/wnmp/lee;
index index.html index.htm index.php;
}
root C:/wnmp/lee;
fastcgi_pass 127.0.0.1:9001;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
#modify by lee 20160907 for https -s
重啟 nginx。
在瀏覽器中磨淌,訪問 https://www.lee.com疲憋。發(fā)現(xiàn)出現(xiàn)證書認(rèn)證,并能夠成功訪問梁只。(www.lee.com 為生成證書時缚柳,Common Name 輸入的域名)
(執(zhí)行此步驟時,需要配置好 Virtual Host搪锣,并且在 www.lee.com 開放目錄中添加了 index.php 默認(rèn)入口訪問文件秋忙。)
上面的 https 被紅色劃線是因為我們使用的是自己生成的證書,此證書不受瀏覽器信任构舟,如果想使其變?yōu)榫G色灰追,則需要向證書管理機構(gòu)進(jìn)行申請。
6. 添加重定向狗超,自動跳轉(zhuǎn)使用 https琐凭。
在 nginx.conf 中 virtual host 中如下代碼位置添加一行代碼:
listen 80;
server_name www.lee.com;
#modify by lee 20160907 for https Redirect -s
rewrite ^(.*) https://$server_name$1 permanent;
#modify by lee 20160907 for https Redirect -e
重啟 nginx算芯。
訪問 www.lee.com囱嫩,會發(fā)現(xiàn)瀏覽器自動跳轉(zhuǎn)到 https://www.lee.com沾谓,并能夠成功訪問。
至此渗稍,https 訪問配置成功完成佩迟。
附加配置
server {
listen 80;
# http 自動轉(zhuǎn)發(fā)給 https
rewrite ^(.*) https://$server_name$1 permanent;
ssl_certificate D:/phpStudy/PHPTutorial/nginx/ssl/aijia.crt;
ssl_certificate_key D:/phpStudy/PHPTutorial/nginx/ssl/aijia.key;
server_name aijia.test ;
root "D:\phpStudy\PHPTutorial\WWW\aijia";
location / {
index index.html index.htm index.php;
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php?s=$1 last;
break;
}
#autoindex on;
}
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
}
server {
listen 443 ssl;
ssl_certificate D:/phpStudy/PHPTutorial/nginx/ssl/aijia.crt;
ssl_certificate_key D:/phpStudy/PHPTutorial/nginx/ssl/aijia.key;
server_name aijia.test ;
root "D:\phpStudy\PHPTutorial\WWW\aijia";
location / {
index index.html index.htm index.php;
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php?s=$1 last;
break;
}
#autoindex on;
}
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
}