2020-06-13 19:51:02
为Nginx添加basic_auth,意思就是访问页面的时候需要弹出来一个用户和密码验证的东西,本文基于CentOS 6
yum install httpd-tools #适用centos
sudo apt-get install apache2-utils #适用ubuntu
生成用户密码文件
$ htpasswd -c /var/www/html/.htpasswd user1 #回车会要求输入两遍密码,会清除所有用户!
$ htpasswd -bc /var/www/html/.htpasswd user1 password #不用回车,直接指定user1的密码为password
$ htpasswd -b /var/www/html/.htpasswd user2 password #添加一个用户,如果用户已存在,则是修改密码
$ htpasswd -D /var/www/html/.htpasswd user2 #删除用户
server {
listen 80;
# root /tmp;
# index index.html index.htm;
server_name zhukun.net www.zhukun.net;
location / {
auth_basic "input you user name and password";
auth_basic_user_file /export/servers/.htpasswd;
proxy_pass http://127.0.0.1:9000;
}
}
然后再次访问zhukun.net时便会弹出验证框要求输入用户名和密码。
访问zhukun.net没有弹出验证框怎么办?
首先修改nginx.conf,将日志级别调为info,如下
$ cat /export/servers/nginx-1.12.1/conf/nginx.conf
.......
user admin;
worker_processes 8;
error_log logs/error.log info;
......
然后再次访问让其产error_log
看到error_log时会发现有如下错误产生
*69 no user/password was provided for basic authentication, client: 10.12.138.126, server: www.zhukun.net, request: "GET /date_lateral HTTP/1.1", host: "www.zhukun.net"
原因在于
The HTTP Basic authentication works as following:
*) A browser requests a page without user/password.
*) A server response with 401 page, sending realm as well.
At this stage the 401 code appears in access_log and the message
“no user/password …” appears in error_log.
*) The browser shows a realm/login/password prompt.
*) If a user will press cancel, then the browser will show the received
401 page.
*) If the user enters login/password, then the browser repeats the request
with login/password.
Then until you will exit the browser, it will send these login/password
with all requests in protected hierarchy.
error_page配置的401页面不存在或者指向问题导致的,可以注释掉401配置或者保证401配置指向的文件可用,然后basic_auth便会生效。
Ubuntu 16.04配置Nginx使用GeoIP
07-12
【工作】Nginx 实现域名的登录验证功能
07-04
Nginx 启用 Brotli 压缩
05-09
Nginx使用教程(一):下载并编译安装Nginx
07-07
Nginx使用教程(四):提高Nginx网络吞吐量之buffers优化
04-05
Nginx和Apache如何设置ajax跨域
05-09
CentOS7源码编译安装Nginx
06-11
OpenResty(Nginx Lua)统计网站访问信息
07-07
如何在CentOS 7上安装Nginx
07-05
配置Vsftpd MySQL虚拟用户验证登录
03-31
配置haproxy在http请求头添加后端用户真实IP
07-05
BIOS中设置开机密码无效,如何解决?
06-03
CentOS 7配置并更换为本地或网络yum源
07-17
Debian 7 (Wheezy)安装配置Apache mod_fastcgi PHP-FPM
07-20
Docker安装配置tomcat jdk
06-21
Jenkins运行多个步骤的配置
06-21
Linux CentOS 7 重置root用户密码
07-12
Linux 的系统服务及其配置
07-07
TR260 G2服务器内存DIMM安装配置和内存插的槽分布
06-23
Ubuntu系统配置samba实现文件夹共享
06-22
visual basic 下载
5.55MB
下载winx dvd author(dvd烧录软件) 6.3.0 中文免费版
10.5 MB
下载密码键盘下载
1.6M
下载Buttercup(密码管理软件) v1.20.5 官方版
76.2M
下载Eziriz IntelliLock(密码保护软件) v2.8.5.0 免费版
15.13M
下载PassFab for PDF(PDF密码恢复工具) v8.1.0 免费版
23.36M
下载adsl密码查看器(宽带密码查看工具)v6.6.2 免费版
331KB
下载biospwds(bios密码获取软件)v1.21 免费版
127.1K
下载elcomsoft wireless security auditor(无线网络密码破解软件) v5.5.282.1 最新版
46.5M
下载h3c模拟器(网络虚拟环境配置工具) 1.5.2 正式版
59.1M
下载unlocker(密码解锁工具) v3.0.3.4 中文版
32.1M
下载webcrack(路由器密码破解工具)v4.0 中文版
1.13MB
下载效能密码管理器 v5.60.547 中文版
15.19M
下载疯师傅苹果密码解锁(iOS设备解锁软件) 5.6.0 官方版
1.1M
下载Alternate Password DB下载
714.9K
下载PDF Password Recover下载
8.1M
下载advanced rar password recovery下载
4.3M
下载routerpassview下载
120.8K
下载