Nginx的访问控制

nginx的访问控制主要分为两类:

  • 基于IP的访问控制 http_access_module
  • 基于用户的信任登录 http_auth_basic_module
对于http_access_module模块:

模块允许限制访问某些客户端地址。访问也可以通过密码子请求结果或JWT来限制满足控制地址和密码的同时访问限制。

配置语法:

Syntax:   allow address | CIDR | unix: | all;
Default:  —
Context:  http, server, location, limit_except

Syntax:   deny address | CIDR | unix: | all;
Default:  —
Context:  http, server, location, limit_except

语法中address表示地址,CIDR表示网段。unix:指定了特殊值,则允许访问所有UNIX域套接字。all表示所有的。

实例:配置访问控制
首先我们查看没有限制的时候进行的输出

《Nginx的访问控制》 图片.png

编辑配置文件

    location ~ ^/admin.html {
        root   /opt/app/code;
        deny 122.233.229.350;
        allow all;
        index  index.html index.htm;
    }

其中 ~ 表示对请求路径URL模式匹配,表示跟目下以admin.html开头的家目录设置在/opt/app/code。

然后检查配置重启

[root@hongshaorou conf.d]# nginx -tc /etc/nginx/nginx.conf 
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@hongshaorou conf.d]# systemctl reload nginx

重新访问我们的页面

《Nginx的访问控制》 图片.png

现在配置本机可以访问

    location ~ ^/admin.html {
        root   /opt/app/code;
        allow 122.233.329.0/24;
        deny all;
        index  index.html index.htm;
    }

然后检查配置重启

[root@hongshaorou conf.d]# nginx -tc /etc/nginx/nginx.conf 
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@hongshaorou conf.d]# systemctl reload nginx

重新访问我们的页面

《Nginx的访问控制》 图片.png

http_access_module模块的局限性:

《Nginx的访问控制》 图片.png

nginx的访问控制限制是针对客户端的IP来进行限制的,但是nginx并不确定真正的客户端是哪个,凡是和nginx进行交互的都被当做是客户端。(remote_addr是直接和nginx通信的IP)如果我们访问不是直接访问到服务端而是由中间代理进行(如上图),访问控制这时就会失效。

局限性解决方法总结:

方法一: 采用http头信息控制访问,如HTTP_X_FORWARD_FOR
方法二: 结合geo模块
方法三: 通过HTTP自定义变量传递

http_x_forwarded_for头信息控制访问 会更好的解决该问题,它要求访问时必须带上所有用到的ip的地址信息
我们看一下http_x_forwarded_for记录过程:

http_x_forwarded_for = Client IP, Proxy(1)IP, Proxy(2)IP,...

《Nginx的访问控制》 图片.png

http_auth_basic_module模块

配置语法

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

Syntax:     auth_basic_user_file file;
Default:    —
Context:    http, server, location, limit_except

语法讲解:
auth_basic 默认关闭,开启的话输入一段字符串即可。
auth_basic_user_file 该文件存储用户账号密码。

我们看一下官网的文件格式

# comment
name1:password1
name2:password2:comment
name3:password3

密码加密方式有多中这里我们使用htpasswd

can be generated using the “htpasswd” utility from the Apache HTTP Server distribution or the “openssl passwd” command

想要使用该方式,首先要装对应的包

[root@hongshaorou conf.d]# yum -y install httpd-tools

接下来创建对应的use_file文件

[root@hongshaorou conf.d]# cd ..
[root@hongshaorou nginx]# ls
conf.d          koi-utf  mime.types  nginx.conf   uwsgi_params
fastcgi_params  koi-win  modules     scgi_params  win-utf
[root@hongshaorou nginx]# htpasswd -c ./auth_conf hongshaorou
New password: 
Re-type new password: 
Adding password for user hongshaorou
[root@hongshaorou nginx]# cat auth_conf 
hongshaorou:$apr1$kNj29q4w$zOu18mCbh06.nOTGNMe7h/

编辑配置文件

    location ~ ^/admin.html {
        root   /opt/app/code;
    auth_basic "Auth access test! input your password!";
    auth_basic_user_file /etc/nginx/auth_conf;
        index  index.html index.htm;
    }

进行语法检查

[root@hongshaorou conf.d]# nginx -tc /etc/nginx/nginx.conf 
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@hongshaorou conf.d]# systemctl reload nginx

现在我们再次访问页面的时候就需要输入用户名密码

《Nginx的访问控制》 图片.png

输入刚才的账号密码就可以正常访问了。

局限性:

一: 用户信息依赖文件
二: 操作管理机械,效率低
解决方式:

一: nginx结合LUA实现高效验证
二: nginx配合LDAP打通,利用nginx-auth-ldap模块

    原文作者:海贼之路飞
    原文地址: https://www.jianshu.com/p/9f68da37bcaf
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