V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
liuyinltemp
V2EX  ›  NGINX

nginx 反代 https 源站,为啥 upstream 源站变成 https://ip/XXX

  •  
  •   liuyinltemp · 2022-09-01 23:02:03 +08:00 · 2376 次点击
    这是一个创建于 574 天前的主题,其中的信息可能已经有所发展或是发生改变。

    菜鸟求助,nginx 反代 https://AAA.com 页面打开为空,如果直接反代 http://AAA.com 则完全正常; 看错误代码

    [error] 5790#5790: *15933 upstream prematurely closed connection while reading upstream, client: 127.0.0.1, server: m.shss.pw, request: "GET /bbs/index.php HTTP/2.0", upstream: "https://数字 IP:443/bbs/index.php", host: "BBB.com", referrer: "https:/BBB.com/"

    https://AAA.com 是 ke 可以打开的,但是 https://数字 IP:443/ 显然是无法打开的,所以报错

    请问这种情形下 nginx 如何修改配置?

    24 条回复    2022-09-07 16:50:25 +08:00
    mxT52CRuqR6o5
        1
    mxT52CRuqR6o5  
       2022-09-01 23:04:10 +08:00 via Android
    host 对的就没问题,host 是 http 协议的 header ,upstream 不是,问题原因应该不是你目前认为的那样
    turan12
        2
    turan12  
       2022-09-01 23:05:00 +08:00 via iPhone
    把 conf 文件贴上来看看
    liuyinltemp
        3
    liuyinltemp  
    OP
       2022-09-01 23:06:35 +08:00
    反代源站 http 是没问题的,ip 也是源站的 ip ,https://数字 IP:443 这个是没法访问
    liuyinltemp
        4
    liuyinltemp  
    OP
       2022-09-01 23:09:28 +08:00
    server {
    listen 443 ssl http2;
    server_name BBB.com;
    ssl_certificate cert/XXX.crt;
    ssl_certificate_key cert/XXX.key;
    resolver 8.8.8.8 1.1.1.1 valid=3600s;
    resolver_timeout 3s;
    set $backend https://AAA.com;
    add_header Strict-Transport-Security "max-age=31536000;includeSubDomains" always;
    add_header X-Frame-Options SAMEORIGIN;
    add_header Referrer-Policy same-origin;
    add_header X-Content-Type-Options nosniff;
    proxy_set_header Early-Data $ssl_early_data;
    location / {
    proxy_redirect off;
    proxy_pass $backend;
    proxy_ssl_server_name on;
    proxy_http_version 1.1;
    proxy_set_header Connection "";
    proxy_set_header Host AAA.com;
    proxy_set_header Accept-Encoding "";
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Referer https://AAA.com;
    }
    }
    nulIptr
        5
    nulIptr  
       2022-09-01 23:11:05 +08:00
    代理 https 需要配证书啊。。。不然不就是中间人了吗
    liuyinltemp
        6
    liuyinltemp  
    OP
       2022-09-01 23:12:56 +08:00
    @nulIptr 证书没有问题,
    ssl_certificate cert/XXX.crt;
    ssl_certificate_key cert/XXX.key;
    liuyinltemp
        7
    liuyinltemp  
    OP
       2022-09-01 23:13:27 +08:00
    为啥 upstream 哪儿用 ip 替换了域名
    hefish
        8
    hefish  
       2022-09-01 23:22:40 +08:00
    日志里面是 HTTP/2.0 , 配置里面是 1.1 ,不知道有没有关系。
    nulIptr
        9
    nulIptr  
       2022-09-01 23:24:03 +08:00
    @liuyinltemp #6
    我这样解释一下,
    证书是颁发给域名的,你的 a 域名有 a 证书,而 a 证书用不到 b 域名上。如果 a 服务只有 a 域名的 https 的话是不能被 b 域名代理的,如果是同一个域名不同服务器的话理论上可以代理,具体怎么配就不知道了
    你这问题把我给问懵逼了,实际我一般只在最外面的 ingress 网关配 https ,内部的微服务都是 http 。
    PMR
        10
    PMR  
       2022-09-01 23:34:55 +08:00
    在什么地方看过 proxy_pass 是固死 IP 的 开启 SNI
    程序启动时 会自动解析 IP 并固定 域名的解析变动 要 reload nginx 才能 proxy_pass 到变动 IP

    找下 nginx plus 的 example 照葫芦画瓢就行
    ab
        11
    ab  
       2022-09-02 00:26:02 +08:00 via iPhone
    用 stream 转发会更方便
    ZE3kr
        12
    ZE3kr  
       2022-09-02 00:26:32 +08:00 via iPhone
    你得传 SNI
    aaa.com;
    proxy_ssl_server_name on;
    proxy_ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
    ZE3kr
        13
    ZE3kr  
       2022-09-02 00:27:40 +08:00 via iPhone
    proxy_ssl_name

    V2EX 回复不能编辑,没办法,需要你自己拼凑下
    liuyinltemp
        14
    liuyinltemp  
    OP
       2022-09-02 09:20:13 +08:00
    up 一下,这个配置反代其他 https 域名没有问题
    liuyinltemp
        15
    liuyinltemp  
    OP
       2022-09-02 09:45:35 +08:00
    对了,还有一个特征,就是访问 https://AAA.com 是正常的,但是跳转 https://AAA.com/bbs/index.php 出现问题,只显示网页底色
    konsh
        16
    konsh  
       2022-09-02 10:42:56 +08:00
    @liuyinltemp https 协议是需要 ca 证书,证书是颁发给域名的,与 IP 无关,所以访问 https://XXX.com 是没有问题的,而 https://ip:443 则是无法访问的。看了下你的 conf 文件,proxy_pass 代理的是 https 请求,加上证书配置试试看,参考文档 https://docs.nginx.com/nginx/admin-guide/security-controls/securing-http-traffic-upstream/
    jiabing520a
        17
    jiabing520a  
       2022-09-02 13:28:37 +08:00
    我是在 /etc/hosts 里指定源站 IP ,使用起来没有问题
    liuyinltemp
        18
    liuyinltemp  
    OP
       2022-09-02 15:56:48 +08:00
    chrome F12 查看显示 Failed to load resource: net::ERR_HTTP2_PROTOCOL_ERROR
    liuyinltemp
        19
    liuyinltemp  
    OP
       2022-09-02 17:13:01 +08:00
    @konsh 没有源站的证书
    liuyinltemp
        20
    liuyinltemp  
    OP
       2022-09-02 17:27:20 +08:00
    单独看反代后 js 是可以正常打开,现在就是进入 https://AAA.com/bbs/index.php 没有响应
    liuyinltemp
        21
    liuyinltemp  
    OP
       2022-09-04 23:27:42 +08:00
    curl: (60) SSL certificate problem: unable to get local issuer certificate
    More details here: https://curl.se/docs/sslcerts.html

    curl failed to verify the legitimacy of the server and therefore could not
    establish a secure connection to it. To learn more about this situation and
    how to fix it, please visit the web page mentioned above.
    liuyinltemp
        22
    liuyinltemp  
    OP
       2022-09-04 23:28:17 +08:00
    又查了一下,应该是 vps 下访问源站有问题,但是证书又是对的。
    liuyinltemp
        23
    liuyinltemp  
    OP
       2022-09-05 09:44:59 +08:00
    应该是源站的证书缺失了中间证书,这种情况有啥办法
    liuyinltemp
        24
    liuyinltemp  
    OP
       2022-09-07 16:50:25 +08:00
    源站补齐中间证书就好了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3638 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 00:12 · PVG 08:12 · LAX 17:12 · JFK 20:12
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.