V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
wang794822947
V2EX  ›  问与答

各位大神们,请教一个 nginx 反代问题

  •  
  •   wang794822947 · 2023-01-14 14:52:34 +08:00 · 924 次点击
    这是一个创建于 472 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我的 http8008 端口起了一个哪吒面板的服务,目前用 http://域名.com:8008 访问服务正常 我现在想用 https 反代 我希望的结果是当我访问 https://域名.com/nezha/的时候,可以正常的访问哪吒服务 我的 nginx 配置如下

    location ^~ /nezha/
            {
             proxy_redirect off;
             proxy_read_timeout 1200s;
    	 proxy_pass http://127.0.0.1:8008/;
             proxy_set_header X-Real-IP $remote_addr;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
             proxy_set_header Host $host;
             proxy_set_header Upgrade-Insecure-Requests 1;
             proxy_set_header X-Forwarded-Proto https;
             
            }
            location ~* /(static)/.*\.(gif|jpg|jpeg|bmp|png|js|css)$
                    {
                    proxy_pass http://127.0.0.1:8008;#访问路径及端口
                    }
    
            location /ws {        
            proxy_redirect off;
            proxy_pass http://127.0.0.1:8008;      # 转发
            proxy_set_header Host $host;
            proxy_set_header X-Real_IP $remote_addr;
            proxy_set_header X-Forwarded-For $remote_addr:$remote_port;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;   # 升级协议头 websocket
            proxy_set_header Connection upgrade;
        }
    

    我遇到的问题是,目前首页可以正常使用,但当我点击哪吒面板的其他功能时,页面的链接是 https://域名.com/service

    此时 nginx 就代理失败了,页面无法正常访问,请问怎么做才能让我在点击哪吒面板其他功能时,页面的地址是 https://域名.com/nezha/service

    5 条回复    2023-01-15 16:18:25 +08:00
    dethan
        1
    dethan  
       2023-01-14 15:02:06 +08:00   ❤️ 1
    没用过哪吒
    但是之前遇到过这个问题?类似的问题?
    就是服务前端写了绝对路径,导致无法反代,经过查阅资料,我发现了 2 种解决办法:
    1. 通过二级域名的方式反代而不是添加自定义前缀: https://域名.com/nezha/ 改成 https://nezha.域名.com
    2. 改写前端文件内容:
    ```
    sub_filter 'src="/assets/' 'src="/nezha/assets/';
    sub_filter 'href="/assets/' 'href="/nezha/assets/';
    sub_filter 'url(/assets/' 'url(/nezha/assets/';
    sub_filter '/api' '/nezha/api';
    sub_filter_types *;
    sub_filter_once off;
    ```
    wang794822947
        2
    wang794822947  
    OP
       2023-01-14 15:32:58 +08:00
    @dethan 非常感谢,因为是部署的 docker ,我无法改变他们的前端代码,但是我用第一种方式用了二级域名成功代理了。
    dethan
        3
    dethan  
       2023-01-14 15:43:44 +08:00 via Android
    @wang794822947 第二种方式也是改 nginx 的配置文件,具体用法可以百度下 sub_filter
    hdp5252
        4
    hdp5252  
       2023-01-14 17:14:53 +08:00 via Android
    oneisall8955
        5
    oneisall8955  
       2023-01-15 16:18:25 +08:00
    这个?

    https://nezha.wiki/guide/dashboard.html#%E9%85%8D%E7%BD%AE%E5%8F%8D%E5%90%91%E4%BB%A3%E7%90%86

    <a href="https://imgse.com/i/pSQWO0K"><img src="https://s1.ax1x.com/2023/01/15/pSQWO0K.png" alt="pSQWO0K.png" border="0" /></a>
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   820 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 21:12 · PVG 05:12 · LAX 14:12 · JFK 17:12
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.