nginx 代理的后端为https

  • 1、需求:接入层有一个nginx,代理后端所有的服务都走的http,突然有一天接入了一个平台走的https(https://192.168.1.1)也需要通过接入层的nginx进行代理

  • 2、配置

########### 测试nginx 代理https start ##########
    server {
        listen       10080 default_server;
        server_name  _;
    location /aaa {
        proxy_pass https://httpbin.org/get;  # 代理到目标 HTTPS 服务
        proxy_set_header Host $host;  # 保留原始请求的 Host 头
        proxy_set_header X-Forwarded-Proto $scheme;  # 传递原始请求的协议(http/https)
        access_log /tmp/baidu_access.log main;  # 自定义日志文件
    }
}
########### 测试nginx 代理https end ##########

nginx -t
nginx -s reload
  • 3、验证
curl http://localhost:10080/aaa
{
  "args": {},
  "headers": {
    "Accept": "*/*",
    "Host": "localhost",
    "User-Agent": "curl/7.29.0",
    "X-Amzn-Trace-Id": "Root=1-6736b041-1d8584a713cbfd1e7089cd7c"
  },
  "origin": "111.202.167.97",
  "url": "https://localhost/get"
}
- 排查思路:检查nginx日志

- 解决思路:最小化配置。注释掉 #proxy_set_header Host $host;  # 保留原始请求的 Host 头后403的问题解决,应该是https服务对客户端的访问ip有白名单限制(待验证)

    location /aaa {
        proxy_pass https://httpbin.org/get;  # 代理到目标 HTTPS 服务
        proxy_set_header Host $host;  # 保留原始请求的 Host 头
        proxy_set_header X-Forwarded-Proto $scheme;  # 传递原始请求的协议(http/https)
        access_log /tmp/baidu_access.log main;  # 自定义日志文件
    }

posted @ 2024-11-15 10:31  Hello_worlds  阅读(526)  评论(0)    收藏  举报