升級Https前的可行性驗證(一)
阿新 • • 發佈:2018-08-30
規則 codes 無法訪問 events 博客 ogr 數據安全 tls add
升級Https之前的可行性驗證
註意:自簽證書和Nginx的安裝都基於ContOS 6
一、如何申請OpenSSL自簽證書
1、安裝OpenSSL
(一)OpenSSL 工具下載
下載地址
(二)OpenSSL 安裝
參考博客
-
查看服務器是否安裝有OpenSSL
openssl version -a
- 1
-
將下載的OpenSSL源碼上傳至Linux服務器
可以使用Xshell的Xftp工具。
-
解壓上傳的.tar.gz壓縮包
tar -zxvf openssl-1.1.1-pre8.tar.gz
- 1
-
安裝gcc編譯器,如果已經安裝請略過
#如果系統安裝了gcc編譯器,如下圖所示的gcc version gcc -v #安裝gcc yum install gcc-c++
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
-
安裝zlib庫,如果已經安裝請略過
#檢查是否安裝zlib,如果安裝如下圖所示 whereis zlib #獲取zlib源碼包 wget http://zlib.net/zlib-1.2.11.tar.gz #切換到zlib源碼包中 cd zlib-1.2.11 #安裝zlib ./configure && make && make install
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
-
安裝OpenSSL工具
#首先進入OpenSSL工具解壓之後的目錄 cd openssl-1.1.1-pre8 #--prefix=指定的安裝路徑 ./config shared zlib --prefix=/usr/local/openssl && make && make install #安裝完成之後再當前目錄再執行下面命令 ./config -t make depend #然後進入OpenSSL的安裝目錄 cd /usr/local #建立文件鏈接 ln -s openssl ssl #打開etc下的這個ld.so.conf配置文件,然後再文本中添加/usr/local/openssl/lib vim /etc/ld.so.conf #執行命令使文件鏈接共享生效 ldconfig
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
-
環境變量配置
#打開etc目錄下的profile文件 vim /etc/profile #然後再文件的末尾添加如下內容 export OPENSSL=/usr/local/openssl/bin export PATH=$OPENSSL:$PATH:$HOME/bin
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
-
重開命令窗口,加載環境變量
2、自簽證書
參考博客
(一)生成根證書
-
生成.key後綴私鑰
#生成私鑰到指定目錄 openssl genrsa -out /usr/local/nginx/conf/rootca.key
- 1
- 2
- 3
- 4
-
生成.csr後綴的證書申請文件
#通過私鑰生成申請文件到指定目錄 openssl req -new -key /usr/local/nginx/conf/rootca.key -out /usr/local/nginx/conf/rootca.csr
- 1
- 2
- 3
- 4
-
生成.crt後綴的證書文件
#通過私鑰和證書申請文件,來自簽證書 openssl x509 -req -days 3650 -in /usr/local/nginx/conf/rootca.csr -signkey /usr/local/nginx/conf/rootca.key -out /usr/local/nginx/conf/rootca.crt
- 1
- 2
- 3
- 4
(二)通過根證書簽發服務端證書
-
生成.key後綴私鑰
#生成服務端私鑰 openssl genrsa -out /usr/local/nginx/conf/server.key
- 1
- 2
- 3
- 4
-
生成.csr後綴的證書申請文件
#生成服務端證書申請文件 openssl req -new -key /usr/local/nginx/conf/server.key -out /usr/local/nginx/conf/server.csr
- 1
- 2
- 3
- 4
-
生成.crt後綴的證書文件
#簽發服務端證書文件 openssl ca -in /usr/local/nginx/conf/server.csr -cert /usr/local/nginx/conf/rootca.crt -keyfile /usr/local/nginx/conf/rootca.key -out /usr/local/nginx/conf/server.csr
- 1
- 2
- 3
- 4
二、Nginx 安裝
1、安裝Nginx
參考博客
(一)下載相關源碼包
-
下載Nginx源碼包
#通過wget命令來遠程獲取源碼包到當前目錄 wget http://nginx.org/download/nginx-1.15.2.tar.gz ./
- 1
- 2
- 3
- 4
-
下載Pcre源碼包
#通過wget命令來遠程獲取源碼包到當前目錄 wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.40.tar.gz ./
- 1
- 2
- 3
- 4
(二)安裝
-
安裝Pcre
#解壓壓縮包 tar -zxvf pcre-8.40.tar.gz #切換到解壓的目錄中 cd ./pcre-8.40 #安裝 ./configure && make && make install
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
-
安裝Nginx
#解壓壓縮包 tar -zxvf nginx-1.15.2.tar.gz #切換到解壓的目錄中 cd ./nginx-1.15.2 #安裝 ./configure && make && make install
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
2、安裝Https模塊
參考博客
-
如果之前有安裝過Nginx並且配置過nginx.conf,那麽一定先做備份
#備份配置文件,前面是文件名,後面攜帶備份時間 cp nginx.conf ./nginx.conf.2018816 #備份安裝目錄sbin中 nginx運行文件 cp ./nginx/sbin/nginx ./nginx/sbin/nginx2018816
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
-
在源碼目錄下安裝Https模塊
#備份安裝目錄sbin中 nginx運行文件 cp ./nginx/sbin/nginx ./nginx/sbin/nginx2018816 #先cd到源碼包中 #獲取https模塊到指定目錄 ./configure --prefix=./nginx --with-http_stub_status_module --with-http_ssl_module #編譯 make #將編譯好的nginx運行文件復制到安裝目錄的sbin中 cp ./objs/nginx /usr/local/nginx/sbin/
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
-
驗證Https模塊是否安裝成功
#通過命令查看Https模塊是否安裝成功,如果安裝成功如下圖所示 /usr/local/nginx/sbin/nginx -V
- 1
- 2
- 3
- 4
3、啟動Nginx驗證是否安裝成功
-
Nginx 常用的一些命令
#測試nginx.conf文件是否配置正確 ./sbin/nginx -t #啟動nginx ./sbin/nginx #重啟 ./sbin/nginx -s reload
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
-
啟動Nginx
訪問Nginx部署的服務器ip地址,出現如下圖所示頁面則表示安裝成功。
三、Nginx 配置
1、Nginx 配置Https
(一)配置端口監聽
listen 443 ssl;
server_name 10.3.1.2;
- 1
- 2
(二)配置證書
ssl_certificate /usr/local/nginx/conf/server.crt;
ssl_certificate_key /usr/local/openssl/bin/nopass-server.key;
- 1
- 2
(三)其他參數配置
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
server_tokens off;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
2、Nginx 配置請求重定向和代理
<!-- 配置重定向 -->
location = /xxx {
return 302 http://10.3.1.2:18080/bms_core;
}
<!-- 配置代理 -->
location = /bms_core/ {
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://10.3.1.1:18080;
}
<!--
註意:
1、location後接的是請求匹配規則
2、在代理時如果,轉發的地址最後加了/,那麽location後匹配的請求路徑不會被代理到proxy_pass指定的路徑後,如果不加/,那麽location後的匹配路徑就會添加在proxy_pass指定的代理路徑後。示例:如果加/就會是這樣http://10.3.1.1:18080,如果不加/就會使這樣http://10.3.1.1:18080/bms_core/
3、在代理配置時加上proxy_set_header Host $host:$server_port;的作用就是在代理時端口號就不會被去掉
-->
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
3、Nginx 配置項目靜態資源
<!-- 靜態資源配置
註意:如果監聽了80端口(Http)和445端口(https),如果不配置靜態資源的代理,則會出現靜態資源無法訪問的情況。原因是因為,如果是一個Html頁面,你在第一次訪問的時候給你返回之後,在Html中引用的一個靜態資源是重新發起請求去獲取的,當請求到了80端口和445端口就會再次取來匹配location,因為這個時候沒有配置就會出現靜態資源無法訪問。
解決方案:1、配置靜態資源代理
2、將Nginx服務器作為靜態資源訪問服務器,然後再配置如果請求靜態資源就去Nginx中去匹配,在實際改造中這樣不現實,還是配置靜態資源訪問吧
如果有更好的解決方案請在下方評論!!!
-->
location ~ \.(gif|jpg|png|js|css)$ {
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://10.3.1.1:18080;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
4、Nginx Http和Https共存
<!--
目前的Http和Https共存,我暫時是單獨配置一個Http Server 和一個Https Server兩個端口監聽互不幹擾,同樣如果有更好的方案請在下方評論告訴我!!!
-->
server {
listen 80;
server_name 10.3.1.2;
location / {
root html;
index index.html index.htm;
}
location = /bms_core/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host:$server_port;
proxy_pass http://10.3.1.1:18080;
#return 302 http://10.3.1.2:18080/bms_core;
}
location ~ \.(gif|jpg|png|js|css)$ {
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://10.3.1.1:18080;
}
location = /bms {
return 302 http://10.3.1.2:18080/bms_core;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
5、我的nginx.conf配置文件的完整配置
worker_processes 4;
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 /usr/local/nginx/logs/access.log main;
sendfile on;
keepalive_timeout 65;
upstream my_server{
server 10.3.1.2:3128 weight=5 ;
server 10.3.1.2:80 weight=1;
}
server {
listen 80;
server_name 10.3.1.2;
location / {
root html;
index index.html index.htm;
}
location = /bms_core/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host:$server_port;
proxy_pass http://10.3.1.1:18080;
#return 302 http://10.3.1.2:18080/bms_core;
}
location ~ \.(gif|jpg|png|js|css)$ {
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://10.3.1.1:18080;
}
location = /bms {
return 302 http://10.3.1.2:18080/bms_core;
}
}
server {
listen 443 ssl;
server_name 10.3.1.2;
ssl_certificate /usr/local/nginx/conf/server.crt;
ssl_certificate_key /usr/local/openssl/bin/nopass-server.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
server_tokens off;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
}
location = /xxx {
return 302 http://10.3.1.2:18080/bms_core;
}
location = /bms_core/ {
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://10.3.1.1:18080;
}
location ~ \.(gif|jpg|png|js|css)$ {
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://10.3.1.1:18080;
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
四、驗證Client到Nginx Https請求是否生效,Https請求中是否建立SSL握手
1、驗證Https模塊和Https Server配置是否生效
(一)通過使用Https請求Nginx
(二)需要註意的點
-
使用OpenSSL進行自簽證書,所得證書是不能被瀏覽器所信任的。
-
如果實際業務並沒有瀏覽器與服務端交互,那麽就可以使用OpenSSL進行自簽證書,簽發證書的目的只是為了單純的使用Https請求來提高數據安全性,關於Https為什麽安全,請參考下面博客。
Https參考博客
SSL/TLS握手參考博客
-
如果需要使用到受瀏覽器信任的CA證書,可以參考下面博客對CA證書簽發機構的介紹,自己選擇哪個簽來機構來簽發自己的受信任CA證書。
收費證書申請推薦
免費證書簽發機構參考博客
2、Tcpdump使用
參考博客
#查看網卡名命令
ifconfig
#通過tcpdump抓包
#eth0為網卡名
#host 10.2.1.254為發起請求的客戶端ip地址
#-w ./eth1.cap是將抓包信息輸出到指定目錄下指定文件中
tcpdump -i eth0 host 10.2.1.254 -w ./eth1.cap
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
3、Wireshark使用
參考博客
(一)查看抓包信息
在Wireshark中點擊文件,然後打開抓包文件。
(二)查看TCP傳輸流
點擊請求,然後右鍵追蹤TCP流。
4、抓取Http請求
(一)查看是否發起Http請求
(二)追蹤TCP流,查看數據包在TCP傳輸過程中是否是明文
5、抓取Https請求
(一)查看是否建立握手
(二)追蹤TCP流,查看數據包是否加密
6、結論
(一)Http
? 通過Client向服務端發起的Http請求看到,客戶端向服務端發起Http請求,並且通過追蹤TCP流可以看到數據是明文傳輸沒有被加密。
(二)Https
? 通過Client向服務端發起的Https請求看到,中間建立了TLS握手,並且通過追蹤TCP流可以看到數據是使用對稱加密後的數據。
升級Https前的可行性驗證(一)