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

聊聊一个Nginx应用:Nginx配置Basic Auth登录认证的实现方法。

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

1. 确定你安装了httpd-tools

yum install httpd-tools -y

2. 创建授权用户和密码

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

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

3. 配置Nginx

大致配置如下:

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. 使用

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

# 使用 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

作者:terry,如若转载,请注明出处:https://www.web176.com/news/frontend/27935.html

(0)
打赏 支付宝 支付宝 微信 微信
terryterry
上一篇 2024年1月12日
下一篇 2024年1月15日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注