Nginx

Nginx的访问控制

2019-05-23  本文已影响0人  燃燃的爸爸

基于IP的访问控制 http_access_module
基于用户的信任登录 http_auth_basic_module

一、http_access_module

Syntax: [allow|deny] address | CIDR | unix: | all;
Default: -
Context:http,server,location,limit_except
location ~ ^/admin.html {
        root   /usr/share/nginx/html;
        deny 192.168.2.94;
        allow all;
        index  index.html index.htm;
    }

1.局限性:

http_access_module对应的是remote_addr,如果通过代理那么只通过remote_addr并不能很好的隔离。


image.png

那么就想到了http_x_forwarded_for 他可以记录通过的这些路径IP。
http_x_forwarded_for ClientIP,Proxy(1)IP,Proxy(2)IP...

2.解决局限性的方法

方法一、采用别的HTTP头信息控制访问,如:HTTP_X_FORWARD_FOR(但该方式也存在问题,因为存在头部可能被修改或者CDN厂商这种不一定传递)
方法二、结合geo模块作
方法三、通过HTTP自定义变量传递

二、http_auth_basic_module

Syntax: auth_basic string | off;
Default: auth_basic off;
Context:http,server,location,limit_except

Syntax: auth_basic_user_file off;
Default: auth_basic off;
Context:http,server,location,limit_except

生成秘钥文件

[root@localhost nginx]# htpasswd -c ./auth_conf jeson
New password:
Re-type new password:
location ~ ^/admin.html {
        root   /usr/share/nginx/html;
        auth_basic "Auth access test! input your password!";
        auth_basic_user_file /etc/nginx/auth_conf;
        index  index.html index.htm;
    }

测试:


image.png

局限性:
1)依赖文件 效率低下
2)操作管理繁琐

解决方案:
一、Nginx 结合LUA实现高效验证
二、Nginx和LDAP打通,利用nginx-auth-ldap模块

上一篇 下一篇

猜你喜欢

热点阅读