1. 程式人生 > >實戰http切換成https

實戰http切換成https

否則 分別是 項目 evel 大屏 ebs 技術 ket reload

Server端使用Nginx + Tomcat

Niginx SSL on

Tomcat SSL non

步驟:

1、修改代碼,將外部引用的http js css 文件修改為https,若外部鏈接不支持https 則需將靜態文件下載到項目中 在項目中引用。

2、將申請到的https安全證書放入NGINX目錄

技術分享圖片

3、修改Nginx配置文件,打開SSL支持,並將安全證書引入配置文件。

技術分享圖片

4、打開nginx websocket 支持

技術分享圖片

5、將js ,app中引用的websocket地址由ws改為wss

技術分享圖片

6、系統會直接調用外部平臺的詳情頁面,需要將其代理到https域名,否則頁面無法訪問

技術分享圖片

7、./nginx -t 測試配置文件是否能夠成功加載,若配置成功則重新加載niginx配置文件 ./nginx -s reload

8、FASTDFS文件服務器暫時不用https處理,http文件也可以訪問

升級完成後測試點

1.系統各個頁面,圖標能正常訪問

2.app調用接口能正常訪問

3.app,web 跟蹤頁圖片視頻正常顯示

4.銳明科技引用頁面正常訪問

5.監控大屏正常訪問數據顯示正常

6.外部平臺能正常調用平臺https接口

註意事項:

1、NGINX 缺少SSL模塊

在centos中,配置nginx的https時,出現如下錯誤。

nginx: [emerg] unknown directive "ssl" in /usr/local/nginx/conf/nginx.conf:102

A)到解壓的nginx目錄下

./configure --with-http_ssl_module

當執行上面語句,出現./configure: error: SSL modules require the OpenSSL library.

用 yum -y install openssl openssl-devel

B)再執行./configure

重新執行./configure --with-http_ssl_module

make ,切記不能make install 會覆蓋。

C)把原來nginx備份

cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak

把新的nginx覆蓋舊的

cp objs/nginx /usr/local/nginx/sbin/nginx

出現錯誤時cp: cannot create regular file ‘/usr/local/nginx/sbin/nginx’: Text file busy

用cp -rfp objs/nginx /usr/local/nginx/sbin/nginx解決

D)測試nginx是否正確

/usr/local/nginx/sbin/nginx -t

(nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful)

E)重啟nginx

/usr/local/nginx/sbin/nginx -s reload

2、request.getScheme() 取到https正確的協議詳解

A)配置nginx的轉發項,配置文件為nginx.conf,添加以下內容如下:

proxy_set_header X-Forwarded-Proto $scheme;

技術分享圖片

B) 配置tomcat,配置文件為server.xml,添加內容如下:

<Valve className="org.apache.catalina.valves.RemoteIpValve"
remoteIpHeader="X-Forwarded-For"
protocolHeader="X-Forwarded-Proto"
protocolHeaderHttpsValue="https"/>

技術分享圖片

3.  NGINX的匹配順序

location的語法規則如下: location [=|~|~*|^~] /uri/ { … }.

  在nginx中location分為兩類:普通location和正則location。普通 location ”是以“ = ”或“ ^~ ”為前綴或者沒有任何前綴的 /uri/,包括“/”;“正則 location ”是以“ ~ ”或“ ~* ”為前綴的 /uri/ 。

  那麽如果一個 server 塊中編寫了多個 location 的時候,Nginx對於客戶端請求匹配順序如何呢?

  官網說明如下:先匹配普通location,取的最大前綴匹配,再匹配正則location,如果匹配到則按照正則匹配,如果有多個正則可以匹配到,則按照第一個匹配結果處理,如果正則匹配失敗則使用普通location的最大前綴匹配。Nginx也設置了幾種機制可以打斷這種順序,分別是“^~ ”、“= ”或者location精確匹配。

  簡單的講順序如下:

  首先普通location“=”精確匹配;

  然後普通location的URL精確匹配;

  然後普通location”^~"配置;

  然後正則匹配;

  然後其他普通location匹配;

  最後“/”通用匹配

實戰http切換成https