緣由
當前公司服務器已經(jīng)采用 http 協(xié)議的方式部署成功,可 App Store 要求必須采用 https 協(xié)議,那么,能否在不改變公司服務器代碼的情況下,實現(xiàn) https 的要求呢?
答案是肯定的,采用 Nginx 反向代理實現(xiàn)(以代理服務器來接受internet上的連接請求,然后將請求轉發(fā)給內部網(wǎng)絡上的服務器)。
windows 環(huán)境下安裝
http://nginx.org/
一些代碼
unzip nginx-1.3.13.zipcd nginx-1.3.13cd c:/nginx-1.10.2/
啟動
start nginx// 臨時代碼c:/nginx-1.10.2/start.bat
停止
停止:nginx.exe -s stop或nginx.exe -s quit批量刪除 nginx 進程(可能點擊太多引起的)taskkill /F /IM nginx.exe > nul
重新載入
nginx.exe -s reload
配置
nginx-1.10.2/conf/nginx.conf
server { # 監(jiān)聽8080端口 listen 8080; # 服務器根目錄 root html/data/up1; location / { } }
配置好后,打開地址即可訪問頁面( html/data/up1 下的默認首頁)
http://localhost:8080
啟動 ssl
server { listen 443 ssl; # 域名,實際情況下時,將這個改成域名 server_name localhost; ssl on; # 證書位置 ssl_certificate ssl/server.crt; ssl_certificate_key ssl/server.key; location / { } }
可以打開這個地址
https://localhost:8080
當然,瀏覽器會提示站點不安全,解決這個問題的方式是 選擇官方頒布的 SSL 證書
反向代理
方式是指以代理服務器來接受internet上的連接請求,然后將請求轉發(fā)給內部網(wǎng)絡上的服務器,并將從服務器上得到的結果返回給internet上請求連接的客戶端
示例
將 1.gif 放在 html/data/up1 目錄下
詳細可參照幫助文檔
http://nginx.org/en/docs/beginners_guide.html
服務器1
server { listen 8080; root html/data/up1; location / { }}
服務器2
server { listen 82; location / { # 當前的轉發(fā)到 proxy_pass http://localhost:8080; } location /images/ { root html/data; } # location ~ \.(gif|jpg|png)$ { # root html/data/images; # }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
http://localhost:82/1.gif
如果最后配置打開后,相當于找 root 為 html/data/images/1.gif 的文件
http://localhost:82/images/1.gif ==> http://localhost:82/data/images/1.gif
因為匹配到了 /images/ 相當于找 root 為 html/data/images/1.gif
http://localhost:82/data/images/1.gif ==> http://localhost:8080/data/images/data/images/1.gif
所以,找不到了
https 轉 http
轉發(fā)測試示例
server { listen 82; listen 443 ssl; ssl on; ssl_certificate ssl/server.crt; ssl_certificate_key ssl/server.key; location / { # 轉發(fā)到 http 服務器中 proxy_pass http://localhost; } }
蘋果ATS驗證
更新Nginx根目錄下 conf/nginx.conf 文件如下:
server {
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
}
允許windows 防火墻端口例外
443
nginx 配置
server { listen 443 ssl; # server_name s4.url.cn; #填寫綁定證書的域名 server_name localhost; #匹配的域名名稱 ssl on; ssl_certificate ssl/1_s4.url.cn_bundle.crt; ssl_certificate_key ssl/2_s4.url.cn.key; ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照這個協(xié)議配置 ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;#按照這個套件配置 ssl_prefer_server_ciphers on; location / { proxy_pass http://s4.url.cn; } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
技巧
發(fā)現(xiàn)錯誤的調試方法
查看錯誤日志,這里通常會寫好路徑轉發(fā)的測試
c:/nginx-1.10.2/logs/error.log
IP地址的SSL可以訪問,但域名不可以
找了好久,才發(fā)現(xiàn)測試的 域名 與 IP 地址不一致,當然域名訪問不了啦
參考資料
反向代理:Web服務器的“經(jīng)紀人”
http://www.open-open.com/lib/view/open1417488526633.html
修改轉發(fā)的信息
http://blog.csdn.net/u010391029/article/details/50395680
SSL證書配置
http://www.wosign.com/news/ios-app-https.htm
轉載地址:http://blog.csdn.net/lvye1221/article/details/53843607