newtonMiku
V2EX  ›  NGINX

关于反代的 location 中 uri 有 https 的问题

By newtonMiku at 2024 年 4 月 25 日 · 2739 次点击

RT,最近在香港的机器上做 GitHub 和 mikan 的反代,方便家里的设备在无代理情况下只需要增加一个前缀即可访问这些服务

    location ~ /^(?:https?:\/\/)?mikanani\.me(.*) {
        # 匹配到 URI 路径中含有"https://mikanani.me"的情况
        proxy_ssl_server_name on;
        proxy_pass 'https://mikanani.me/$1';
        sub_filter 'mikanani.me' $my_filter;
        sub_filter_once off;

        # Check if the request is for RSS
        add_header X-Request-URL https://mikanani.me$request_uri;
    }

正常的如果是 location /的话直接改对应的域名即可,但是想一个域名实现对好几个站的反代,(假设我的域名是 abc.com) 我访问 abc.com/abc 和访问 abc.com/https://mikanani.me/abc,两者都应该反代到 https://mikanani.me/abc ,求问该如何实现

7 条回复  •  2024-04-26 17:31:45 +08:00
mohumohu
   1
mohumohu  
   2024 年 4 月 25 日
用 caddy 会简单很多。
LinePro
   2
LinePro  
   2024 年 4 月 25 日
说个题外话,mikan 有官方的反代
https://mikanime.tv/
newtonMiku
   3
newtonMiku  
OP
   2024 年 4 月 25 日
@LinePro 好吧,那 GitHub 呢,主要是想研究下 nginx 能不能实现这种路径带完整连接的
ysc3839
   4
ysc3839  
   2024 年 4 月 26 日 via Android
不建议用字符串替换的方案,实际情况很复杂,总会出现一些问题的。
内网设备无代理访问的话,可以考虑用 sniproxy 。
newtonMiku
   5
newtonMiku  
OP
   2024 年 4 月 26 日
@ysc3839 不是啊,类似于 ghproxy 这种的,在 GitHub 的链接前加个前缀即可实现对 GitHub 资源的代理就行啊
ysc3839
   6
ysc3839  
   2024 年 4 月 26 日 via Android
@newtonMiku 你发的配置文件里不是有用到 sub_filter 吗?这个就是字符串替换。
newtonMiku
   7
newtonMiku  
OP
   2024 年 4 月 26 日
@ysc3839 #6 问题是我这个带原本 https 的匹配不上,不知道是写的正则有问题还是怎么,他只会匹配 location /的
› NGINX
› NGINX Trac
› 3rd Party Modules
› Security Advisories
› CHANGES
› OpenResty
› ngx_lua
› Tengine
在线学习资源
› NGINX 开发从入门到精通
NGINX Modules
› ngx_echo
© 2026 V2EX · 31ms · 3.9.8.5