Nginx端到端反向代理https配置

bash 复制代码
客户端(用户) --HTTPS--> Nginx --HTTPS--> 上游服务器

完整配置

bash 复制代码
# 上游服务器定义
upstream https_backend {
    server backend1.example.com:443 weight=3;
    server backend2.example.com:443 weight=2;
    keepalive 32;
}

# HTTPS 服务器块
server {
    listen 443 ssl http2;
    server_name proxy.example.com;
    
    # 前端 SSL 配置
    ssl_certificate /etc/ssl/certs/proxy.example.com.crt;
    ssl_certificate_key /etc/ssl/private/proxy.example.com.key;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384;
    
    # 安全头
    add_header Strict-Transport-Security "max-age=31536000" always;
    
    location / {
        # 基本代理设置
        proxy_pass https://https_backend;
        
        # SSL 端到端配置
        proxy_ssl_verify on; 
        proxy_ssl_trusted_certificate /etc/ssl/certs/ca-bundle.crt;
        proxy_ssl_verify_depth 2;
        proxy_ssl_server_name on; 
        proxy_ssl_name 后端服务域名;
        proxy_ssl_session_reuse on;
        
        # 请求头设置
        proxy_set_header Host $proxy_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 5s;
        proxy_send_timeout 60s;
        proxy_read_timeout 60s;
        
        # 缓冲设置
        proxy_buffering on;
        proxy_buffer_size 4k;
        proxy_buffers 8 4k;
        
        # HTTP 1.1 保持连接SSE流式传输
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        chunked_transfer_encoding off;
        
        # 错误处理
        proxy_next_upstream error timeout invalid_header;
        proxy_next_upstream_tries 2;
    }
    
    # 健康检查
    location /health {
        access_log off;
        proxy_pass https://https_backend/health;
        proxy_ssl_verify off;
    }
}
bash 复制代码
proxy_ssl_trusted_certificate /etc/ssl/certs/ca-bundle.crt;   #证书为服务器自带
相关推荐
RisunJan1 天前
Linux命令-netstat(查看Linux中网络系统状态信息)
linux·运维·服务器
SPC的存折1 天前
1、MySQL数据库基础
linux·运维·数据库·mysql
无忧.芙桃1 天前
进程之环境变量
linux·运维·服务器
chxii1 天前
linux 下用 acme.sh 搞定 Nginx 免费 SSL 证书自动续期(上)
nginx
chxii1 天前
linux 下用 acme.sh 搞定 Nginx 免费 SSL 证书自动续期(下) 对于acme.sh命令安装详解
linux·运维·服务器
大阿明1 天前
使用vite打包并部署vue项目到nginx
前端·vue.js·nginx
Bert.Cai1 天前
Linux more命令详解
linux·运维
minji...1 天前
Linux 多线程(四)线程等待,线程分离,线程管理,C++多线程,pthread库
linux·运维·开发语言·网络·c++·算法
倔强的胖蚂蚁1 天前
云原生服务器存储规划与磁盘选型实施
运维·服务器·云原生
ZGUIZ1 天前
Ubuntu 25.10 无法外接显示器解决方案
linux·运维·ubuntu