Nginx配置Basic Auth登录认证的实现方法

有时候我们通过nginx搭建了一台文件服务器, 一般来讲是公开的, 但我们又希望该服务器不让他人看到, 有人可能会搭建一个登录系统, 但是太麻烦, 也没太大必要, 比较简单的做法是配置Basic Auth登录认证

1. 确定你安装了httpd-tools

1

yum install httpd-tools -y

2. 创建授权用户和密码

1

htpasswd -c -d /usr/local/openresty/nginx/conf/pass_file magina

这个配置文件存放路径可以随意指定, 这里我指定的是nginx配置文件目录, 其中magina是指允许登录的用户名, 这个可以自定义

3. 配置Nginx

大致配置如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

server {

  listen    80; 

  server_name res.yinnote.com;

 

  auth_basic  "登录认证";

  auth_basic_user_file /usr/local/openresty/nginx/conf/pass_file;

 

  autoindex on;

  autoindex_exact_size on;

  autoindex_localtime on;

 

  root  /mnt/html/resource;

  index index.html index.php;

}

其中 auth_basic 和 auth_basic_user_file 是认证的配置, 注意密码文件的路径一定是上面生成的

4. 使用

1

2

3

4

5

6

7

8

# 浏览器中使用

直接在浏览器中输入地址, 会弹出用户密码输入框, 输入即可访问

 

# 使用 wget

wget --http-user=magina --http-passwd=123456 http://res.yinnote.com/xxx.zip

 

# 使用 curl

curl -u magina:123456 -O http://res.yinnote.com/xxx.zip

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值