Nginx中使用htpasswd

上一篇博客介绍了Nginx添加第三方模块并实现平滑启动 其中留下了一个问题?

监控Nginx服务器运行情况的模块。只要用户在浏览器输入http://your_ip/status,就可以访问监控页面。这样很不安全,因为任何人都可以访问这个页面。是否可以再添加一个授权模块呢?

答案是肯定的,Nginx的源码提供了ngx_http_auth_basic_module这个模块,它可以来解决这个问题。这个模块是默认就编译进nginx的,可以直接拿来使用。

ngx_http_auth_basic_module它提供了最基本的http认证,这是http协议支持的,它会弹出一个框让你输入用户名和密码,只有用户名和密码输入正确了才能访问,这样就能防止http://your_ip/status被任何人访问了。

ngx_http_auth_basic_module是使用文件作为存储介质的,用户名是明文存储,而密码是加密之后再存储,这样在认证框输入的用户名和密码必须和文件的信息匹配才能认证成功。这里使用htpasswd这个命令来生成存放用户名和密码的文件。

什么是 htpasswd ?

htpasswd 是开源 http 服务器 apache httpd 的一个命令工具,用于生成 http 基本认证的密码文件。

安装

ubuntu

sudo apt-get install apache2-utils

centos

yum -y install httpd 

使用

语法
htpasswd(选项)(参数)

htpasswd使用帮助

htpasswd 选项说明

-c 创建passwdfile.如果passwdfile 已经存在,那么它会重新写入并删去原有内容.
-n 不更新passwordfile,只将加密后的用户名密码显示在屏幕上;
-m 默认采用MD5算法对密码进行加密
-d 采用CRYPT算法对密码进行加密
-p 不对密码进行进行加密,即使用普通文本格式的密码
-s 采用SHA算法对密码进行加密
-b 命令行中一并输入用户名和密码而不是根据提示输入密码,可以看见明文,不需要交互
-D 删除指定的用户

htpasswd 参数说明

  • 用户:用户名
  • 密码:用户的密码
实例

利用htpasswd命令添加用户

shell> htpasswd -c /usr/local/nginx/.htpasswd Javen
New password:
Re-type new password:
Adding password for user Javen

或者不用交互的模式

shell> htpasswd -bc /usr/local/nginx/.htpasswd Javen javen205

/usr/local/nginx/目录下生成一个.htpasswd文件,用户名Javen,密码javen205,默认采用MD5加密方式。

在原有密码文件中增加下一个用户

去掉-c选项即可在第一个用户之后添加第二个用户

shell> htpasswd -b /usr/local/nginx/.htpasswd JJ jj

不更新密码文件,只显示加密后的用户名和密码

不更新.passwd文件,只在屏幕上输出用户名和经过加密后的密码。

shell> htpasswd -nb Javen Javen205

利用htpasswd命令删除用户名和密码

shell>  htpasswd -D /usr/local/nginx/.htpasswd  Javen
Deleting password for user Javen

利用htpasswd命令修改密码

htpasswd 并没有直接修改密码的函数,需要先使用htpasswd删除命令删除指定用户,再利用htpasswd添加用户命令创建用户即可实现修改密码的功能。

htpasswd -D .htpasswd Javen
htpasswd -b .htpasswd Javen javen205

Nginx中使用htpasswd

在配置文件中打开http basic auth认证。

location /status {
     auth_basic "Restricted";
     auth_basic_user_file /usr/local/nginx/.htpasswd;
     vhost_traffic_status_display;
     vhost_traffic_status_display_format html;
}

效果图

访问时提示密码框

401没有权限

个人能力有限如有错误欢迎指正。你有更好的解决方案或者建议欢迎一起交流讨论,如有疑问欢迎留言。

1. 在服务器上安装nginxhtpasswd 2. 创建一个新的htpasswd文件并添加用户和密码 ```bash sudo htpasswd -c /etc/nginx/.htpasswd username ``` 3. 配置nginx服务器以使用htpasswd文件进行身份验证 ```nginx server { listen 80; server_name example.com; root /var/www/html; location / { auth_basic "Restricted Content"; auth_basic_user_file /etc/nginx/.htpasswd; index index.html; } } ``` 4. 下载并安装nginx-auth-ldap模块 ```bash sudo apt-get install libldap2-dev sudo apt-get install nginx-extras ``` 5. 编辑nginx配置文件以启用nginx-auth-ldap模块 ```nginx ldap_server ldapserver { url ldap://ldap.example.com/ou=people,dc=example,dc=com?uid?sub?(objectClass=posixAccount); binddn "cn=admin,dc=example,dc=com"; binddn_passwd "password"; group_attribute memberUid; group_attribute_is_dn on; require valid_user; } server { listen 80; server_name example.com; root /var/www/html; location / { auth_ldap "Restricted Content"; auth_ldap_servers ldapserver; index index.html; } } ``` 6. 自定义htpasswd认证页面 ```nginx server { listen 80; server_name example.com; root /var/www/html; location / { auth_basic "Restricted Content"; auth_basic_user_file /etc/nginx/.htpasswd; error_page 401 /401.html; index index.html; } location = /401.html { internal; root /var/www/html; } } ``` 7. 创建自定义htpasswd认证页面 ```html <!DOCTYPE html> <html> <head> <title>Authentication Required</title> <style> body { font-family: Arial, Helvetica, sans-serif; margin: 0; padding: 0; background-color: #f1f1f1; } .container { padding: 20px; background-color: #fff; border-radius: 5px; margin-top: 50px; margin-left: auto; margin-right: auto; width: 300px; } h1 { text-align: center; } form { text-align: center; } input[type=password] { padding: 12px 20px; margin: 8px 0; box-sizing: border-box; border: none; background-color: #f1f1f1; } button[type=submit] { background-color: #4CAF50; color: white; padding: 12px 20px; margin: 8px 0; border: none; border-radius: 4px; cursor: pointer; width: 100%; } button[type=submit]:hover { background-color: #45a049; } </style> </head> <body> <div class="container"> <h1>Authentication Required</h1> <form> <label for="username">Username:</label><br> <input type="text" id="username" name="username"><br> <label for="password">Password:</label><br> <input type="password" id="password" name="password"><br><br> <button type="submit">Login</button> </form> </div> </body> </html> ``` 8. 重新加载nginx配置 ```bash sudo service nginx reload ``` 9. 访问受保护的页面并查看美化后的htpasswd认证页面。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值