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;   #证书为服务器自带
相关推荐
zhping101123 分钟前
Linux 系统上使用 GitHub 加速工具
linux·运维·github
于眠牧北35 分钟前
ubuntu22.04安装docker以及安装过程中报错解决方法
运维·docker·容器
煜3641 小时前
Linux初识与基本指令
linux·运维·服务器
执笔论英雄1 小时前
【大模型推理】cudastream 学习
linux·运维·学习
佑白雪乐2 小时前
<Linux基础第14集>总结前面知识点,不含Linux命令
linux·运维·服务器
Lau_way2 小时前
windows通过xshell局域网连接linux
linux·运维·服务器
ken22322 小时前
(a-) 在不同软件包里:相同名称和用途的软件工具,功能不完全一样 + 查询网络路径中的最小 MTU 值工具 (***)
linux·运维·服务器
shamalee2 小时前
Nginx反向代理出现502 Bad Gateway问题的解决方案
运维·nginx·gateway
爱莉希雅&&&2 小时前
haproxy安装以及haproxy+nginx简单案例详解
linux·运维·nginx·haproxy
黄昏晓x2 小时前
Linux----进程通信
linux·运维·服务器