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

2023-01-14 14:52:34 +08:00
 wang794822947

我的 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

950 次点击
所在节点    问与答
5 条回复
dethan
2023-01-14 15:02:06 +08:00
没用过哪吒
但是之前遇到过这个问题?类似的问题?
就是服务前端写了绝对路径,导致无法反代,经过查阅资料,我发现了 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
2023-01-14 15:32:58 +08:00
@dethan 非常感谢,因为是部署的 docker ,我无法改变他们的前端代码,但是我用第一种方式用了二级域名成功代理了。
dethan
2023-01-14 15:43:44 +08:00
@wang794822947 第二种方式也是改 nginx 的配置文件,具体用法可以百度下 sub_filter
hdp5252
2023-01-14 17:14:53 +08:00
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>

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

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

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

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

© 2021 V2EX