Nginx-http_auth_basic_module使用


前言

nginx可以通过HTTP Basic Authutication协议进行用户名和密码的认证。


一、ngx_http_auth_basic_module

生效阶段: NGX_HTTP_ACCESS_PHASE
模块默认编译进nginx,通过–without-http_auth_basic_module禁用。

二、指令

1.auth_basic

使用示例:

Syntax:	auth_basic string | off;
Default:	
auth_basic off;
Context:	http, server, location, limit_except

1.auth_basic_user_file

使用示例:

Syntax:	auth_basic_user_file file;
Default:	—
Context:	http, server, location, limit_except

示例

生成密码文件

Usage:
    htpasswd [-cmdpsD] passwordfile username
    htpasswd -b[cmdpsD] passwordfile username password

    htpasswd -n[mdps] username
    htpasswd -nb[mdps] username password
 -c  Create a new file. # 创建一个新的文件
 -n  Don't update file; display results on stdout. # 直接输出产生的结果,无需写入或者更新文件。
 -m  Force MD5 encryption of the password.#  采用md5哈希
 -d  Force CRYPT encryption of the password (default).
 -p  Do not encrypt the password (plaintext).
 -s  Force SHA encryption of the password.
 -b  Use the password from the command line rather than prompting for it. # 密码会放在命令行里。
 -D  Delete the specified user. # 删除某个账户
# 生成密码 
htpasswd -cb  auth.pass  admin admin@6688
#查看
[root@test20 nginx]# cat auth.pass 
admin:$apr1$sZKLzS5n$DA2E0qxK3Rap7QmyWzKIR/

配置basic认证

[root@test20 ~]# cat /etc/nginx/conf.d/access.conf 
server {
	server_name access.test.io;
	root html/;
	location / {
		auth_basic "auth_basic认证";
		auth_basic_user_file auth.pass;
	}
}

浏览器验证

在这里插入图片描述
在这里插入图片描述


总结

basic认证在一些场景还是有用的,但是basic毕竟是http协议下的,可以通过抓包获取到密码。

### 使用 Nginx 配置 WebDAV 服务 为了通过 Nginx 实现 WebDAV 功能,需先安装必要的模块并调整配置文件。 #### 安装依赖项和编译工具 在 Linux 系统上,建议使用包管理器来获取所需的开发库: 对于基于 Debian 的系统: ```bash apt-get update && apt-get install -y build-essential libpcre3-dev zlib1g-dev libssl-dev nginx ``` 对于基于 RedHat 的系统: ```bash yum groupinstall "Development Tools" yum install pcre-devel openssl-devel zlib-devel epel-release nginx ``` #### 获取并编译带有 WebDAV 支持的 Nginx 版本 由于默认版本可能不包含 `ngx_http_dav_module` 或者扩展功能不足,推荐下载源码重新编译加入额外支持[^1]。 克隆 GitHub 上由 arut 维护的增强型 WebDAV 模块仓库到本地路径 `/root/nginx-dav-ext-module` 下: ```bash git clone https://github.com/arut/nginx-dav-ext-module.git /root/nginx-dav-ext-module ``` 接着从官网下载最新稳定版 Nginx 压缩包解压后进入目录执行如下命令完成自定义构建过程(假设当前工作区位于已解压缩后的 Nginx 文件夹内): ```bash ./configure --with-http_ssl_module --add-module=/root/nginx-dav-ext-module make make install ``` #### 修改 Nginx 主机配置启用 WebDAV 接口 编辑站点对应的虚拟主机配置文件,在 server{} 中添加 location 节点指定共享资源位置以及允许的操作权限集。下面是一个简单的例子说明如何设置只读访问控制列表 ACL 和身份验证机制 Basic Auth 来保护上传接口的安全性: ```nginx server { listen 80; server_name example.org; # 设置WebDAV根目录 dav_methods PUT DELETE MKCOL COPY MOVE; create_full_put_path on; autoindex on; auth_basic "Restricted Area"; auth_basic_user_file conf/htpasswd.users; location /private/ { root html; dav_access user:rw group:r all:r; satisfy any; allow 192.168.1.0/24; deny all; } } ``` 上述配置实现了基本的身份验证,并限定了特定 IP 地址范围内的客户端可以写入数据至 `/html/private/` 子目录下;其他请求则仅能浏览现有内容而无法修改它们。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值