jsjcjsjc
V2EX  ›  问与答

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

  •  
  •   jsjcjsjc · Jan 30, 2023 · 1706 views
    This topic created in 1382 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
    kaedeair
    Jan 30, 2023❤️1
    1
    可以搞一个摘要认证的中间件,通过匹配路径来重定向,认证完毕再转回去,之前用 traefik 搭建 htpc 的时候实现过
    cosmain
    cosmain
    Jan 30, 2023❤️1
    2
    nginx 的 location 配置+http basic authentication
    jsjcjsjc
    jsjcjsjc
    OP
    Jan 30, 2023 via iPhone
    3
    @cosmain 这个方案我以前试过,直连也要密码哈
    cosmain
    cosmain
    Jan 30, 2023
    4
    @jsjcjsjc
    那说明你路径匹配没有写好。
    killva4624
    killva4624
    Jan 30, 2023❤️1
    5
    location ~ ^/folder/\S+
    {
    ...
    # Process with files
    }

    location ~ ^/folder {
    ....
    # Process with auth
    # auth_basic "Administrator Login";
    # auth_basic_user_file /other_folder/.htpasswd;
    }
    jsjcjsjc
    jsjcjsjc
    OP
    Jan 30, 2023 via iPhone
    6
    @kaedeair 是的,现成的方案吗?感谢
    kaedeair
    kaedeair
    Jan 30, 2023
    7
    @jsjcjsjc 6# nginx 因为没怎么用过,所以不太熟悉,思路是差不多的
    2 楼的方案应该是能行的
    jsjcjsjc
    jsjcjsjc
    OP
    Jan 31, 2023
    8
    @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
    killva4624
    Feb 1, 2023❤️1
    9
    @jsjcjsjc 你这里的目录有空格,需要双引号吧...
    jsjcjsjc
    jsjcjsjc
    OP
    Feb 1, 2023
    10
    @killva4624 感谢回复.

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

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

    然后 location 换成实际目录,正常后再改成 proxy_pass 。
    jsjcjsjc
    jsjcjsjc
    OP
    Feb 3, 2023
    12
    @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   ·   Privacy   ·   Solana   ·   1070 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 23:06 · PVG 07:06 · LAX 15:06 · JFK 18:06
    ♥ Do have faith in what you're doing.