使用htpasswd创建密码文件做nginx的目录认证 in ubuntu

5 篇文章 0 订阅
3 篇文章 0 订阅

当需要在Nginx的配置文件添加对目录的auth_basic时,通常需要用到htpasswd来生成密码文件。

htpasswd是apache2的自带工具,安装apache2后在/usr/bin/可以找到此文件。

创建密码文件命令如下:

1:先走到/etc/nginx

2:sudo htpasswd -c -d pass_file user_name

要注意的是:

  • 如果当前用户不是管理员,需要使用sudo
  • -c 表示创建文件,-d 表示对密码进行CRYPT加密

Nginx的auth_basic认证:

location /nginx {
    auth_basic   "NginxStatus";
    auth_basic_user_file  pass_file;
    stub_status     on;
    access_log       off;
    #allow  127.0.0.1;
    #deny    all;
}

 需要注意的几点是:

  • pass_file跟nginx.conf文件在同级目录下
  • stub_status查看nginx的运行情况
  • 另外auth_basic 可以针对具体的某个文件进行权限认证,只需修改location的匹配即可。

以上操作都是在ubuntu系统上验证过的,真实有效,放心使用!酷

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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认证页面。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值