目錄
前言:
一、安裝nginx
二、安裝OpenSSL
? ? ? ? ?1、下載OpenSSL:
? ? ? ? ?2、配置環(huán)境變量:
? ? ? ? ? ? ? ? ? 2.1:配置環(huán)境變量,OpenSSL_HOME?
? ? ? ? ? ? ? ? ? 2.2:配置path
?三、生成https證書
? ? ? ? ?1、創(chuàng)建ssl文件夾用于存放證書。創(chuàng)建私鑰 (建議使用系統(tǒng)窗口,不要用gitBash 有涉及到選擇的地方,gitBash無法選擇)
? ? ? ? ?2、文件夾中生成shidian.key文件 創(chuàng)建csr證書。
? ? ? ? ?3、復制?shidian.key?并重命名?shidian.key.org?執(zhí)行命令
? ? ? ? ?4、生成crt證書
?四、修改nginx.conf配置,使用https請求
?五、關(guān)于對https請求的理解
? ? ? ? ?1、為什么要用https?
? ? ? ? ?2、http存在的問題
? ? ? ? ?3、什么是https?
? ? ? ? ? ? ? ? ? 1、https定義:
? ? ? ? ? ? ? ? ? 2、SSL證書:
? ? ? ? ? ? ? ? ? 3、SSL發(fā)展史:?
? ? ? ? ?4、瀏覽器在使用HTTPS傳輸數(shù)據(jù)的流程是什么?
? ? ? ? ?5、HTTPS的缺點
? ? ? ? ?6、HTTP與HTTPS的對比
前言:
項目中有需要做接口回調(diào),測試和dev環(huán)境都是https方式請求調(diào)用沒有問題,但是本地調(diào)試不通。因為本地啟動后端服務都是http請求,所以使用nginx代理,將http請求轉(zhuǎn)換為https請求,方便調(diào)試。
一、安裝nginx
Nginx官網(wǎng):nginx: download
安裝到非中文目錄下:
?啟動Nginx:點擊nginx.exe文件,啟動nginx
然后訪問:
訪問成功,說明安裝nginx成功
二、安裝OpenSSL
OpenSSL官網(wǎng):Win32/Win64 OpenSSL Installer for Windows - Shining Light Productions
? ? ? ? ?1、下載OpenSSL:
?放到一個非中文的目錄下去:
? ? ? ? ?2、配置環(huán)境變量:
? ? ? ? ? ? ? ? ? 2.1:配置環(huán)境變量,OpenSSL_HOME?
? ? ? ? ? ? ? ? ? 2.2:配置path
?三、生成https證書
首先在Nginx得文件夾中新建一個ssl文件夾:
? ? ? ? ?1、創(chuàng)建ssl文件夾用于存放證書。創(chuàng)建私鑰 (建議使用系統(tǒng)窗口,不要用gitBash 有涉及到選擇的地方,gitBash無法選擇)
openssl genrsa -des3 -out shidian.key 1024? ? ? ? ? ? //shidian 自己取的名字
可以輸入pass作為初始的密碼,兩次一致。
? ? ? ? ?2、文件夾中生成shidian.key文件 創(chuàng)建csr證書。
openssl req -new -key shidian.key -out shidian.csr
?此時應該有兩個文件:
? ? ? ? ?3、復制?shidian.key?并重命名?shidian.key.org?執(zhí)行命令
openssl rsa -in shidian.key.org -out shidian.key
? ? ? ? ?4、生成crt證書
openssl x509 -req -days 365 -in shidian.csr -signkey shidian.key -out shidian.crt
最終的文件夾里的內(nèi)容為:
?四、修改nginx.conf配置,使用https請求
整個nginx.conf文件配置:
#user ?nobody;
worker_processes ?1;#error_log ?logs/error.log;
#error_log ?logs/error.log ?notice;
#error_log ?logs/error.log ?info;#pid ? ? ? ?logs/nginx.pid;
events {
? ? worker_connections ?1024;
}
http {
? ? include ? ? ? mime.types;
? ? default_type ?application/octet-stream;? ? #log_format ?main ?'$remote_addr - $remote_user [$time_local] "$request" '
? ? # ? ? ? ? ? ? ? ? ?'$status $body_bytes_sent "$http_referer" '
? ? # ? ? ? ? ? ? ? ? ?'"$http_user_agent" "$http_x_forwarded_for"';? ? #access_log ?logs/access.log ?main;
? ? sendfile ? ? ? ?on;
? ? #tcp_nopush ? ? on;? ? #keepalive_timeout ?0;
? ? keepalive_timeout ?65;? ? #gzip ?on;
? ? ?server {
? ? ? ?listen ? ? ? 80;
? ? ? ?server_name ?localhost;? ? ? ?#charset koi8-r;
? ? ? ?#access_log ?logs/host.access.log ?main;
? ? ? ? location / {
? ? ? ? ? ? root ? html;
? ? ? ? ? ? index ?index.html index.htm;
? ? ? ?}? ? ? ?#error_page ?404 ? ? ? ? ? ? ?/404.html;
? ? ? ? # redirect server error pages to the static page /50x.html
? ? ? ? #
? ? ? ? error_page ? 500 502 503 504 ?/50x.html;
? ? ? ? location = /50x.html {
? ? ? ? ? ? root ? html;
? ? ? ? }? ? ? ? # proxy the PHP scripts to Apache listening on 127.0.0.1:80
? ? ? ? #
? ? ? ? #location ~ \.php$ {
? ? ? ? # ? ?proxy_pass ? http://127.0.0.1;
? ? ? ? #}? ? ? ? # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
? ? ? ? #
? ? ? ? #location ~ \.php$ {
? ? ? ? # ? ?root ? ? ? ? ? html;
? ? ? ? # ? ?fastcgi_pass ? 127.0.0.1:9000;
? ? ? ? # ? ?fastcgi_index ?index.php;
? ? ? ? # ? ?fastcgi_param ?SCRIPT_FILENAME ?/scripts$fastcgi_script_name;
? ? ? ? # ? ?include ? ? ? ?fastcgi_params;
? ? ? ? #}? ? ? ? # deny access to .htaccess files, if Apache's document root
? ? ? ? # concurs with nginx's one
? ? ? ? #
? ? ? ? #location ~ /\.ht {
? ? ? ? # ? ?deny ?all;
? ? ? ? #}
? ? }
? ? # another virtual host using mix of IP-, name-, and port-based configuration
? ? #
? ? #server {
? ? # ? ?listen ? ? ? 8000;
? ? # ? ?listen ? ? ? somename:8080;
? ? # ? ?server_name ?somename ?alias ?another.alias;? ? # ? ?location / {
? ? # ? ? ? ?root ? html;
? ? # ? ? ? ?index ?index.html index.htm;
? ? # ? ?}
? ? #}
? ? # HTTPS server
? ? #
? ? server {
? ? ? ? listen ? ? ? 443 ssl;
? ? ? ? server_name ?localhost;? ? ? ? # ssl_certificate ? ? ?cert.pem;
? ? ? ? # ssl_certificate_key ?cert.key;
?? ?ssl_certificate ? ? ?..//ssl//shidian.crt;
? ? ? ? ssl_certificate_key ?..//ssl//shidian.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;
? ? ? ? }
? ? }
}
?
?關(guān)于ssl配置
? ? server {
? ? ? ? listen ? ? ? 443 ssl;
? ? ? ? server_name ?localhost;? ? ? ? # ssl_certificate ? ? ?cert.pem;
? ? ? ? # ssl_certificate_key ?cert.key;
? ? ? ? ssl_certificate ? ? ?..//ssl//shidian.crt;? ? ?指的是ssl證書
? ? ? ? ssl_certificate_key ?..//ssl//shidian.key; 指的是ssl的密鑰? ? ? ? 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請求訪問:
給nginx配置https請求成功!
?五、關(guān)于對https請求的理解
? ? ? ? ?1、為什么要用https?
之前我們大部分的請求都是用的http的請求,現(xiàn)在為什么突然要用到https請求?
實際使用中,絕大多數(shù)的網(wǎng)站現(xiàn)在都采用的是https協(xié)議,這也是未來互聯(lián)網(wǎng)發(fā)展的趨勢。
在wireshark抓取的一個博客網(wǎng)站的登錄請求過程中,訪問的賬號密碼都是明文傳輸, 這樣客戶端發(fā)出的請求很容易被不法分子截取利用,因此,HTTP協(xié)議不適合傳輸一些敏感信息,比如:各種賬號、密碼等信息,使用http協(xié)議傳輸隱私信息非常不安全。
? ? ? ? ?2、http存在的問題
- 請求信息明文傳輸,容易被竊聽截取。
- 數(shù)據(jù)的完整性未校驗,容易被篡改
- 沒有驗證對方身份,存在冒充危險
? ? ? ? ?3、什么是https?
? ? ? ? ? ? ? ? ? 1、https定義:
HTTPS 協(xié)議(HyperText Transfer Protocol over Secure Socket Layer):一般理解為HTTP+SSL/TLS,通過 SSL證書來驗證服務器的身份,并為瀏覽器和服務器之間的通信進行加密。
那么問題來了,什么是SSL證書?
? ? ? ? ? ? ? ? ? 2、SSL證書:
1、SSL(Secure Socket Layer,安全套接字層):1994年為 Netscape 所研發(fā),SSL 協(xié)議位于 TCP/IP 協(xié)議與各種應用層協(xié)議之間,為數(shù)據(jù)通訊提供安全支持。
2、TLS(Transport Layer Security,傳輸層安全):其前身是 SSL,它最初的幾個版本(SSL 1.0、SSL 2.0、SSL 3.0)由網(wǎng)景公司開發(fā),1999年從 3.1 開始被 IETF 標準化并改名,發(fā)展至今已經(jīng)有 TLS 1.0、TLS 1.1、TLS 1.2 三個版本。SSL3.0和TLS1.0由于存在安全漏洞,已經(jīng)很少被使用到。TLS 1.3 改動會比較大,目前還在草案階段,目前使用最廣泛的是TLS 1.1、TLS 1.2。
? ? ? ? ? ? ? ? ? 3、SSL發(fā)展史:?
- 1994年NetSpace公司設計SSL協(xié)議(Secure Sockets Layout)1.0版本,但未發(fā)布。
- 1995年NetSpace發(fā)布SSL/2.0版本,很快發(fā)現(xiàn)有嚴重漏洞
- 1996年發(fā)布SSL/3.0版本,得到大規(guī)模應用
- 1999年,發(fā)布了SSL升級版TLS/1.0版本,目前應用最廣泛的版本
- 2006年和2008年,發(fā)布了TLS/1.1版本和TLS/1.2版本
? ? ? ? ?4、瀏覽器在使用HTTPS傳輸數(shù)據(jù)的流程是什么?
文章來源:http://www.zghlxwxcb.cn/news/detail-808135.html
- 首先客戶端通過URL訪問服務器建立SSL連接。
- 服務端收到客戶端請求后,會將網(wǎng)站支持的證書信息(證書中包含公鑰)傳送一份給客戶端。
- 客戶端的服務器開始協(xié)商SSL連接的安全等級,也就是信息加密的等級。
- 客戶端的瀏覽器根據(jù)雙方同意的安全等級,建立會話密鑰,然后利用網(wǎng)站的公鑰將會話密鑰加密,并傳送給網(wǎng)站。
- 服務器利用自己的私鑰解密出會話密鑰。
- 服務器利用會話密鑰加密與客戶端之間的通信
? ? ? ? ?5、HTTPS的缺點
- HTTPS協(xié)議多次握手,導致頁面的加載時間延長近50%;
- HTTPS連接緩存不如HTTP高效,會增加數(shù)據(jù)開銷和功耗;
- 申請SSL證書需要錢,功能越強大的證書費用越高。
- SSL涉及到的安全算法會消耗 CPU 資源,對服務器資源消耗較大。
? ? ? ? ?6、HTTP與HTTPS的對比
- HTTPS是HTTP協(xié)議的安全版本,HTTP協(xié)議的數(shù)據(jù)傳輸是明文的,是不安全的,HTTPS使用了SSL/TLS協(xié)議進行了加密處理。
- http和https使用連接方式不同,默認端口也不一樣,http是80,https是443。
今天的分享就到這了,希望能夠幫助到你!文章來源地址http://www.zghlxwxcb.cn/news/detail-808135.html
到了這里,關(guān)于Nginx實現(xiàn)本地http轉(zhuǎn)https請求的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!