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

请教关于 NGINX 反代的问题

  •  
  •   luxu 2022-04-08 11:36:53 +08:00 2895 次点击
    这是一个创建于 1289 天前的主题,其中的信息可能已经有所发展或是发生改变。

    想通过 http://aaa/ceshi 访问到 http://bbb 的内容, 但是代理后一些 js ,css 文件报 404,发现是缺少前缀的问题。请问 a 机器上怎么配置? 谢谢大家

    a 机器配置

     location ^~/ceshi/ { 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-NginX-Proxy true; proxy_pass http://192.168.0.50; } 

    b 机器配置

     location ^~/ceshi/ { 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-NginX-Proxy true; proxy_pass http://localhost/; } 

    b 机器日志

    [08/Apr/2022:11:22:46 +0800] "GET / HTTP/1.0" 127.0.0.1 - -200 26867 "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36" "192.168.21.233, 192.168.0.53" 

    浏览器 Console

    :81/js/chunk-f0487752.5a2a9024.js:1 Failed to load resource: the server responded with a status of 404 (Not Found) :81/js/chunk-f5dfb222.b4892263.js:1 Failed to load resource: the server responded with a status of 404 (Not Found) :81/js/chunk-f46a5f00.d5bf17fb.js:1 Failed to load resource: the server responded with a status of 404 (Not Found) :81/js/chunk-fc9277e6.a8e8a87f.js:1 Failed to load resource: the server responded with a status of 404 (Not Found) :81/js/chunk-ffb40534.0d082972.js:1 Failed to load resource: the server responded with a status of 404 (Not Found) iview.css:1 Failed to load resource: the server responded with a status of 404 (Not Found) gitalk.css:1 Failed to load resource: the server responded with a status of 404 (Not Found) quill.snow.css:1 
    7 条回复    2022-04-10 21:47:02 +08:00
    julyclyde
        1
    julyclyde  
       2022-04-08 12:31:08 +08:00
    skiy
        2
    skiy  
       2022-04-08 12:41:11 +08:00
    原因是因为你网站请求的静态资源(前缀)不包含 /ceshi/ ,所以它没进代理里面。

    你还需要另外再设置一个 (.css | .js ) 的伪静态。
    adoal
        3
    adoal  
       2022-04-08 12:54:23 +08:00
    因为后端的应用并不具备“意识到自己有可能运行在前缀路径下”的能力,所以动态页面里生成的静态资源链接还是按照自己运行在根路径下的。
    要解决这个问题,要么改造后端程序加入这种探测能力。要么(不一定能 100%完美准确判定)在 nginx 里加入改写规则,对后端返回的页面里静态资源的绝对路径做改写加上前缀。
    adoal
        4
    adoal  
       2022-04-08 13:04:49 +08:00
    比如说用 Python Flask 写的可以参照这个例子 https://gist.github.com/jrialland/bf017843bbead538c20c382a19d880fe
    其它语言和框架应该有类似 trick
    rekulas
        5
    rekulas  
       2022-04-08 13:10:03 +08:00
    <base>标签强制制定下根路径试试
    如果还不行只能改造输出代码了,没有 100%完美解决方案
    luxu
        6
    luxu  
    OP
       2022-04-08 14:13:24 +08:00
    @julyclyde 感谢回复,我试一下

    @skiy 感谢,不可以用这种方法 :(

    @adoal 对,就想发帖问下如果改写可以的话如何改写

    @rekulas 谢谢,我试一下
    wolfmei
        7
    wolfmei  
       2022-04-10 21:47:02 +08:00
    试下 location /ceshi {proxy_pass http://192.168.0.50;}
    如果不行,可以加个 proxy_redirect 试下
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2632 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 30ms UTC 12:56 PVG 20:56 LAX 05:56 JFK 08:56
    Do have faith in what you're doing.
    ubao msn 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