Nginx proxy_pass如何到https后端
Nginx proxy_pass到https后端
使用SSL/TLS加密,確保NGINX或NGINX Plus與上游服務器之間的HTTP流量安全。
本文解釋了如何加密NGINX和上游組或代理服務器之間的HTTP流量。
生成證書(自簽名證書需要)
生成自簽名CA證書。
openssl genrsa -out ca.key 2048 openssl req -new -key ca.key -out ca.csr openssl x509 -req -sha256 -days 365 -in ca.csr -signkey ca.key -out ca.crt
生成客戶端證書和密鑰。
openssl genrsa -out client.key 2048 openssl req -new -key client.key -out client.csr openssl x509 -req -sha256 -days 365 -in client.csr -CA ca.crt -CAkey ca.key -set_serial 01 -out client.crt
生成服務器證書和密鑰。
openssl genrsa -out server.key 2048 openssl req -new -key server.key -out server.csr openssl x509 -req -sha256 -days 365 -in server.csr -CA ca.crt -CAkey ca.key -set_serial 01 -out server.crt
配置NGINX
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; # // 驗證上游服務器時,使用的ca證書 proxy_ssl_trusted_certificate /etc/nginx/trusted_ca_cert.crt; proxy_ssl_verify on; proxy_ssl_verify_depth 2; # 每次連接都需要完整的ssl握手,消耗cpu較大,加上此參數,可以復用連接,減少握手次數 proxy_ssl_session_reuse on; proxy_ssl_server_name on; # 次參數不是太懂 proxy_ssl_name backend.example.com; } } server { listen 443 ssl; server_name backend1.example.com; ssl_certificate /etc/ssl/certs/server.crt; ssl_certificate_key /etc/ssl/certs/server.key; # 驗證客戶端使用的CA證書 ssl_client_certificate /etc/ssl/certs/ca.crt; # 開啟驗證客戶端 ssl_verify_client on; 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 on; location /yourapp { proxy_pass http://url_to_app.com; #... } } }
官網鏈接
Nginx proxy_pass后出現部分請求404
有一個后端服務,地址是 http://127.0.0.1:8888/web-test/
nginx 配置如下:
location ^~ /web-test/ { proxy_pass http://127.0.0.1:8888; }
訪問 http://ip:port/web-test/
是正常的
但我想改寫成訪問 http://ip:port/test/
替代訪問 http://ip:port/web-test/
nginx 配置如下:
location ^~ /test/ { proxy_pass http://127.0.0.1:8888/web-test/; }
重寫后訪問 http://ip:port/test/
后出現問題,看到請求下面的 靜態(tài)資源URL 都返回 404,也就是說頁面的靜態(tài)資源發(fā)起的請求還是訪問了原來的 /web-test 路由
http://127.0.0.1:8888/web-test/resource/... http://127.0.0.1:8888/web-test/image/... http://127.0.0.1:8888/web-test/system/... http://127.0.0.1:8888/web-test/其他 URI/...
這種情況 nginx 要怎樣配置重寫規(guī)則?訪問 http://ip:port/test/
能返回正常的請求
解決辦法
1、
location ^~ /test/ { proxy_pass http://127.0.0.1:8888/web-test/; }
其他 靜態(tài)資源URI 請求也 proxy_pass 到后端服務
location /web-test/resource/ { proxy_pass http://127.0.0.1:8888; } location /web-test/image/ { proxy_pass http://127.0.0.1:8888; }
location /其他靜態(tài)資源URI請求 { proxy_pass http://127.0.0.1:8888; }
這種方式要找到所有的 靜態(tài)資源URL 請求,一個個重寫
2、直接修改后端服務路由為 http://127.0.0.1:8888/test/,規(guī)則變?yōu)?/p>
location ^~ /test/ { proxy_pass http://127.0.0.1:8888; }
就可以了
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- Nginx中l(wèi)ocation proxy_pass加與不加/的區(qū)別說明
- Nginx?location和proxy_pass配置示例詳解
- Nginx中proxy_pass的斜杠的兩種方式
- Nginx的location路徑與proxy_pass匹配規(guī)則說明
- Nginx捕獲并自定義proxy_pass返回的錯誤問題
- nginx中如何配置proxy_pass
- Nginx rewrite和proxy_pass的區(qū)別及說明
- nginx代理參數proxy_pass的實現
- nginx反向代理proxy_pass遇到的死循環(huán)問題
- 解決nginx配置proxy_pass之后,響應變慢的問題
- Nginx使用if指令實現多個proxy_pass方式
- Nginx中proxy_pass使用小結
相關文章
Nginx+RTMP+nginx-http-flv-module環(huán)境搭建
本文主要介紹了Nginx+RTMP+nginx-http-flv-module環(huán)境搭建,搭建方式可用于直播、視頻會議等場景,同時支持HTTP-FLV,方便在瀏覽器中進行播放2024-03-03