Nginx 访问控制
nginx 访问控制模块
1 2
| (1)基于IP的访问控制:http_access_module (2)基于用户的信任登录:http_auth_basic_module
|
1、基于IP的访问控制
1 2 3 4 5 6 7 8 9 10 11 12 13
| Syntax:allow address | all default:默认无 Context:http,server,location
Syntax:deny address | all default:默认无
Context:http,server,location ===================================================
allow 允许 //ip或者网段 deny 拒绝 //ip或者网段
|
示例一
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| [root@192 ~] server { listen 80 server_name localhost location / { root /usr/share/nginx/html index index.html index.hml deny 192.168.1.8 allow all } } [root@192 ~] [root@192 ~]
1.按顺序匹配,已经被匹配的ip或者网段,后面不再被匹配。 2.如果先允许所有ip访问,在定义拒绝访问。那么拒绝访问不生效。 3.默认为allow all
|
示例二
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| [root@192 ~] server { listen 80 server_name localhost location / { root /usr/share/nginx/html index index.html index.hml deny all } }
[root@192 ~] [root@192 ~]
|
2、基于用户的信任登录
1 2 3 4 5 6 7 8 9
| Syntax:auth_basic string | off default:auth_basic off Context:http,server,location
Syntax:auth_basic_user_file file default:默认无 Context:http,server,location file:存储用户名密码信息的文件。
|
示例一
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| [root@192 ~] server { listen 80 server_name localhost location ~ /admin { root /var/www/html index index.html index.hml auth_basic "Auth access test!" auth_basic_user_file /etc/nginx/auth_conf } }
[root@192 ~] [root@192 ~] [root@192 ~] [root@192 ~]
|
auth_basic不为off,开启登录验证功能,
auth_basic_user_file加载账号密码文件。
3、建立口令文件
登入需要密码
示例一
1 2 3 4 5 6
| [root@192 ~] [root@192 ~] [root@192 ~] [root@192 ~] user10:$apr1$MOa9UVqF$RlYRMk7eprViEpNtDV0n40 user20:$apr1$biHJhW03$xboNUJgHME6yDd17gkQNb0
|