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

请教关于 NGINX 反代的问题

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

    想通过 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   ·   我们的愿景   ·   实用小工具   ·   1204 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 18:12 · PVG 02:12 · LAX 11:12 · JFK 14:12
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.