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

反向代理一个网站,可以设置指定目录密码,但是直连无需密码?

  •  
  •   jsjcjsjc · 2023-01-30 09:19:43 +08:00 · 795 次点击
    这是一个创建于 451 天前的主题,其中的信息可能已经有所发展或是发生改变。

    也就是只是点击这个目录的时候需要密码,但是直连目录下的文件不需要密码

    比如访问 https://drive.swo.moe/zh-CN/Shared%20files/的时候需要密码,但是直连 https://drive.swo.moe/api/raw/?path=/Shared%20files/genshin-impact-npc-scene-interaction.mp4 不需要密码.

    是不是不大可能,由于服务在 serverless 上,不大好操作哈,不知道 nginx 或者 cloudflare 可以实现吗? 有大佬愿意的话,收费也可以哈

    感谢

    12 条回复    2023-02-03 20:50:16 +08:00
    kaedeair
        1
    kaedeair  
       2023-01-30 09:52:45 +08:00   ❤️ 1
    可以搞一个摘要认证的中间件,通过匹配路径来重定向,认证完毕再转回去,之前用 traefik 搭建 htpc 的时候实现过
    cosmain
        2
    cosmain  
       2023-01-30 10:01:20 +08:00   ❤️ 1
    nginx 的 location 配置+http basic authentication
    jsjcjsjc
        3
    jsjcjsjc  
    OP
       2023-01-30 10:06:29 +08:00 via iPhone
    @cosmain 这个方案我以前试过,直连也要密码哈
    cosmain
        4
    cosmain  
       2023-01-30 10:09:35 +08:00
    @jsjcjsjc
    那说明你路径匹配没有写好。
    killva4624
        5
    killva4624  
       2023-01-30 10:17:59 +08:00   ❤️ 1
    location ~ ^/folder/\S+
    {
    ...
    # Process with files
    }

    location ~ ^/folder {
    ....
    # Process with auth
    # auth_basic "Administrator Login";
    # auth_basic_user_file /other_folder/.htpasswd;
    }
    jsjcjsjc
        6
    jsjcjsjc  
    OP
       2023-01-30 10:40:00 +08:00 via iPhone
    @kaedeair 是的,现成的方案吗?感谢
    kaedeair
        7
    kaedeair  
       2023-01-30 10:48:34 +08:00
    @jsjcjsjc 6# nginx 因为没怎么用过,所以不太熟悉,思路是差不多的
    2 楼的方案应该是能行的
    jsjcjsjc
        8
    jsjcjsjc  
    OP
       2023-01-31 20:14:47 +08:00
    @killva4624 感谢,后面两个 location 是不是有问题哈~~

    ```

    location / {
    proxy_ssl_name drive.swo.moe;
    proxy_ssl_server_name on;
    proxy_redirect off;

    proxy_pass https://drive.swo.moe;
    proxy_set_header Host drive.swo.moe;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    proxy_cookie_domain drive.swo.moe test.domain.com; #
    proxy_set_header User-Agent $http_user_agent;
    proxy_set_header Referer drive.swo.moe;
    proxy_set_header Accept-Encoding "";

    sub_filter 'drive.swo.moe' 'test.domain.com';
    sub_filter_once off;

    }

    location ~ ^/Shared files/\S+
    {
    proxy_pass https://drive.swo.moe/Shared files;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    }

    location ~ ^/Shared files {
    proxy_pass https://drive.swo.moe/Shared files;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;

    auth_basic "Administrator Login";
    auth_basic_user_file /var/.htpasswd;
    }
    ```
    killva4624
        9
    killva4624  
       2023-02-01 10:58:42 +08:00   ❤️ 1
    @jsjcjsjc 你这里的目录有空格,需要双引号吧...
    jsjcjsjc
        10
    jsjcjsjc  
    OP
       2023-02-01 19:46:19 +08:00
    @killva4624 感谢回复.

    但似乎也不行哈~~
    killva4624
        11
    killva4624  
       2023-02-02 10:13:36 +08:00   ❤️ 1
    先去掉其他因素调试:

    - 用不带空格的目录名;
    - proxy_pass 改成本地文件目录(root 或者 alias ,可以打开 debug 日志看最后访问的实际目录);

    然后 location 换成实际目录,正常后再改成 proxy_pass 。
    jsjcjsjc
        12
    jsjcjsjc  
    OP
       2023-02-03 20:50:16 +08:00
    @killva4624 再次感谢,似乎问题出在这里,这样写好像是不规范的.
    ```
    location ~ ^/Shared/\S+
    {
    proxy_pass https://drive.swo.moe/Shared;
    }
    ```

    会报错
    ```
    nginx: [emerg] "proxy_pass" cannot have URI part in location given by regular expression, or inside named location, or inside "if" statement, or inside "limit_except" block in /www/server/panel/vhost/nginx/test.1ka.net.conf:55
    nginx: configuration file /www/server/nginx/conf/nginx.conf test failed
    ```
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2687 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 16:01 · PVG 00:01 · LAX 09:01 · JFK 12:01
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.