Nginx代理配置詳解:正向代理與反向代理完全指南

系列文章索引:

  • 第一篇:《Nginx入門與安裝詳解:從零開始搭建高性能Web服務器》
  • 第二篇:《Nginx基礎配置詳解:nginx.conf核心配置與虛擬主機實戰》
  • 第三篇:《Nginx代理配置詳解:正向代理與反向代理完全指南》
  • 第四篇:《Nginx性能優化與安全配置:打造高性能Web服務器》
  • 第五篇:《Nginx負載均衡配置詳解:多種負載均衡策略實戰》
  • 第六篇:《Nginx高可用方案實戰:Keepalived+雙機熱備部署》

前言

在現代網絡架構中,代理服務器扮演著至關重要的角色。Nginx作為一款高性能的Web服務器,其代理功能被廣泛應用于各種場景,從企業內網訪問控制到大型網站負載均衡。本文將深入探討Nginx的正向代理和反向代理配置,通過實際案例幫助你掌握代理配置的核心技能。

代理服務器本質上是一個中間人,負責在客戶端和服務器之間傳遞請求和響應。根據代理的方向不同,可以分為正向代理和反向代理,它們在應用場景和工作原理上有著本質的區別。

一、代理服務器基礎概念

1.1 什么是代理服務器

代理服務器(Proxy Server)是位于客戶端和目標服務器之間的中間服務器,它接收客戶端的請求,然后轉發給目標服務器,并將服務器的響應返回給客戶端。

代理服務器的基本功能:

  • 請求轉發:將客戶端請求轉發到目標服務器
  • 響應緩存:緩存服務器響應,提高訪問速度
  • 訪問控制:控制客戶端對特定資源的訪問
  • 內容過濾:過濾不合適的內容
  • 安全防護:隱藏真實IP地址,提供安全屏障

1.2 正向代理 vs 反向代理

正向代理(Forward Proxy)

工作原理:

  • 客戶端明確知道代理服務器的存在
  • 客戶端配置代理服務器地址
  • 代理服務器代表客戶端訪問外部網絡
  • 服務器不知道真實客戶端的IP地址

應用場景:

  • 企業內網訪問外網
  • 突破網絡訪問限制
  • 訪問控制與審計
  • 緩存加速

工作流程:

客戶端 → 代理服務器 → 目標服務器
反向代理(Reverse Proxy)

工作原理:

  • 客戶端不知道代理服務器的存在
  • 客戶端直接訪問代理服務器
  • 代理服務器代表服務器接收客戶端請求
  • 客戶端不知道真實服務器的IP地址

應用場景:

  • 負載均衡
  • 安全防護
  • SSL卸載
  • 緩存加速

工作流程:

客戶端 ← 代理服務器 ← 目標服務器
對比總結
特性正向代理反向代理
服務對象客戶端服務器
配置位置客戶端服務器端
隱藏對象客戶端IP服務器IP
典型應用翻墻、訪問控制負載均衡、安全防護
配置復雜度簡單復雜
性能要求一般

1.3 Nginx代理模塊介紹

Nginx提供了多個代理相關的模塊:

核心代理模塊:

  • ngx_http_proxy_module:HTTP反向代理模塊
  • ngx_http_upstream_module:上游服務器定義模塊
  • ngx_stream_proxy_module:TCP/UDP代理模塊

功能增強模塊:

  • ngx_http_proxy_connect_module:HTTPS正向代理支持
  • ngx_http_headers_module:HTTP頭部處理模塊
  • ngx_http_cache_module:緩存模塊
  • ngx_http_ssl_module:SSL支持模塊

二、正向代理配置詳解

2.1 HTTP正向代理配置

基礎HTTP正向代理

配置文件:/usr/local/nginx/conf/conf.d/forward-proxy.conf

# =============================================
# HTTP正向代理配置
# 監聽端口:3128
# =============================================server {# 監聽代理端口listen 3128;# 服務器名稱(可選)server_name proxy.example.com;# 解析器配置(DNS服務器)resolver 8.8.8.8 8.8.4.4 114.114.114.114;# 解析器超時時間resolver_timeout 30s;# 訪問日志access_log /var/log/nginx/proxy.access.log main;# 錯誤日志error_log /var/log/nginx/proxy.error.log warn;# =============================================# 正向代理配置# =============================================location / {# 代理目標地址# $http_host: 請求的主機名# $request_uri: 請求的URIproxy_pass http://$http_host$request_uri;# 設置代理頭信息proxy_set_header Host $http_host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;# 代理超時設置proxy_connect_timeout 30s;proxy_send_timeout 60s;proxy_read_timeout 60s;# 代理緩沖區設置proxy_buffering on;proxy_buffer_size 4k;proxy_buffers 8 4k;proxy_busy_buffers_size 8k;# 代理臨時文件路徑proxy_temp_path /usr/local/nginx/proxy_temp;# 代理緩存路徑proxy_cache_path /usr/local/nginx/proxy_cache levels=1:2 keys_zone=proxy_cache:10m inactive=60m use_temp_path=off;# 啟用代理緩存proxy_cache proxy_cache;# 緩存有效期proxy_cache_valid 200 302 10m;proxy_cache_valid 404 1m;# 緩存鍵proxy_cache_key $scheme$proxy_host$request_uri;# 緩存狀態頭add_header X-Proxy-Cache $upstream_cache_status;}# =============================================# 訪問控制配置# =============================================# 限制訪問IP(可選)allow 192.168.1.0/24;allow 10.0.0.0/8;deny all;# =============================================# 錯誤處理# =============================================# 代理連接錯誤處理error_page 502 503 504 /proxy_error.html;location = /proxy_error.html {root /usr/local/nginx/html;internal;}
}
帶認證的HTTP正向代理
# =============================================
# 帶認證的HTTP正向代理配置
# =============================================server {listen 3128;server_name proxy.example.com;resolver 8.8.8.8 8.8.4.4;access_log /var/log/nginx/proxy.auth.access.log main;error_log /var/local/nginx/proxy.auth.error.log warn;# =============================================# 基本認證配置# =============================================# 啟用HTTP基本認證auth_basic "Proxy Authentication";auth_basic_user_file /usr/local/nginx/conf/htpasswd.proxy;# =============================================# 代理配置# =============================================location / {# 檢查認證狀態if ($remote_user = "") {return 401;}# 代理目標地址proxy_pass http://$http_host$request_uri;# 設置代理頭信息proxy_set_header Host $http_host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;# 添加用戶信息到代理頭proxy_set_header X-Proxy-User $remote_user;# 代理超時設置proxy_connect_timeout 30s;proxy_send_timeout 60s;proxy_read_timeout 60s;# 代理緩沖區設置proxy_buffering on;proxy_buffer_size 4k;proxy_buffers 8 4k;proxy_busy_buffers_size 8k;# 代理緩存配置proxy_cache_path /usr/local/nginx/proxy_cache levels=1:2 keys_zone=proxy_cache:10m inactive=60m;proxy_cache proxy_cache;proxy_cache_valid 200 302 10m;proxy_cache_valid 404 1m;proxy_cache_key $scheme$proxy_host$request_uri;# 緩存狀態頭add_header X-Proxy-Cache $upstream_cache_status;# 訪問日志記錄用戶access_log /var/log/nginx/proxy.auth.access.log main proxy=$upstream_addr user=$remote_user;}# =============================================# 訪問控制# =============================================# 允許特定網段訪問allow 192.168.1.0/24;allow 10.0.0.0/8;deny all;
}

