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

声明:本站所有文章和图片,如无特殊说明,均为原创发布,转载请注明出处。
随机推荐
WordPress 文章页作者信息 get_the_author() 和 get_the_author_meta()
Node.js net 模块
JavaScript 使用剪切板
Node.js 内置模块
Page Visibility API
WordPress 文章排序
JavaScript 私有方法和私有属性
Cookie 的 HTTP Only 属性