nginx 设置htpasswd实现网站认证访问

#htpasswd 是开源 http 服务器密码认证生成工具
#Centos环境
#没有什么是一个视频不能解决的

yum -y install httpd
#首先创建文件生成密码:
htpasswd -c /usr/local/nginx/passwd root (创建文件并生成一个账户)
htpasswd -b /usr/local/nginx/passwd root root1 (增加账户)
#nginx.conf配置
auth_basic  "enter you password:";
auth_basic_user_file /usr/local/nginx/passwd;

**#在线观看视频更清楚

nginx使用htpasswd设置访问权限


**

  • 0
    点赞
  • 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、付费专栏及课程。

余额充值