Nginx 如何配置 Basic Auth 认证

1, 使用 htpasswd 生成 passfile 文件

htpasswd 是 apache 实用工具的一部分,Nginx 的密码格式和 Apache 的一样。

CentOS 下的生成方法:

yum install -y httpd-tools # 安装
htpasswd -c -d /etc/nginx/pass_file username


Debian 下的生成方法:

sudo apt-get install apache2-utils
sudo htpasswd -c /etc/nginx/.htpasswd admin


2,添加到 nginx 配置

在 server 或者 local 下,添加 auth_basic 和 auth_basic_user_file。

location /test {
    auth_basic  "登录认证"; 
    auth_basic_user_file /data/conf.d/pass_file;
    
    autoindex on;
    autoindex_exact_size on;
    autoindex_localtime on;
}


参考:

https://www.howtogeek.com/devops/how-to-setup-basic-http-authentication-on-nginx/

https://www.tecmint.com/setup-nginx-basic-http-authentication/


修改时间 2022-11-16

声明:本站所有文章和图片,如无特殊说明,均为原创发布,转载请注明出处。
随机推荐
Node.js readline 模块
HTML textarea 文本输入框
Content Security Policy(CSP)简介
视频剪辑软件 Shotcut 笔记
JavaScript 鼠标事件
JavaScript 数组
WordPress 添加 Favicon 图标的方法
JavaScript getter和setter