2.2 HTTPS正向代理配置

基礎HTTPS正向代理

配置文件:/usr/local/nginx/conf/conf.d/forward-proxy-https.conf

# =============================================
# HTTPS正向代理配置
# 監聽端口:3129
# 注意:需要ngx_http_proxy_connect_module模塊支持
# =============================================server {# 監聽HTTPS代理端口listen 3129;# 服務器名稱server_name proxy.example.com;# DNS解析器resolver 8.8.8.8 8.8.4.4 114.114.114.114;resolver_timeout 30s;# 訪問日志access_log /var/log/nginx/proxy.https.access.log main;# 錯誤日志error_log /var/log/nginx/proxy.https.error.log warn;# =============================================# HTTPS代理配置# =============================================location / {# HTTPS代理需要特殊處理proxy_pass https://$http_host$request_uri;# SSL相關配置proxy_ssl_server_name on;proxy_ssl_protocols TLSv1.2 TLSv1.3;proxy_ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384;proxy_ssl_session_reuse on;proxy_ssl_trusted_certificate /etc/ssl/certs/ca-certificates.crt;# 設置代理頭信息proxy_set_header Host $http_host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;# 代理超時設置proxy_connect_timeout 30s;proxy_send_timeout 60s;proxy_read_timeout 60s;# SSL連接超時proxy_ssl_timeout 60s;# 代理緩沖區設置proxy_buffering on;proxy_buffer_size 4k;proxy_buffers 8 4k;proxy_busy_buffers_size 8k;# 代理臨時文件路徑proxy_temp_path /usr/local/nginx/proxy_temp;# 禁用緩存(HTTPS通常不緩存)proxy_cache off;# 添加SSL信息到日志add_header X-Proxy-SSL $proxy_ssl_server_name;}# =============================================# CONNECT方法處理(HTTPS握手)# =============================================# 處理CONNECT方法(用于HTTPS握手)location /connect {# 啟用CONNECT方法支持proxy_connect_address $http_host:443;proxy_connect_connect_timeout 30s;proxy_connect_read_timeout 60s;proxy_connect_send_timeout 60s;# SSL配置proxy_ssl_server_name on;proxy_ssl_protocols TLSv1.2 TLSv1.3;proxy_ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384;# 代理頭信息proxy_set_header Host $http_host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;# 超時設置proxy_connect_timeout 30s;proxy_send_timeout 60s;proxy_read_timeout 60s;}# =============================================# 訪問控制# =============================================# 限制訪問IPallow 192.168.1.0/24;allow 10.0.0.0/8;deny all;# =============================================# 錯誤處理# =============================================# SSL連接錯誤處理error_page 497 495 496 /proxy_ssl_error.html;location = /proxy_ssl_error.html {root /usr/local/nginx/html;internal;}# 代理連接錯誤處理error_page 502 503 504 /proxy_error.html;location = /proxy_error.html {root /usr/local/nginx/html;internal;}
}
帶緩存的HTTPS正向代理
# =============================================
# 帶緩存的HTTPS正向代理配置
# =============================================server {listen 3129;server_name proxy.example.com;resolver 8.8.8.8 8.8.4.4;resolver_timeout 30s;access_log /var/log/nginx/proxy.https.cache.access.log main;error_log /var/log/nginx/proxy.https.cache.error.log warn;# =============================================# 緩存配置# =============================================# HTTPS代理緩存路徑proxy_cache_path /usr/local/nginx/proxy_https_cache levels=1:2 keys_zone=proxy_https_cache:20m inactive=120m use_temp_path=off;# =============================================# 代理配置# =============================================location / {# HTTPS代理proxy_pass https://$http_host$request_uri;# SSL配置proxy_ssl_server_name on;proxy_ssl_protocols TLSv1.2 TLSv1.3;proxy_ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384;proxy_ssl_session_reuse on;proxy_ssl_trusted_certificate /etc/ssl/certs/ca-certificates.crt;# 代理頭信息proxy_set_header Host $http_host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;# 超時設置proxy_connect_timeout 30s;proxy_send_timeout 60s;proxy_read_timeout 60s;proxy_ssl_timeout 60s;# 緩沖區設置proxy_buffering on;proxy_buffer_size 8k;proxy_buffers 8 8k;proxy_busy_buffers_size 16k;# 啟用緩存proxy_cache proxy_https_cache;# 緩存條件:只緩存成功的響應proxy_cache_valid 200 302 30m;proxy_cache_valid 301 1h;proxy_cache_valid 404 1m;proxy_cache_valid 500 502 503 504 0s;# 緩存鍵proxy_cache_key $scheme$proxy_host$request_uri;# 緩存控制proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;proxy_cache_lock on;proxy_cache_lock_timeout 5s;# 緩存狀態頭add_header X-Proxy-Cache $upstream_cache_status;# 繞過緩存的條件proxy_cache_bypass $cookie_nocache $arg_nocache $arg_comment;proxy_no_cache $cookie_nocache $arg_nocache $arg_comment;}# =============================================# 特殊資源緩存配置# =============================================# 靜態資源緩存location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff|woff2|ttf|eot)$ {proxy_pass https://$http_host$request_uri;# SSL配置proxy_ssl_server_name on;proxy_ssl_protocols TLSv1.2 TLSv1.3;# 代理頭信息proxy_set_header Host $http_host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;# 靜態資源緩存時間更長proxy_cache proxy_https_cache;proxy_cache_valid 200 302 24h;proxy_cache_valid 404 1m;proxy_cache_key $scheme$proxy_host$request_uri;# 緩存狀態頭add_header X-Proxy-Cache $upstream_cache_status;# 瀏覽器緩存控制add_header Cache-Control "public, max-age=86400";# 關閉訪問日志access_log off;}# =============================================# 訪問控制# =============================================allow 192.168.1.0/24;allow 10.0.0.0/8;deny all;
}

2.3 正向代理客戶端配置

