V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
jsjcjsjc
V2EX  ›  问与答

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

  •  
  •   jsjcjsjc · Jan 30, 2023 · 1297 views
    This topic created in 1186 days ago, the information mentioned may be changed or developed.

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

    比如访问 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 replies    2023-02-03 20:50:16 +08:00
    kaedeair
        1
    kaedeair  
       Jan 30, 2023   ❤️ 1
    可以搞一个摘要认证的中间件,通过匹配路径来重定向,认证完毕再转回去,之前用 traefik 搭建 htpc 的时候实现过
    cosmain
        2
    cosmain  
       Jan 30, 2023   ❤️ 1
    nginx 的 location 配置+http basic authentication
    jsjcjsjc
        3
    jsjcjsjc  
    OP
       Jan 30, 2023 via iPhone
    @cosmain 这个方案我以前试过,直连也要密码哈
    cosmain
        4
    cosmain  
       Jan 30, 2023
    @jsjcjsjc
    那说明你路径匹配没有写好。
    killva4624
        5
    killva4624  
       Jan 30, 2023   ❤️ 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
       Jan 30, 2023 via iPhone
    @kaedeair 是的,现成的方案吗?感谢
    kaedeair
        7
    kaedeair  
       Jan 30, 2023
    @jsjcjsjc 6# nginx 因为没怎么用过,所以不太熟悉,思路是差不多的
    2 楼的方案应该是能行的
    jsjcjsjc
        8
    jsjcjsjc  
    OP
       Jan 31, 2023
    @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  
       Feb 1, 2023   ❤️ 1
    @jsjcjsjc 你这里的目录有空格,需要双引号吧...
    jsjcjsjc
        10
    jsjcjsjc  
    OP
       Feb 1, 2023
    @killva4624 感谢回复.

    但似乎也不行哈~~
    killva4624
        11
    killva4624  
       Feb 2, 2023   ❤️ 1
    先去掉其他因素调试:

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

    然后 location 换成实际目录,正常后再改成 proxy_pass 。
    jsjcjsjc
        12
    jsjcjsjc  
    OP
       Feb 3, 2023
    @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
    ```
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5714 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 48ms · UTC 01:59 · PVG 09:59 · LAX 18:59 · JFK 21:59
    ♥ Do have faith in what you're doing.