Nginx (可为容器)配置 BasicAuth 与访问

创建认证信息

在 Nginx 容器内部,下载安装 apache2-utils 。

apt-get install apache2-utils

创建用于保存认证信息的 .db 文件夹。

mkdir /etc/nginx/basicauth/

使用 apache2-utils 工具集中的 htpasswd 工具创建用于保存认证信息的 .db 文件。

htpasswd -c /etc/nginx/basicauth/example_auth.db example_account_name
New password: 
Re-type new password:

创建完成后,可查看认账信息:

cat  /etc/nginx/basicauth/example_auth.db
example_account_name:$aaaa$bbbbbbbbbb$ccccccccccccccccccc

配置 .conf 文件

对于 Nginx 的 .conf 配置文件,简单来说包含一大一小两个模块:

server {
    server_name www.yogile.site;
    listen 80;
    ......
    location / {
        ......
    }
}

无视 location 映射规则

如果想要无视 location 映射规则,对于监听的域名下所有 URL 的访问都添加 BasicAuth ,只需要在 location 前添加配置即可。

server {
    server_name www.yogile.site;
    listen 80;
    ......
    auth_basic "上不上?";
    auth_basic_user_file /etc/nginx/basicauth/example_auth.db;
    location / {
        ......
    }
}

特定 location 映射规则

要想针对监听的域名下特定 URL 的访问添加 BasicAuth ,只需要在目标 location 中添加即可。

server {
    server_name www.yogile.site;
    listen 80;
    ......
    location / {
        auth_basic "上不上?";
        auth_basic_user_file /etc/nginx/basicauth/example_auth.db;
        ......
    }
}

在 URL 中认证登录

有时候需要在软件中访问被 BasicAuth 认证的站点,要使用也非常简单。

将用户名和密码由 : 英文冒号分割,加上 @ 添加到 https://blivechat 字符之间。

Copyhttps://[用户名]:[密码]@blivechat.tools.yogile.site/room/7866049?......

示例:

Copyhttps://Account:Password@blivechat.tools.yogile.site/room/7866049?......

将插入后的 URL 复制到 OBS 中,即可像往常一样使用,不用第二次认证。

img

原文地址:https://www.cnblogs.com/Yogile/p/15049795.html