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

2022-09-01 23:02:03 +08:00
 liuyinltemp

菜鸟求助,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 如何修改配置?

2443 次点击
所在节点    NGINX
24 条回复
mxT52CRuqR6o5
2022-09-01 23:04:10 +08:00
host 对的就没问题,host 是 http 协议的 header ,upstream 不是,问题原因应该不是你目前认为的那样
turan12
2022-09-01 23:05:00 +08:00
把 conf 文件贴上来看看
liuyinltemp
2022-09-01 23:06:35 +08:00
反代源站 http 是没问题的,ip 也是源站的 ip ,https://数字 IP:443 这个是没法访问
liuyinltemp
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
2022-09-01 23:11:05 +08:00
代理 https 需要配证书啊。。。不然不就是中间人了吗
liuyinltemp
2022-09-01 23:12:56 +08:00
@nulIptr 证书没有问题,
ssl_certificate cert/XXX.crt;
ssl_certificate_key cert/XXX.key;
liuyinltemp
2022-09-01 23:13:27 +08:00
为啥 upstream 哪儿用 ip 替换了域名
hefish
2022-09-01 23:22:40 +08:00
日志里面是 HTTP/2.0 , 配置里面是 1.1 ,不知道有没有关系。
nulIptr
2022-09-01 23:24:03 +08:00
@liuyinltemp #6
我这样解释一下,
证书是颁发给域名的,你的 a 域名有 a 证书,而 a 证书用不到 b 域名上。如果 a 服务只有 a 域名的 https 的话是不能被 b 域名代理的,如果是同一个域名不同服务器的话理论上可以代理,具体怎么配就不知道了
你这问题把我给问懵逼了,实际我一般只在最外面的 ingress 网关配 https ,内部的微服务都是 http 。
PMR
2022-09-01 23:34:55 +08:00
在什么地方看过 proxy_pass 是固死 IP 的 开启 SNI
程序启动时 会自动解析 IP 并固定 域名的解析变动 要 reload nginx 才能 proxy_pass 到变动 IP

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

V2EX 回复不能编辑,没办法,需要你自己拼凑下
liuyinltemp
2022-09-02 09:20:13 +08:00
up 一下,这个配置反代其他 https 域名没有问题
liuyinltemp
2022-09-02 09:45:35 +08:00
对了,还有一个特征,就是访问 https://AAA.com 是正常的,但是跳转 https://AAA.com/bbs/index.php 出现问题,只显示网页底色
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
2022-09-02 13:28:37 +08:00
我是在 /etc/hosts 里指定源站 IP ,使用起来没有问题
liuyinltemp
2022-09-02 15:56:48 +08:00
chrome F12 查看显示 Failed to load resource: net::ERR_HTTP2_PROTOCOL_ERROR
liuyinltemp
2022-09-02 17:13:01 +08:00
@konsh 没有源站的证书
liuyinltemp
2022-09-02 17:27:20 +08:00
单独看反代后 js 是可以正常打开,现在就是进入 https://AAA.com/bbs/index.php 没有响应

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/877124

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX