linux location root访问文件夹404_Nginx学习(六):Nginx访问控制

介绍

Nginx的访问控制分为:

  • 基于IP的访问控制(http_access_module)

  • 基于用户的信任登录(http_auth_basic_module)


一、基于IP的访问控制

配置语法:

Syntax:  allow address|CIDR|unix:|all;Default: --Context: http,server,location,limit_except
  • address:表示ip地址

  • CIDR:表示网段。例:192.168.1.0-24

  • unix:主要在linux,unix上面用到的socket方式的访问

  • all:允许所有的

与之对应的不允许访问的配置语法:

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

二、基于IP的访问控制演示

查询自己本机的出口ip:

https://www.ip138.com/ // 公网查看本机ipipconfig/ifconfig // 内网环境查看本机ip

新建admin.html:

    admin

Admin

修改配置文件:

[root@node1 conf.d]# mv default.conf access_mod.conflocation ~ ^/admin.html {        root   /opt/app/code;        deny 192.168.x.xxx;        allow all;        index  index.html index.htm;    }

重载nginx服务:

[root@node1 conf.d]# nginx -t -c /etc/nginx/nginx.conf nginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful[root@node1 conf.d]# nginx -s reload -c /etc/nginx/nginx.conf

访问测试:

403 Forbiddennginx/1.16.1

测试只允许信任的ip访问:

location ~ ^/admin.html {        root   /opt/app/code;        allow 192.168.x.0/24;  // 配置ip段方式        deny all;        index  index.html index.htm;    }

三、基于IP的访问控制局限性

Nginx基于ip的访问控制的原理是基于客户端的ip,但是对于nginx来说,他不知道哪个是真正的客户端。如果我们的访问不是客户端与服务端直接连接,而是通过了一层代理(nginx,7层负载均衡,CDN),因为http_access_module是基于remote_addr来进行识别客户端的ip。如下图:

f0fd0e82e976602c8485915d8ac7697f.png

IP1是客户端,IP3是服务端,而IP1通过IP2去访问IP3的时候,remote_addr就识别的是IP2。也就是说我们想对客户端IP1进行访问限制是没有起到作用,反而是对中间的代理IP2进行了限制。所以说他的准确率是不高的。

如何解决这个问题呢?

方法一:采用别的HTTP头信息控制访问,如:http_x_forwarded_for。

d69ce69d4aca7f7d58923b6e4aa0c22e.png

从上图可以看出:

http_x_forwarded_for=IP1,IP2http_x_forwarded_for=ClientIP,Proxy(1)IP,Proxy(1)IP,...

方法二:结合geo模块做。

方法三:通过HTTP自定义变量传递。

四、基于用户的信任登录

配置语法:

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

string

这个字符串即表示了开启,又会在前端显示出了字符串的信息,也可以作为前端的登录提示。

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

file

文件配置路径,用来作为认证存储用户名和密码信息的文件。密码文件格式:

# commentname1:password1name2:password2:commentname3:password3

需要先安装htpasswd,可以直接安装httpd-tools这个包:

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

首次需要先生成密码文件:

[root@node1 nginx]# htpasswd -c ./auth_conf duanNew password: Re-type new password: Adding password for user duan[root@node1 nginx]# more ./auth_conf duan:$apr1$yBkwo1kS$vPr2sM.EfrcNcuzqSHfz9/

五、基于用户的信任登录演示

修改配置文件:

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

重载nginx服务:

[root@node1 conf.d]# nginx -t -c /etc/nginx/nginx.conf nginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful[root@node1 conf.d]# nginx -s reload -c /etc/nginx/nginx.conf

访问测试:

http://192.168.x.xxx/admin.html // 需要输入用户名密码

六、基于用户的信任登录局限性

  • 用户信息依赖文件方式

  • 操作管理机械,效率低下

解决方式:

  • Nginx结合LUA实现高效验证

  • Nginx和LDAP打通,利用nginx-auth-ldap模块

推荐阅读

Nginx学习(五):Nginx请求限制

JVM调优小结

JVM字节码执行引擎

e5b9ca8eb943d76bf5431ee62f3292eb.png

看完本文有收获?请转发分享给更多人

关注「并发编程之美」,一起交流Java学习心得

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值