前言
本文解釋了怎麼對nginx和後端服務器組或代理服務器進行加密http通信。
內容提綱
前提條件
獲取SSL服務端證書
獲取SSL客戶端證書
配置nginx
配置後端服務器
完整示例
前提條件
nginx源碼或nginx plus源碼
一個代理服務器或一個代理服務器組
SSL證書和私鑰
獲取SSL服務端證書
你可以從一個可信證書頒發機構(CA)購買一個服務器證書, 或者你可以使用openssl庫創建一個內部CA, 並給自己頒發證書。這個服務器端證書和私鑰需要部署在後端的每一個服務器上。
獲取SSL客戶端證書
nignx使用一個SSL客戶端證書來對後端服務器組來標識自己。這個客戶端證書必須是被一個可信CA簽名的,並且和相匹配的私鑰一起部署在nginx中。
你還需要在後端服務器上配置好所有的來源SSL連接都需要客戶端證書,並信任這個CA頒發的nginx客戶端證書。 然後當nginx連接後端時,將提供客戶端證書,並且後端將會接收這個連接。
配置nginx
首先,改變相應URL到支持SSL連接的後端服務器組。在nginx的配置文件中,指明proxy_pass指令在代理服務器或後端服務器組中使用"https"協議:
location /upstream { proxy_pass https://backend.example.com; }
增加客戶端證書和私鑰,用於驗證nginx和每個後端服務器。使用proxy_ssl_certificate
和 proxy_ssl_certificate_key
指令:
location /upstream { proxy_pass https://backend.example.com; proxy_ssl_certificate /etc/nginx/client.pem; proxy_ssl_certificate_key /etc/nginx/client.key }
如果你在後端服務器使用了自簽名證書或者使用了自建CA,你需要配置proxy_ssl_trusted_certificate
. 這個文件必須是PEM格式的。另外還可以配置proxy_ssl_verify
和proxy_ssl_verfiy_depth
指令, 用來驗證安全證書:
location /upstream { ... proxy_ssl_trusted_certificate /etc/nginx/trusted_ca_cert.crt; proxy_ssl_verify on; proxy_ssl_verify_depth 2; ... }
每一個新的SSL連接都需要在服務端和客戶端進行一個完整的SSL握手過程,這非常耗費CPU計算資源。爲了是nignx代理預先協商連接參數,使用一種簡略的握手過程,增加proxy_ssl_session_reuse
指令配置:
location /upstream { ... proxy_ssl_session_reuse on; ... }
可選的,你也可以配置使用的SSL協議和SSL祕鑰算法:
location /upstream { ... proxy_ssl_protocols TLSv1 TLSv1.1 TLSv1.2; proxy_ssl_ciphers HIGH:!aNULL:!MD5; }
配置後端服務器
每一個後端服務器都必須配置成接受https連接。每一個後端服務器需要使用ssl_certificate
和ssl_certificate_key
指令來指定服務器證書和私鑰的文件路徑:
server { listen 443 ssl; server_name backend1.example.com; ssl_certificate /etc/ssl/certs/server.crt; ssl_certificate_key /etc/ssl/certs/server.key; ... location /yourapp { proxy_pass http://url_to_app.com; ... } }
使用ssl_client_certificat
指令來設定客戶端證書的文件路徑:
server { ... ssl_client_certificate /etc/ssl/certs/ca.crt; ssl_verify_client off; ... }
完整示例
http { ... upstream backend.example.com { server backend1.example.com:443; server backend2.example.com:443; } server { listen 80; server_name www.example.com; ... location /upstream { proxy_pass https://backend.example.com; proxy_ssl_certificate /etc/nginx/client.pem; proxy_ssl_certificate_key /etc/nginx/client.key proxy_ssl_protocols TLSv1 TLSv1.1 TLSv1.2; proxy_ssl_ciphers HIGH:!aNULL:!MD5; proxy_ssl_trusted_certificate /etc/nginx/trusted_ca_cert.crt; proxy_ssl_verify on; proxy_ssl_verify_depth 2; proxy_ssl_session_reuse on; } } server { listen 443 ssl; server_name backend1.example.com; ssl_certificate /etc/ssl/certs/server.crt; ssl_certificate_key /etc/ssl/certs/server.key; ssl_client_certificate /etc/ssl/certs/ca.crt; ssl_verify_client off; location /yourapp { proxy_pass http://url_to_app.com; ... } server { listen 443 ssl; server_name backend2.example.com; ssl_certificate /etc/ssl/certs/server.crt; ssl_certificate_key /etc/ssl/certs/server.key; ssl_client_certificate /etc/ssl/certs/ca.crt; ssl_verify_client off; location /yourapp { proxy_pass http://url_to_app.com; ... } } }
在這個示例中, proxy_pass
指令設置使用了"https"協議,所以nginx轉發到後端服務器的流量是安全的。
當一個安全的連接第一次從nginx轉發到後端服務器,將會實施一次完整的握手過程。proxy_ssl_certificate
指令設置了後端服務器需要的PEM格式證書的文件位置。proxy_ssl_certificate_key
指令設置了證書的私鑰位置。proxy_ssl_protocols
和proxy_ssl_ciphers
指令控制使用的協議和祕鑰算法。
因爲proxy_ssl_session_reuse
指令配置,當下一次nginx轉發一個連接到後端服務器時,會話參數會被重複使用,從而更快的建立安全連接。
proxy_ssl_trusted_certificate
指令設置的那個可信CA證書文件是用來驗證後端服務器的證書。proxy_ssl_verify_depth
指令指定了證書鏈檢查的深度。proxy_ssl_verify
指令驗證證書的有效性。
譯註
本文爲翻譯,英文原文地址:https://www.nginx.com/resources/admin-guide/nginx-https-upstreams/ ,儘量遵循原文,不準確的地方,還請指正,謝謝。 本文原文地址: https://my.oschina.net/foreverich/blog/1517128永福翻譯,未經授權,不得轉載!
© 著作權歸作者所有
上一篇: git自學1: git使用基礎篇
永福
粉絲 32
博文 22
碼字總數 24431
作品 0
長沙
技術主管
假設nginx中的配置是這樣的: 那麼,當用戶請求http://x.x.x.x/subdir/other時,匹配到該區塊,nginx反向代理到後端時會保留虛擬路徑。nginx實際向後端發起的請求URL爲http://y.y.y.y/subdir...
獨指蝸牛
2018/07/07
0
0
Nginx4大模塊——proxy、headers、upstream、stream
一:ngxhttpproxy_module 反向代理( reverse proxy) 方式是指用代理服務器來接受 Internet 上的連接請求, 然後將請求轉發給內部網絡中的上游服務器, 並將從上游服務器上得到的結果返...
yaohong
2018/05/27
0
0
nginx的反向代理模塊 參數proxy_pass,proxy_method,proxy_hide_
nginx的反向代理模塊有很多種配置,下面介紹一些常用的配置實例: 1.proxy_pass 語法:proxy_pass URL 配置塊:location,if 詳解:此配置項將當前請求反向代理到URL參數指定的服務器上,URL...
adbug
2016/02/24
2.9K
0
一、基於nginx將客戶端請求反向代理至後端服務器: 1、proxy_pass指令只能用於location上下文: proxypass 後面的路徑不帶URI時,將會把location的URI傳遞給後端主機,如location /img/ { pr...
the_script
2018/12/16
0
0
nginx有兩種用途 :靜態內容的web服務器和反向代理 反向代理模塊proxy屬於標準http模塊 默認編譯時是安裝的。 最重要命令是proxy_pass,其用於指定代理的協議、服務器地址和映射的URI. 下面演...