Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

新增的 login 页面不支持二级目录的反代 #1137

Closed
yfdoor opened this issue Jun 1, 2024 · 5 comments
Closed

新增的 login 页面不支持二级目录的反代 #1137

yfdoor opened this issue Jun 1, 2024 · 5 comments
Labels
bug Something isn't working

Comments

@yfdoor
Copy link

yfdoor commented Jun 1, 2024

问题描述

之前是可以通过 http://1235.com/ddns 这样来反代二级目录的,现在新增的 login 页面不会定位到 http://1235.com/ddns/login 下,而是 http://1235.com/login。导致页面显示出错。

DNS服务商

No response

是否已搜索同类问题

已搜索,但未能解决

操作系统

Linux

架构

x86_64

版本

v6.6

安装方式

Docker

额外信息

No response

@yfdoor yfdoor added the bug Something isn't working label Jun 1, 2024
@WaterLemons2k
Copy link
Contributor

#1129

@yfdoor
Copy link
Author

yfdoor commented Jun 2, 2024

#1129

按照上面的案例添加 rewrite

location ^~ /ddns/ {
    proxy_pass http://127.0.0.1:9876/; 
    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 REMOTE-HOST $remote_addr; 
    proxy_set_header Upgrade $http_upgrade; 
    proxy_set_header Connection "upgrade"; 
    proxy_set_header X-Forwarded-Proto $scheme; 
    proxy_http_version 1.1; 
    add_header X-Cache $upstream_cache_status; 
    add_header Strict-Transport-Security "max-age=31536000"; 
    add_header Cache-Control no-cache; 
    rewrite /ddns/(.*) /$1 break;
}

还是跳转到 http://1235.com/login 页。。。

@charley008
Copy link

+1, 6.6.3版,现在只能输入http://1235.com/ddns/login 才能正常使用。要不然还是出现上面的错误

@charley008
Copy link

通用chatgpt 找到一个凑合的办法:

location /ddns/ {
    proxy_pass http://127.0.0.1:9876/;
    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;
    proxy_set_header REMOTE-HOST $remote_addr;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_http_version 1.1;
    add_header Cache-Control no-cache;
    add_header Strict-Transport-Security "max-age=31536000";
    rewrite /ddns/(.*) /$1 break;
    proxy_redirect / /ddns/;
}

这里添加了proxy_redirect / /ddns/;指令,这个指令会将后端返回的重定向响应头中的Location字段从/重写为/ddns/。这样,当后端应用重定向到/login时,Nginx会将其重写为/ddns/login。

@jeessy2
Copy link
Owner

jeessy2 commented Oct 31, 2024

这个是可以的,还处理了非标端口问题

location /ddns/ {
    proxy_pass http://your_ip:9876/;  # 将请求转发到后端服务

    # 替换响应头中的 Location 字段,使其指向二级目录
    proxy_redirect / $scheme://$http_host/ddns/;

    # 处理可能的路径问题
    sub_filter_types text/html application/javascript text/css;
    sub_filter 'href="/' 'href="/ddns/';
    sub_filter 'src="/' 'src="/ddns/';
    sub_filter_once off;

    # 其他代理设置
    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;
}

@jeessy2 jeessy2 closed this as completed Oct 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants