NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
Moris
V2EX  ›  NGINX

请教一个关于 nginx 根据子目录进行反向代理的问题

  •  
  •   Moris · Aug 22, 2022 · 2974 views
    This topic created in 1362 days ago, the information mentioned may be changed or developed.

    请教一个关于 nginx 根据子目录进行反向代理的问题

    我现在有三个网页控制台,一个是 frps 的,一个是 cloudreve 的,还有一个是 portainer 的。

    我想要用 nginx 实现 https://abc.com/frps 转发到本地的 http://127.0.0.1:8080 ,对应的是 frps 控制台服务,

    https://abc.com/cloudreve 转发到本地的 http://127.0.0.1:8081 对应的是 cloudreve 控制台服务,

    https://abc.com/portainer 转发到本地的 http://127.0.0.1:8082 对应的是 portainer 控制台服务。

    nginx 对应配置文件部分如下

    server {	
    		
            listen 443
    		...
            location /cloudreve/ { #cloudreve 控制台
                    proxy_pass http://127.0.0.1:8081/;
                    }
    
            location /portainer/ { #docker portainer 控制台
                    proxy_pass https://127.0.0.1:8082/;
                    }
    
            location /frps/ { #frps 控制台
                    proxy_pass http://127.0.0.1:8080/;
                    }
    }
    

    但是只有 portainer 实现了需求,其余两个都没有办法访问。 /frps/和 /cloudreve/并没有被传递到对应的 url 后面,但是没办法访问,结果都显示 nginx 的 404 页面。 请教一下这是为什么?

    如果我开启对子域名的解析,让 cloudreve.abc.com 能够被解析到 http://127.0.0.1:8081 , 而且 location 匹配的是根目录,nginx 配置文件如下

    server
            {
    
            listen 443 ssl;
            server_name  frps.abc.com;
            ...
    
            location / {
                    proxy_pass http://127.0.0.1:8080;
                    }
            }
            
    server
            {
    
            listen 443 ssl;
            server_name  cloudreve.abc.com;
            ...
    
            location / {
                    proxy_pass http://127.0.0.1:8081;
                    }
            }
    

    此时直接访问 https://frps.abc.comhttps://cloudreve.abc.com 都能够成功访问。

    我不理解为什么 location 改为非根目录就无法访问了,按理来说 https://abc.com/frps/ 也应该指向 http://127.0.0.1:8080 才对啊,问题出在哪呢?

    7 replies    2022-08-22 20:14:30 +08:00
    akira
        1
    akira  
       Aug 22, 2022
    错误日志呢,看他实际访问的是哪个后端地址去了
    Seanfuck
        2
    Seanfuck  
       Aug 22, 2022
    不要斜杠结尾
    location /frps { #frps 控制台
    proxy_pass http://127.0.0.1:8080;
    }
    Moris
        3
    Moris  
    OP
       Aug 22, 2022
    @Seanfuck 我试过不加斜杠和加上斜杠的区别,frps 不加斜杠可以加载出网页,但是详细的 frps 数据却没有,反正还是不正常工作。cloudreve 无论加不加斜杠都不行
    qjbcnrs
        4
    qjbcnrs  
       Aug 22, 2022
    LindsayZhou
        5
    LindsayZhou  
       Aug 22, 2022   ❤️ 1
    猜测原因是后端项目里用了绝对路径,比如说如果某个项目需要加载 "/js/script.js",或者 "/socket" ,这样不会击中你 nginx 的规则。
    sutra
        6
    sutra  
       Aug 22, 2022
    也许你需要 X-Forwarded-Prefix ,然后让后端也支持它。或者利用 sub_filter 来过滤响应内容里的路径。
    zbinlin
        7
    zbinlin  
       Aug 22, 2022
    proxy_pass 参数后不加斜杠是对的,至于这样还不能正常工作,应该在后端程序里查下
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   942 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 46ms · UTC 19:38 · PVG 03:38 · LAX 12:38 · JFK 15:38
    ♥ Do have faith in what you're doing.