nginx美化htpasswd认证页面全流程
时间: 2023-12-05 08:02:16 浏览: 123
nginx配置React静态页面的方法教程
1. 在服务器上安装nginx和htpasswd
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认证页面。
阅读全文