Windows客戶端配置

Internet Explorer/Edge:

  1. 打開IE設置 → Internet選項
  2. 選擇"連接"選項卡
  3. 點擊"局域網設置"
  4. 勾選"為LAN使用代理服務器"
  5. 輸入代理服務器地址和端口
  6. 點擊"確定"保存

Chrome瀏覽器:

  1. 打開設置 → 高級 → 系統
  2. 點擊"打開您計算機的代理設置"
  3. 配置代理服務器地址和端口

Firefox瀏覽器:

  1. 打開設置 → 常規 → 網絡設置
  2. 選擇"手動代理配置"
  3. 輸入HTTP代理和HTTPS代理
  4. 勾選"同時用于HTTPS"
Linux客戶端配置

環境變量方式:

# 設置HTTP代理
export http_proxy="http://proxy.example.com:3128"
export https_proxy="http://proxy.example.com:3129"# 設置FTP代理
export ftp_proxy="http://proxy.example.com:3128"# 設置不使用代理的地址
export no_proxy="localhost,127.0.0.1,*.local"# 永久生效(添加到~/.bashrc或/etc/profile)
echo 'export http_proxy="http://proxy.example.com:3128"' >> ~/.bashrc
echo 'export https_proxy="http://proxy.example.com:3129"' >> ~/.bashrc
source ~/.bashrc

APT/YUM包管理器配置:

# APT代理配置(Ubuntu/Debian)
cat > /etc/apt/apt.conf.d/01proxy << EOF
Acquire::http::Proxy "http://proxy.example.com:3128";
Acquire::https::Proxy "http://proxy.example.com:3129";
EOF# YUM代理配置(CentOS/RHEL)
cat > /etc/yum.conf << EOF
[main]
proxy=http://proxy.example.com:3128
EOF
macOS客戶端配置

系統代理設置:

  1. 打開系統偏好設置 → 網絡
  2. 選擇當前網絡連接 → 高級
  3. 選擇"代理"選項卡
  4. 配置HTTP和HTTPS代理
  5. 點擊"確定"保存

命令行配置:

# 設置網絡代理
networksetup -setwebproxy Wi-Fi proxy.example.com 3128
networksetup -setsecurewebproxy Wi-Fi proxy.example.com 3129# 設置代理認證
networksetup -setwebproxy Wi-Fi proxy.example.com 3128 on username password
networksetup -setsecurewebproxy Wi-Fi proxy.example.com 3129 on username password

三、反向代理配置詳解

3.1 基礎反向代理配置

單后端服務器反向代理

配置文件:/usr/local/nginx/conf/conf.d/reverse-proxy-basic.conf

# =============================================
# 基礎反向代理配置
# 監聽端口:80
# 后端服務器:127.0.0.1:8080
# =============================================server {# 監聽端口listen 80;# 服務器名稱server_name web.example.com;# 網站根目錄(可選)root /usr/local/nginx/html/web.example.com;# 默認首頁文件index index.html index.htm;# 字符集設置charset utf-8;# 訪問日志access_log /var/log/nginx/web.example.com.access.log main;# 錯誤日志error_log /var/log/nginx/web.example.com.error.log warn;# =============================================# 反向代理配置# =============================================location / {# 后端服務器地址proxy_pass http://127.0.0.1:8080;# 設置代理頭信息proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;proxy_set_header X-Forwarded-Host $host;proxy_set_header X-Forwarded-Port $server_port;# 連接超時設置proxy_connect_timeout 60s;proxy_send_timeout 60s;proxy_read_timeout 60s;# 代理緩沖區設置proxy_buffering on;proxy_buffer_size 4k;proxy_buffers 8 4k;proxy_busy_buffers_size 8k;# 代理臨時文件路徑proxy_temp_path /usr/local/nginx/proxy_temp;# 代理重定向設置proxy_redirect off;# Cookie設置proxy_cookie_domain off;proxy_cookie_path off;# HTTP版本設置proxy_http_version 1.1;proxy_set_header Connection "";# 客戶端請求體大小client_max_body_size 50m;client_body_buffer_size 128k;}# =============================================# 靜態文件處理# =============================================# 靜態文件直接由Nginx處理location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff|woff2|ttf|eot|svg)$ {# 嘗試訪問本地文件try_files $uri =404;# 設置緩存頭expires 7d;add_header Cache-Control "public, no-transform";# 關閉訪問日志access_log off;}# =============================================# 健康檢查# =============================================# 健康檢查端點location /health {access_log off;return 200 "healthy\n";add_header Content-Type text/plain;}# =============================================# 錯誤處理# =============================================# 錯誤頁面error_page 404 /404.html;error_page 500 502 503 504 /50x.html;location = /50x.html {root /usr/local/nginx/html;}
}
多后端服務器反向代理

配置文件:/usr/local/nginx/conf/conf.d/reverse-proxy-multiple.conf

# =============================================
# 多后端服務器反向代理配置
# 監聽端口:80
# 后端服務器組:backend_servers
# =============================================# 定義后端服務器組
upstream backend_servers {# 后端服務器列表server 192.168.1.10:8080 weight=5 max_fails=3 fail_timeout=30s;server 192.168.1.11:8080 weight=3 max_fails=3 fail_timeout=30s;server 192.168.1.12:8080 weight=2 max_fails=3 fail_timeout=30s backup;# 負載均衡方法# least_conn;  # 最少連接# ip_hash;     # IP哈希# 保持連接設置keepalive 32;keepalive_timeout 30s;keepalive_requests 1000;
}server {listen 80;server_name api.example.com;access_log /var/log/nginx/api.example.com.access.log main;error_log /var/log/nginx/api.example.com.error.log warn;# =============================================# 反向代理配置# =============================================location / {# 代理到后端服務器組proxy_pass http://backend_servers;# 代理頭信息proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;proxy_set_header X-Forwarded-Host $host;proxy_set_header X-Forwarded-Port $server_port;# 連接設置proxy_http_version 1.1;proxy_set_header Connection "";# 超時設置proxy_connect_timeout 60s;proxy_send_timeout 60s;proxy_read_timeout 60s;# 緩沖區設置proxy_buffering on;proxy_buffer_size 4k;proxy_buffers 8 4k;proxy_busy_buffers_size 8k;# 重定向設置proxy_redirect off;# Cookie設置proxy_cookie_domain off;proxy_cookie_path off;# 請求體大小client_max_body_size 100m;client_body_buffer_size 128k;# 代理緩存配置proxy_cache_path /usr/local/nginx/proxy_cache levels=1:2 keys_zone=api_cache:10m inactive=60m;proxy_cache api_cache;proxy_cache_valid 200 302 5m;proxy_cache_valid 404 1m;proxy_cache_key $scheme$request_method$host$request_uri;# 緩存控制proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;proxy_cache_lock on;proxy_cache_lock_timeout 5s;# 緩存狀態頭add_header X-Proxy-Cache $upstream_cache_status;# 繞過緩存proxy_cache_bypass $cookie_nocache $arg_nocache $arg_comment;proxy_no_cache $cookie_nocache $arg_nocache $arg_comment;}# =============================================# API路徑配置# =============================================# API v1路徑location /api/v1/ {proxy_pass http://backend_servers;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;# API特定設置proxy_connect_timeout 30s;proxy_send_timeout 30s;proxy_read_timeout 30s;# API緩存proxy_cache api_cache;proxy_cache_valid 200 302 1m;proxy_cache_key $scheme$request_method$host$request_uri;# CORS設置add_header 'Access-Control-Allow-Origin' '*' always;add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization' always;# 處理OPTIONS請求if ($request_method = 'OPTIONS') {add_header 'Access-Control-Allow-Origin' '*';add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization';add_header 'Access-Control-Max-Age' 1728000;add_header 'Content-Type' 'text/plain; charset=utf-8';add_header 'Content-Length' 0;return 204;}}# =============================================# 靜態資源# =============================================location ~* \.(css|js|jpg|jpeg|png|gif|ico|woff|woff2|ttf|eot|svg)$ {# 嘗試本地文件try_files $uri =404;# 緩存設置expires 30d;add_header Cache-Control "public, no-transform";# 關閉日志access_log off;}# =============================================# 健康檢查# =============================================location /health {access_log off;proxy_pass http://backend_servers/health;proxy_connect_timeout 5s;proxy_read_timeout 5s;}
}

