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;   #证书为服务器自带
相关推荐
IT研究所2 小时前
AI-ITR平台如何减少客户问题反复升级?
大数据·运维·人工智能·低代码·自然语言处理·安全架构·企微
AIgorithmGEEK2 小时前
[Linux]从手写报头到内核套路:序列化、反序列化与自定义协议全链路
linux·运维·服务器·网络·序列化·反序列化
Nil2082 小时前
leetcode 139单词拆分
linux·运维·服务器
Starry-sky(jing)2 小时前
BUG: unable to handle kernel paging request 完整排查:dmesg 四要素与三路定罪
linux·运维·服务器·内核·排障
mounter6252 小时前
从硬件互连到操作系统变革:CXL 技术演进与 Linux 内核工程挑战
linux·运维·服务器
zhengqweasd2 小时前
机房托管服务器怎么选硬盘,机械盘和 SSD 托管场景差异
运维·服务器·github
zxanz13 小时前
网站为什么需要 SSL 证书来实现 https?
https
心易行者4 小时前
Agent应用+API端点商业化进阶实战:从单体智能体到可付费调用的API全流程
运维·服务器·人工智能·python·apache
科技研学社4 小时前
精度决胜品质:羽绒服缝制工艺标准与自动化精度对标解析
运维·自动化