求教关于反代的问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Aicnal
V2EX    NGINX

求教关于反代的问题

  •  
  •   Aicnal 2024-08-24 15:14:58 +08:00 2963 次点击
    这是一个创建于 412 天前的主题,其中的信息可能已经有所发展或是发生改变。

    之前部署了一个 Discourse 站点 部署在一台配置比较高但是线路没有优化的机器上 容器内部使用 nginx 然后通过 Unix Socket 使用宿主机的 Caddy 进行域名访问 之后我在线路比较好的香港服务器上部署 Nginx 对原站进行反代 但是反代后为502,无法正常访问 我看了下error.log说的是代理服务器和源站 ssl 握手失败 但是我在代理服务器里面写的是https,而且代理服务器curl能正常获取到内容 这个是源站的caddyfile

    direct.example.com { reverse_proxy unix//var/discourse/shared/standalone/nginx.http.sock { header_up Host {host} header_up X-Real-IP {remote} header_up X-Forwarded-For {remote} header_up X-Forwarded-Proto {scheme} } header { Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" } } synapse.know-cnu.wiki { reverse_proxy localhost:8008 { # WebSocket 连接的默认配置 header_up Host {host} header_up X-Real-IP {remote} header_up X-Forwarded-For {remote} header_up X-Forwarded-Proto {scheme} # 处理 WebSocket 连接 transport http { read_buffer 0 } } # 设置 HSTS 头部 header { Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" } 

    这个是代理服务器的nginx.conf,未使用ssl

    server { listen 80; server_name example.com; location / { proxy_pass https://direct.example.com; proxy_set_header Host $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; } } 

    error.log如下

    root@VM80459:~# tail -f /var/log/nginx/error.log 2024/08/24 06:08:57 [emerg] 3325#3325: "server" directive is not allowed here in /etc/nginx/nginx.conf:11 2024/08/24 06:09:05 [emerg] 3326#3326: "server" directive is not allowed here in /etc/nginx/nginx.conf:11 2024/08/24 06:10:18 [notice] 3373#3373: signal process started 2024/08/24 06:19:53 [notice] 3445#3445: signal process started 2024/08/24 06:20:02 [error] 3446#3446: *12 SSL_do_handshake() failed (SSL: error:0A000438:SSL routines::tlsv1 alert internal error:SSL alert number 80) while SSL handshaking to upstream, client: 1xx.1x.5x.xx, server: know-cnu.wiki, request: "GET / HTTP/1.1", upstream: "https://[2400:61xx:0:dx::xa:a0x]:443/", host: "example.com" 2024/08/24 06:20:02 [error] 3446#3446: *12 SSL_do_handshake() failed (SSL: error:0A000438:SSL routines::tlsv1 alert internal error:SSL alert number 80) while SSL handshaking to upstream, client: 10x.13x.5x.18x, server: example.com, request: "GET / HTTP/1.1", upstream: "https://15x.8x.2xx.xx:443/", host: "example.com" 
    第 1 条附言    2024-08-24 17:52:42 +08:00

    目前问题和这样一个 我修改了一下nginx文件还是不行

     location / { proxy_pass https://direct.example.com; # 目标 HTTPS 站点 proxy_set_header Host $host; # 使用原始请求的主机头 proxy_set_header X-Real-IP $remote_addr; # 客户端真实 IP proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 代理链中的客户端 IP proxy_set_header X-Forwarded-Proto $scheme; # 请求协议 (http 或 https) proxy_ssl_name proxy.know-cnu.wiki; # SNI 支持 proxy_ssl_server_name on; # 启用 SNI proxy_ssl_protocols TLSv1 TLSv1.2 TLSv1.3; # 支持的 SSL/TLS 协议 proxy_ssl_verify off; proxy_redirect off; # 缓存控制 add_header X-Cache $upstream_cache_status; # 添加缓存状态头 add_header Cache-Control no-cache; # 禁用缓存 # 如果你有需要缓存的场景,可以用适当的配置替代 Cache-Control } 
    12 条回复    2024-08-27 11:12:49 +08:00
    sead
        1
    sead  
       2024-08-24 17:45:16 +08:00
    改为目标固定域名试试
    header_up Host targetDomain
    sead
        2
    sead  
       2024-08-24 17:47:58 +08:00
    看错了,改这个试试
    proxy_set_header Host targetDomain
    Aicnal
        3
    Aicnal  
    OP
       2024-08-24 17:50:34 +08:00
    @sead 还是不行 和 nginx 转发 cloudflare 托管的域名是一样的问题 我网上查了蛮多 还是没法解决
    sead
        4
    sead  
       2024-08-24 17:52:26 +08:00
    https://github.com/seadfeng/cloudflare-proxy-sites

    CF 直接用这个,单域名模式
    Aicnal
        5
    Aicnal  
    OP
       2024-08-24 17:53:26 +08:00
    @sead 我就是想用一个线路比较好的服务器优化访问速度 CF 太慢了
    yinmin
        6
    yinmin  
       2024-08-24 17:59:18 +08:00
    在 proxy_pass https://direct.example.com; 下面加 3 行代码:

    proxy_ssl_name direct.example.com;
    proxy_ssl_server_name on;
    proxy_set_header Host direct.example.com;

    然后去除掉:proxy_set_header Host $host;
    happyn
        7
    happyn  
       2024-08-24 18:13:32 +08:00
    代理服务器先不要设定请求头了,用最简单的方式试试:

    location / {
    proxy_pass https://direct.example.com;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
    }
    Solix
        8
    Solix  
       2024-08-24 20:25:24 +08:00
    你要实在解决不了可以用自建 cdn 系统,比反代控制方便。https://github.com/TeaOSLab/EdgeAdmin
    frankcu
        9
    frankcu  
       2024-08-25 18:11:34 +08:00
    Discourse 不是自带有 launcher ?在 yaml 模板可设置 Nginx + ssl 。不用自己配置反代
    Aicnal
        10
    Aicnal  
    OP
       2024-08-25 22:13:24 +08:00
    @frankcu 一台机器要配置多个站点 不止这一个 Discourse
    daisyfloor
        11
    daisyfloor  
       2024-08-27 11:09:58 +08:00
    你把香港服务器上的 Nginx 换成 Caddy 试一试
    Aicnal
        12
    Aicnal  
    OP
       2024-08-27 11:12:49 +08:00
    @daisyfloor Caddy 就更不行了 出现 308 重定向回环
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     6051 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 26ms UTC 02:37 PVG 10:37 LAX 19:37 JFK 22:37
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86