3.2 帶負載均衡的反向代理

輪詢負載均衡
# =============================================
# 輪詢負載均衡配置
# =============================================# 定義后端服務器組(輪詢方式)
upstream backend_round_robin {# 輪詢方式(默認)server 192.168.1.10:8080;server 192.168.1.11:8080;server 192.168.1.12:8080;# 連接保持設置keepalive 32;keepalive_timeout 30s;keepalive_requests 1000;
}server {listen 80;server_name lb.example.com;access_log /var/log/nginx/lb.example.com.access.log main;error_log /var/log/nginx/lb.example.com.error.log warn;location / {proxy_pass http://backend_round_robin;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;proxy_http_version 1.1;proxy_set_header Connection "";proxy_connect_timeout 60s;proxy_send_timeout 60s;proxy_read_timeout 60s;proxy_buffering on;proxy_buffer_size 4k;proxy_buffers 8 4k;proxy_busy_buffers_size 8k;}
}
加權輪詢負載均衡
# =============================================
# 加權輪詢負載均衡配置
# =============================================# 定義后端服務器組(加權輪詢)
upstream backend_weighted {# 權重分配,數值越大分配到的請求越多server 192.168.1.10:8080 weight=5;    # 50%的請求server 192.168.1.11:8080 weight=3;    # 30%的請求server 192.168.1.12:8080 weight=2;    # 20%的請求# 健康檢查設置server 192.168.1.10:8080 max_fails=3 fail_timeout=30s;server 192.168.1.11:8080 max_fails=3 fail_timeout=30s;server 192.168.1.12:8080 max_fails=3 fail_timeout=30s;# 連接保持keepalive 32;keepalive_timeout 30s;keepalive_requests 1000;
}server {listen 80;server_name weighted.example.com;access_log /var/log/nginx/weighted.example.com.access.log main;error_log /var/log/nginx/weighted.example.com.error.log warn;location / {proxy_pass http://backend_weighted;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;proxy_http_version 1.1;proxy_set_header Connection "";proxy_connect_timeout 60s;proxy_send_timeout 60s;proxy_read_timeout 60s;# 添加負載均衡信息到日志add_header X-Upstream-Addr $upstream_addr;add_header X-Upstream-Response-Time $upstream_response_time;}
}
IP哈希負載均衡
# =============================================
# IP哈希負載均衡配置
# =============================================# 定義后端服務器組(IP哈希)
upstream backend_ip_hash {# IP哈希方式,確保同一客戶端請求始終轉發到同一服務器ip_hash;server 192.168.1.10:8080;server 192.168.1.11:8080;server 192.168.1.12:8080;# 健康檢查server 192.168.1.10:8080 max_fails=3 fail_timeout=30s;server 192.168.1.11:8080 max_fails=3 fail_timeout=30s;server 192.168.1.12:8080 max_fails=3 fail_timeout=30s;# 連接保持keepalive 32;keepalive_timeout 30s;keepalive_requests 1000;
}server {listen 80;server_name iphash.example.com;access_log /var/log/nginx/iphash.example.com.access.log main;error_log /var/log/nginx/iphash.example.com.error.log warn;location / {proxy_pass http://backend_ip_hash;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;proxy_http_version 1.1;proxy_set_header Connection "";proxy_connect_timeout 60s;proxy_send_timeout 60s;proxy_read_timeout 60s;# 添加客戶端哈希信息add_header X-Client-Hash $remote_addr;add_header X-Upstream-Addr $upstream_addr;}
}
最少連接負載均衡
# =============================================
# 最少連接負載均衡配置
# =============================================# 定義后端服務器組(最少連接)
upstream backend_least_conn {# 最少連接方式,將請求轉發到連接數最少的服務器least_conn;server 192.168.1.10:8080;server 192.168.1.11:8080;server 192.168.1.12:8080;# 健康檢查server 192.168.1.10:8080 max_fails=3 fail_timeout=30s;server 192.168.1.11:8080 max_fails=3 fail_timeout=30s;server 192.168.1.12:8080 max_fails=3 fail_timeout=30s;# 連接保持keepalive 32;keepalive_timeout 30s;keepalive_requests 1000;
}server {listen 80;server_name leastconn.example.com;access_log /var/log/nginx/leastconn.example.com.access.log main;error_log /var/log/nginx/leastconn.example.com.error.log warn;location / {proxy_pass http://backend_least_conn;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;proxy_http_version 1.1;proxy_set_header Connection "";proxy_connect_timeout 60s;proxy_send_timeout 60s;proxy_read_timeout 60s;# 添加連接數信息add_header X-Upstream-Addr $upstream_addr;add_header X-Upstream-Connections $upstream_connections;}
}

3.3 帶緩存的反向代理

基礎緩存配置
# =============================================
# 帶緩存的反向代理配置
# =============================================# 定義緩存路徑和參數
proxy_cache_path /usr/local/nginx/proxy_cache levels=1:2 keys_zone=cache_zone:10m inactive=60m use_temp_path=off;
proxy_cache_path /usr/local/nginx/proxy_cache_api levels=1:2 keys_zone=api_cache:20m inactive=120m use_temp_path=off;server {listen 80;server_name cache.example.com;access_log /var/log/nginx/cache.example.com.access.log main;error_log /var/log/nginx/cache.example.com.error.log warn;# =============================================# 基礎緩存配置# =============================================location / {# 后端服務器proxy_pass http://127.0.0.1:8080;# 代理頭信息proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;# 緩存設置proxy_cache cache_zone;proxy_cache_valid 200 302 10m;proxy_cache_valid 301 1h;proxy_cache_valid 404 1m;proxy_cache_valid 500 502 503 504 0s;# 緩存鍵proxy_cache_key $scheme$request_method$host$request_uri;# 緩存控制proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;proxy_cache_lock on;proxy_cache_lock_timeout 5s;# 緩存狀態頭add_header X-Proxy-Cache $upstream_cache_status;# 繞過緩存proxy_cache_bypass $cookie_nocache $arg_nocache $arg_comment;proxy_no_cache $cookie_nocache $arg_nocache $arg_comment;}# =============================================# API緩存配置# =============================================location /api/ {proxy_pass http://127.0.0.1:8080;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;# API專用緩存proxy_cache api_cache;proxy_cache_valid 200 302 5m;proxy_cache_valid 404 1m;proxy_cache_key $scheme$request_method$host$request_uri;# API緩存控制proxy_cache_use_stale error timeout updating;proxy_cache_lock on;proxy_cache_lock_timeout 3s;# CORS設置add_header 'Access-Control-Allow-Origin' '*' always;add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization' always;# 緩存狀態頭add_header X-Proxy-Cache $upstream_cache_status;}# =============================================# 靜態資源緩存# =============================================location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff|woff2|ttf|eot|svg)$ {proxy_pass http://127.0.0.1:8080;proxy_set_header Host $host;# 靜態資源緩存proxy_cache cache_zone;proxy_cache_valid 200 302 24h;proxy_cache_valid 404 1m;proxy_cache_key $scheme$request_method$host$request_uri;# 瀏覽器緩存控制expires 30d;add_header Cache-Control "public, no-transform";# 緩存狀態頭add_header X-Proxy-Cache $upstream_cache_status;# 關閉訪問日志access_log off;}# =============================================# 緩存清理接口# =============================================location /purge/ {# 限制訪問IPallow 127.0.0.1;allow 192.168.1.0/24;deny all;# 緩存清理proxy_cache_purge cache_zone $scheme$request_method$host$request_uri;proxy_cache_purge api_cache $scheme$request_method$host$request_uri;# 返回清理結果add_header Content-Type "text/plain";return 200 "Cache purged\n";}
}
高級緩存配置
# =============================================
# 高級緩存配置
# =============================================# 定義多個緩存區域
proxy_cache_path /usr/local/nginx/proxy_cache_static levels=1:2 keys_zone=static_cache:50m inactive=24h use_temp_path=off;
proxy_cache_path /usr/local/nginx/proxy_cache_api levels=1:2 keys_zone=api_cache:100m inactive=2h use_temp_path=off;
proxy_cache_path /usr/local/nginx/proxy_cache_dynamic levels=1:2 keys_zone=dynamic_cache:200m inactive=1h use_temp_path=off;server {listen 80;server_name advanced-cache.example.com;access_log /var/log/nginx/advanced-cache.example.com.access.log main;error_log /var/log/nginx/advanced-cache.example.com.error.log warn;# =============================================# 緩存條件變量# =============================================# 定義緩存條件變量map $request_method $no_cache_method {POST 1;PUT 1;DELETE 1;PATCH 1;default 0;}map $cookie_user_token $no_cache_auth {default 0;"~*" 1;}map $arg_nocache $no_cache_arg {default 0;"1" 1;"true" 1;}# =============================================# 靜態資源緩存# =============================================location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff|woff2|ttf|eot|svg)$ {proxy_pass http://127.0.0.1:8080;proxy_set_header Host $host;# 靜態資源緩存proxy_cache static_cache;proxy_cache_valid 200 302 7d;proxy_cache_valid 404 1h;proxy_cache_key $scheme$request_method$host$request_uri;# 緩存控制proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;proxy_cache_lock on;proxy_cache_lock_timeout 5s;# 瀏覽器緩存expires 30d;add_header Cache-Control "public, no-transform";# 緩存狀態add_header X-Proxy-Cache $upstream_cache_status;# 關閉日志access_log off;}# =============================================# API緩存# =============================================location /api/ {proxy_pass http://127.0.0.1:8080;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;# API緩存proxy_cache api_cache;proxy_cache_valid 200 302 10m;proxy_cache_valid 404 1m;proxy_cache_key $scheme$request_method$host$request_uri;# 緩存控制proxy_cache_use_stale error timeout updating;proxy_cache_lock on;proxy_cache_lock_timeout 3s;# 條件緩存proxy_no_cache $no_cache_method $no_cache_auth $no_cache_arg;proxy_cache_bypass $no_cache_method $no_cache_auth $no_cache_arg;# CORSadd_header 'Access-Control-Allow-Origin' '*' always;add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization' always;# 緩存狀態add_header X-Proxy-Cache $upstream_cache_status;add_header X-Cache-Condition "method=$no_cache_method,auth=$no_cache_auth,arg=$no_cache_arg";}# =============================================# 動態內容緩存# =============================================location /dynamic/ {proxy_pass http://127.0.0.1:8080;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;# 動態內容緩存proxy_cache dynamic_cache;proxy_cache_valid 200 302 1m;proxy_cache_valid 404 30s;proxy_cache_key $scheme$request_method$host$request_uri;# 緩存控制proxy_cache_use_stale error timeout updating;proxy_cache_lock on;proxy_cache_lock_timeout 2s;# 條件緩存(更嚴格)proxy_no_cache $no_cache_method $no_cache_auth $no_cache_arg;proxy_cache_bypass $no_cache_method $no_cache_auth $no_cache_arg;# 緩存狀態add_header X-Proxy-Cache $upstream_cache_status;}# =============================================# 緩存統計接口# =============================================location /cache_status/ {# 限制訪問allow 127.0.0.1;allow 192.168.1.0/24;deny all;# 返回緩存統計信息add_header Content-Type "application/json";return 200 '{"static_cache": {"size": "50MB","inactive": "24h"},"api_cache": {"size": "100MB","inactive": "2h"},"dynamic_cache": {"size": "200MB","inactive": "1h"}}';}# =============================================# 緩存清理接口# =============================================location /purge/ {# 限制訪問allow 127.0.0.1;allow 192.168.1.0/24;deny all;# 根據URL清理緩存location ~ ^/purge/static/(.*)$ {proxy_cache_purge static_cache $scheme$request_method$host/$1;}location ~ ^/purge/api/(.*)$ {proxy_cache_purge api_cache $scheme$request_method$host/$1;}location ~ ^/purge/dynamic/(.*)$ {proxy_cache_purge dynamic_cache $scheme$request_method$host/$1;}# 返回清理結果add_header Content-Type "text/plain";return 200 "Cache purged\n";}
}

四、代理配置高級應用

4.1 SSL/TLS終止

HTTPS反向代理配置
# =============================================
# HTTPS反向代理配置(SSL終止)
# =============================================server {# 監聽443端口(HTTPS)listen 443 ssl http2;listen [::]:443 ssl http2;# 服務器名稱server_name secure.example.com;# SSL證書配置ssl_certificate /usr/local/nginx/conf/ssl/secure.example.com.crt;ssl_certificate_key /usr/local/nginx/conf/ssl/secure.example.com.key;# SSL協議和加密套件ssl_protocols TLSv1.2 TLSv1.3;ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA384;ssl_prefer_server_ciphers on;# SSL會話配置ssl_session_cache shared:SSL:10m;ssl_session_timeout 10m;ssl_session_tickets on;# OCSP裝訂ssl_stapling on;ssl_stapling_verify on;ssl_trusted_certificate /usr/local/nginx/conf/ssl/chain.pem;# HSTSadd_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;access_log /var/log/nginx/secure.example.com.access.log main;error_log /var/log/nginx/secure.example.com.error.log warn;# =============================================# 反向代理配置# =============================================location / {# 后端服務器(HTTP)proxy_pass http://127.0.0.1:8080;# 代理頭信息proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;proxy_set_header X-Forwarded-SSL $ssl_protocol;proxy_set_header X-Forwarded-SSL-Cipher $ssl_cipher;# 連接設置proxy_http_version 1.1;proxy_set_header Connection "";# 超時設置proxy_connect_timeout 60s;proxy_send_timeout 60s;proxy_read_timeout 60s;# 緩沖區設置proxy_buffering on;proxy_buffer_size 4k;proxy_buffers 8 4k;proxy_busy_buffers_size 8k;# 設置HTTPS參數proxy_set_header HTTPS on;proxy_set_header HTTP_SCHEME https;}# =============================================# WebSocket代理# =============================================location /ws/ {proxy_pass http://127.0.0.1:8080;proxy_http_version 1.1;proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection "Upgrade";proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;# WebSocket超時設置proxy_connect_timeout 60s;proxy_send_timeout 60s;proxy_read_timeout 60s;}
}# =============================================
# HTTP重定向到HTTPS
# =============================================server {listen 80;listen [::]:80;server_name secure.example.com;# 重定向到HTTPSreturn 301 https://$server_name$request_uri;
}

4.2 WebSocket代理

# =============================================
# WebSocket代理配置
# =============================================server {listen 80;server_name ws.example.com;access_log /var/log/nginx/ws.example.com.access.log main;error_log /var/log/nginx/ws.example.com.error.log warn;# =============================================# WebSocket代理配置# =============================================location /ws/ {# WebSocket后端服務器proxy_pass http://127.0.0.1:8080;# WebSocket必要頭信息proxy_http_version 1.1;proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection "Upgrade";proxy_set_header Host $host;# 其他代理頭信息proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;# WebSocket超時設置proxy_connect_timeout 60s;proxy_send_timeout 60s;proxy_read_timeout 60s;# 緩沖區設置(WebSocket通常不緩沖)proxy_buffering off;# 心跳設置proxy_set_header Connection "";}# =============================================# 帶認證的WebSocket# =============================================location /ws-auth/ {# 基本認證auth_basic "WebSocket Authentication";auth_basic_user_file /usr/local/nginx/conf/htpasswd.ws;# WebSocket代理proxy_pass http://127.0.0.1:8080;proxy_http_version 1.1;proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection "Upgrade";proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;# 添加認證信息proxy_set_header X-WS-User $remote_user;# 超時設置proxy_connect_timeout 60s;proxy_send_timeout 60s;proxy_read_timeout 60s;proxy_buffering off;}# =============================================# WebSocket負載均衡# =============================================location /ws-lb/ {# 定義WebSocket后端服務器組proxy_pass http://websocket_backend;proxy_http_version 1.1;proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection "Upgrade";proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;# 負載均衡設置proxy_connect_timeout 60s;proxy_send_timeout 60s;proxy_read_timeout 60s;proxy_buffering off;}
}# =============================================
# WebSocket后端服務器組
# =============================================upstream websocket_backend {# IP哈希確保同一客戶端連接到同一服務器ip_hash;server 192.168.1.10:8080;server 192.168.1.11:8080;server 192.168.1.12:8080;# 健康檢查server 192.168.1.10:8080 max_fails=3 fail_timeout=30s;server 192.168.1.11:8080 max_fails=3 fail_timeout=30s;server 192.168.1.12:8080 max_fails=3 fail_timeout=30s;# 連接保持keepalive 32;keepalive_timeout 30s;keepalive_requests 1000;
}

4.3 代理健康檢查

被動健康檢查
# =============================================
# 被動健康檢查配置
# =============================================upstream backend_health_check {# 后端服務器配置server 192.168.1.10:8080 weight=5 max_fails=3 fail_timeout=30s;server 192.168.1.11:8080 weight=3 max_fails=3 fail_timeout=30s;server 192.168.1.12:8080 weight=2 max_fails=3 fail_timeout=30s backup;# 負載均衡方法least_conn;# 連接保持keepalive 32;keepalive_timeout 30s;keepalive_requests 1000;
}server {listen 80;server_name health.example.com;access_log /var/log/nginx/health.example.com.access.log main;error_log /var/log/nginx/health.example.com.error.log warn;location / {proxy_pass http://backend_health_check;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;proxy_http_version 1.1;proxy_set_header Connection "";proxy_connect_timeout 60s;proxy_send_timeout 60s;proxy_read_timeout 60s;# 添加健康檢查信息add_header X-Upstream-Status $upstream_status;add_header X-Upstream-Response-Time $upstream_response_time;add_header X-Upstream-Addr $upstream_addr;}# =============================================# 健康檢查端點# =============================================location /health {# 限制訪問allow 127.0.0.1;allow 192.168.1.0/24;deny all;# 返回健康狀態add_header Content-Type "application/json";return 200 '{"status": "healthy","upstream": "backend_health_check","servers": [{"addr": "192.168.1.10:8080", "status": "up"},{"addr": "192.168.1.11:8080", "status": "up"},{"addr": "192.168.1.12:8080", "status": "backup"}]}';}
}
主動健康檢查(需要nginx_plus或第三方模塊)
# =============================================
# 主動健康檢查配置(需要nginx_plus)
# =============================================upstream backend_active_health {zone backend_active_health 64k;server 192.168.1.10:8080 slow_start=30s;server 192.168.1.11:8080 slow_start=30s;server 192.168.1.12:8080 slow_start=30s backup;# 主動健康檢查health_check interval=10s fails=3 passes=2 uri=/health port=8080;# 負載均衡least_conn;# 連接保持keepalive 32;keepalive_timeout 30s;keepalive_requests 1000;
}server {listen 80;server_name active-health.example.com;access_log /var/log/nginx/active-health.example.com.access.log main;error_log /var/log/nginx/active-health.example.com.error.log warn;location / {proxy_pass http://backend_active_health;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Forwarded-Proto $scheme;proxy_http_version 1.1;proxy_set_header Connection "";proxy_connect_timeout 60s;proxy_send_timeout 60s;proxy_read_timeout 60s;# 添加健康狀態信息add_header X-Upstream-Status $upstream_status;add_header X-Upstream-Response-Time $upstream_response_time;add_header X-Upstream-Addr $upstream_addr;}# =============================================# 健康狀態監控# =============================================location /upstream_status {# 限制訪問allow 127.0.0.1;allow 192.168.1.0/24;deny all;# 顯示上游服務器狀態upstream_status;add_header Content-Type "text/plain";}
}

五、代理配置常見問題與解決方案

5.1 代理連接超時

問題現象:

2024/01/15 10:30:15 [error] 12345#0: *12345 upstream timed out (110: Connection timed out) while connecting to upstream

解決方案:

# 調整代理超時設置
location / {proxy_pass http://backend;proxy_set_header Host $host;# 增加連接超時時間proxy_connect_timeout 120s;proxy_send_timeout 120s;proxy_read_timeout 120s;# 啟用代理緩沖proxy_buffering on;proxy_buffer_size 8k;proxy_buffers 8 8k;proxy_busy_buffers_size 16k;
}

5.2 代理緩存問題

問題現象:

  • 緩存不生效
  • 緩存內容過期
  • 緩存清理失敗

解決方案:

# 檢查緩存配置
proxy_cache_path /usr/local/nginx/proxy_cache levels=1:2 keys_zone=cache_zone:10m inactive=60m;location / {proxy_pass http://backend;proxy_set_header Host $host;# 確保緩存啟用proxy_cache cache_zone;proxy_cache_valid 200 302 10m;proxy_cache_key $scheme$request_method$host$request_uri;# 添加緩存狀態頭add_header X-Proxy-Cache $upstream_cache_status;# 檢查緩存條件proxy_cache_bypass $cookie_nocache $arg_nocache;proxy_no_cache $cookie_nocache $arg_nocache;
}

5.3 SSL代理問題

問題現象:

2024/01/15 10:30:15 [error] 12345#0: *12345 SSL_do_handshake() failed (SSL: error:14094410:SSL routines:ssl3_read_bytes:sslv3 alert handshake failure)

解決方案:

# 調整SSL配置
location / {proxy_pass https://backend;proxy_set_header Host $host;# SSL配置proxy_ssl_server_name on;proxy_ssl_protocols TLSv1.2 TLSv1.3;proxy_ssl_ciphers HIGH:!aNULL:!MD5;proxy_ssl_session_reuse on;proxy_ssl_trusted_certificate /etc/ssl/certs/ca-certificates.crt;# SSL超時設置proxy_ssl_timeout 60s;
}

5.4 WebSocket代理問題

問題現象:

  • WebSocket連接失敗
  • 連接頻繁斷開

解決方案:

# WebSocket代理配置
location /ws/ {proxy_pass http://backend;proxy_http_version 1.1;proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection "Upgrade";proxy_set_header Host $host;# 禁用緩沖proxy_buffering off;# 調整超時時間proxy_connect_timeout 120s;proxy_send_timeout 120s;proxy_read_timeout 120s;# 心跳設置proxy_set_header Connection "";
}

性能優化建議:

  • 啟用keepalive減少連接開銷
  • 合理配置緩存策略
  • 使用負載均衡分散請求
  • 啟用壓縮減少傳輸數據量
  • 監控代理性能指標

安全配置建議:

  • 限制代理訪問權限
  • 啟用SSL/TLS加密
  • 配置適當的安全頭
  • 定期更新SSL證書
  • 監控異常訪問行為

Nginx代理功能是現代網絡架構中不可或缺的組成部分。通過本文的學習,你應該能夠熟練配置和管理Nginx代理服務器,為構建高性能、高可用的網絡服務打下堅實基礎。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/bicheng/93783.shtml
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/93783.shtml
英文地址,請注明出處:http://en.pswp.cn/bicheng/93783.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Vue3 Element-plus 封裝Select下拉復選框選擇器

廢話不多說&#xff0c;樣式如下&#xff0c;代碼如下&#xff0c;需要自取<template><el-selectv-model"selectValue"class"checkbox-select"multiple:placeholder"placeholder":style"{ width: width }"change"change…

jenkins 自動部署

一、win10 環境安裝&#xff1a; 1、jdk 下載安裝&#xff1a;Index of openjdk-local 2、配置環境變量&#xff1a; 3、jenkins 下載&#xff1a;Download and deploy 下載后的結果&#xff1a;jenkins.war 4、jenkins 啟動&#xff1a; 5、創建管理員用戶 admin 登錄系統…

2020 GPT3 原文 Language Models are Few-Shot Learners 精選注解

本文為個人閱讀GPT3&#xff0c;部分內容注解&#xff0c;由于GPT3原文篇幅較長&#xff0c;且GPT3無有效開源信息 這里就不再一一粘貼&#xff0c;僅對原文部分內容做注解&#xff0c;僅供參考 詳情參考原文鏈接 原文鏈接&#xff1a;https://arxiv.org/pdf/2005.14165 語言模…

設計模式筆記_行為型_迭代器模式

1. 迭代器模式介紹迭代器模式&#xff08;Iterator Pattern&#xff09;是一種行為設計模式&#xff0c;旨在提供一種方法順序訪問一個聚合對象中的各個元素&#xff0c;而又不需要暴露該對象的內部表示。這個模式的主要目的是將集合的遍歷與集合本身分離&#xff0c;使得用戶可…

【Part 4 未來趨勢與技術展望】第一節|技術上的抉擇:三維實時渲染與VR全景視頻的共生

《VR 360全景視頻開發》專欄 將帶你深入探索從全景視頻制作到Unity眼鏡端應用開發的全流程技術。專欄內容涵蓋安卓原生VR播放器開發、Unity VR視頻渲染與手勢交互、360全景視頻制作與優化&#xff0c;以及高分辨率視頻性能優化等實戰技巧。 &#x1f4dd; 希望通過這個專欄&am…

mac查看nginx安裝位置 mac nginx啟動、重啟、關閉

安裝工具&#xff1a;homebrew步驟&#xff1a;1、打開終端&#xff0c;習慣性命令&#xff1a;brew update //結果&#xff1a;Already up-to-date.2、終端繼續執行命令&#xff1a;brew search nginx //查詢要安裝的軟件是否存在3、執行命令&#xff1a;brew info nginx4. …

網絡通信的基本概念與設備

目錄 一、互聯網 二、JAVA跨平臺與C/C的原理 1、JAVA跨平臺的原理 2、C/C跨平臺的原理 三、網絡互連模型 四、客戶端與服務器 五、計算機之間的通信基礎 1、IP地址與MAC地址 2、ARP與ICMP對比 ①ARP協議&#xff08;地址解析協議&#xff09; ②ICMP協議&#xff08…

云原生俱樂部-k8s知識點歸納(1)

這篇文章主要是講講k8s中的知識點歸納&#xff0c;以幫助理解。雖然平時也做筆記和總結&#xff0c;但是就將內容復制過來不太好&#xff0c;而且我比較喜歡打字。因此知識點歸納總結還是以敘述的口吻來說說&#xff0c;并結合我的理解加以論述。k8s和docker首先講一講docker和…

基于Node.js+Express的電商管理平臺的設計與實現/基于vue的網上購物商城的設計與實現/基于Node.js+Express的在線銷售系統

基于Node.jsExpress的電商管理平臺的設計與實現/基于vue的網上購物商城的設計與實現/基于Node.jsExpress的在線銷售系統

Git 對象存儲:理解底層原理,實現高效排錯與存儲優化

### 探秘 Git 對象存儲&#xff1a;底層原理與優化實踐#### 一、Git 對象存儲的底層原理 Git 采用**內容尋址文件系統**&#xff0c;核心機制如下&#xff1a; 1. **對象類型與存儲** - **Blob 對象**&#xff1a;存儲文件內容&#xff0c;通過 git hash-object 生成唯一 SHA-…

【2025CVPR-目標檢測方向】RaCFormer:通過基于查詢的雷達-相機融合實現高質量的 3D 目標檢測

1. 研究背景與動機? ?問題?:現有雷達-相機融合方法依賴BEV特征融合,但相機圖像到BEV的轉換因深度估計不準確導致特征錯位;雷達BEV特征稀疏,相機BEV特征因深度誤差存在畸變。 ?核心思路?:提出跨視角查詢融合框架,通過對象查詢(object queries)同時采樣圖像視角(原…

【每日一題】Day 7

560.和為K的子數組 題目&#xff1a; 給你一個整數數組 nums 和一個整數 k &#xff0c;請你統計并返回該數組中和為 k 的子數組的個數 。 子數組是數組中元素的連續非空序列。 示例 1&#xff1a; 輸入&#xff1a;nums [1,1,1], k 2 輸出&#xff1a;2 示例 2&#x…

3ds MAX文件/貼圖名稱亂碼?6大根源及解決方案

在3ds MAX渲染階段&#xff0c;文件或貼圖名稱亂碼導致渲染失敗&#xff0c;是困擾眾多用戶的常見難題。其背后原因多樣&#xff0c;精準定位方能高效解決&#xff1a;亂碼核心根源剖析字符編碼沖突 (最常見)非ASCII字符風險&#xff1a; 文件路徑或名稱包含中文、日文、韓文等…

鏈路聚合路由器OpenMPTCProuter源碼編譯與運行

0.前言 前面寫了兩篇關于MPTCP的文章&#xff1a; 《鏈路聚合技術——多路徑傳輸Multipath TCP(MPTCP)快速實踐》《使用MPTCPBBR進行數據傳輸&#xff0c;讓網絡又快又穩》 對MPTCP有了基本的了解與實踐&#xff0c;并在虛擬的網絡拓撲中實現了鏈路帶寬的疊加。 1.OpenMPTC…

AI時代企業轉型指南:用AI降本增效,銷售轉化翻3倍,獲客成本砍一半!

AI時代&#xff0c;大部分企業每天都在問同一個問題&#xff1a;AI到底能幫我做什么&#xff1f;無論你是做電商、做IP、做操盤手&#xff0c;還是傳統企業老板&#xff0c;你都會發現一個現實——AI真正的用途是用來在業務場景里直接降本增效的。對我個人來說&#xff0c;AI已…

【牛客刷題】最大公約數與最小公倍數:算法詳解與實現

文章目錄 一、題目介紹 1.1 輸入描述 1.2 輸出描述 1.3 示例(含詳細注釋) 二、考察的知識點 三、算法設計思路 3.1 最大公約數(GCD) 3.2 最小公倍數(LCM) 四、流程圖 五、題解實現 六、復雜度分析 七、關鍵算法知識點 一、題目介紹 計算兩個整數的**最大公約數(GCD)和最小公…

將 iPhone 聯系人轉移到 Infinix 的完整指南

從 iPhone 切換到 Infinix 設備是一次令人興奮的升級&#xff0c;但在切換過程中&#xff0c;轉移個人數據&#xff08;尤其是聯系人&#xff09;可能會有些棘手。聯系人是任何手機上最重要的信息類型之一&#xff0c;如果在切換過程中丟失它們&#xff0c;會帶來很大的不便。由…

Clipboard.js 復制內容

插件地址 clipboard.js 中文網 安裝 npm install clipboard --save使用示例 <template><div><div class"copyBtn" click"copyText">復制文本</div ></div> </template><script> // 引入clipboard.js import…

蛇形方陣構造

給出方陣的長寬&#xff0c;n 和 m &#xff0c;按照斜著的蛇形輸出該方陣 面試官給的送分題裸模擬&#xff0c;寫的太慢了沒過&#xff0c;實際確實慢&#xff0c;結束后起碼用了一個多小時才調完 找了下沒找到leetcode 提交的地方&#xff0c;各種oj 倒是有&#xff0c;不過是…

傳統方式部署(RuoYi-Cloud)微服務

實驗環境192.168.10.43和192.168.10.44內存不能小于4G一、安裝MySQL&#xff08;192.168.10.46&#xff09;1、安裝MySQL依賴庫dnf -y install ncurses-compat-libs2、上傳mysql-8.0.42-linux-glibc2.17-x86_64-minimal.tar.xz二進制包到/root目錄&#xff0c;解壓并移動到指定…