linux
主页 > 服务器 > linux >

Nginx访问控制的原理及实现

2024-07-08 | 佚名 | 点击:

什么是访问控制?

访问控制是一种安全机制,旨在限制系统或网络资源的访问权限,确保只有经过授权的用户或系统可以访问这些资源。对访问控制的理解可能包括以下几个关键方面:

身份验证(Authentication): 访问控制的第一步是确认用户或系统的身份。这通常涉及到使用用户名、密码、密钥或其他身份验证手段来验证用户的身份。合法的身份验证是访问控制的基础。

授权(Authorization): 一旦用户身份得到验证,接下来的步骤是确定用户被授予的权限范围。这包括确定用户可以访问的资源、可以执行的操作以及其他相关权限。授权确保用户只能访问其被明确允许的资源和功能。

访问级别(Access Levels): 访问控制通常涉及定义不同的访问级别,如读取、写入、执行等。每个用户或系统被授予的访问级别取决于其角色和权限配置。精细的访问级别有助于确保安全性和数据完整性。

访问策略(Access Policies): 访问策略是一组规则和规范,用于定义系统中的访问控制规则。这可能包括规定谁可以访问什么资源、在什么条件下可以访问等。访问策略是实施访问控制的具体指南。

审计和监控(Auditing and Monitoring): 访问控制不仅仅是在用户尝试访问资源时的阻止和允许,还包括对访问活动的审计和监控。记录和分析访问日志有助于检测潜在的安全威胁或违规行为。

单点登录(Single Sign-On,SSO): SSO是一种访问控制方法,允许用户通过一次身份验证获得对多个系统的访问权限,而不需要在每个系统中单独登录。这提高了用户体验并简化了访问管理。

1. Nginx 访问控制模块

Nginx 是一款高性能的 Web 服务器,支持多种操作系统。通过 HTTP 模块、TCP 模块、UDP 模块等多种模块的支持,Nginx 提供了很多灵活的访问控制配置选项。
Nginx提供了2种最常用的访问控制方法
(1)基于IP的访问控制:http_access_module可以使用 Nginx 的 allow 和 deny 指令,来控制对来自特定 IP 地址的客户端的访问权限。

1

2

3

4

5

例:

location /admin {

    allow 192.168.1.100;

    deny all;

}

(2)基于用户的信任登录:http_auth_basic_module可以使用 Nginx 的 auth_basic 和 auth_basic_user_file 指令,来启用基于 HTTP 认证的访问控制。

1

2

3

4

5

例:

location /admin {

    auth_basic "Restricted Area";

    auth_basic_user_file /path/to/password/file;

}

2. 基于 IP 的访问控制

2.1. 配置语法

1

2

3

4

5

6

7

8

9

10

11

12

#allow允许IP

Syntax:allow address | all;

default:默认无

Context:http,server,location

?

#deny拒绝IP

Syntax:deny address | all;

default:默认无

Context:http,server,location

=======================================================================

allow    允许    ip或者网段

deny    拒绝     ip或者网段

2.2. allow 允许配置实验

编辑/etc/nginx/conf.d/access_mod.conf内容如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

[root@localhost ~]# hostname -I

192.168.221.138

?

[root@localhost ~]# vim /etc/nginx/conf.d/allow.conf

server {

        listen 80;

        server_name localhost;  #注意域名不要有冲突

        location  / {

                root /usr/share/nginx/html;

                index index.html index.hml;

                deny 192.168.221.136;    #不允许136访问

                allow all;

                }

}

?

[root@localhost conf.d]# nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@localhost conf.d]# systemctl restart nginx

?

//136访问测试:

[root@localhost ~]# hostname -I

192.168.221.136

[root@localhost ~]# curl  -I http://192.168.221.138

HTTP/1.1 403 Forbidden  //403访问被拒绝

Server: nginx/1.24.0

Date: Sun, 30 Jul 2023 23:24:08 GMT

Content-Type: text/html

Content-Length: 153

Connection: keep-alive

?

//130访问测试:

[root@localhost ~]# hostname -I

192.168.221.130

[root@localhost ~]# curl  -I http://192.168.221.138

HTTP/1.1 200 OK         //200访问ok

Server: nginx/1.24.0

Date: Sun, 30 Jul 2023 23:24:28 GMT

Content-Type: text/html

Content-Length: 615

Last-Modified: Tue, 11 Apr 2023 17:22:34 GMT

Connection: keep-alive

ETag: "6435975a-267"

Accept-Ranges: bytes

?需要注意:
1.按顺序匹配,已经被匹配的ip或者网段,后面不再被匹配。
2.如果先允许所有ip访问,在定义拒绝访问。那么拒绝访问不生效。
3.默认为allow all

2.3. deny 拒绝配置实验

如果你想拒绝某个指定URL地址的所有请求,只需要在location块中配置deny all指令:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

[root@localhost conf.d]# hostname -I

192.168.221.138

 

[root@localhost conf.d]# vim /etc/nginx/conf.d/deny.conf

server {

        listen 80;

        server_name localhost;

        location  / {

                root /usr/share/nginx/html;

                index index.html index.hml;

                allow 192.168.221.136;    #只允许136可以访问

                deny all;    #拒绝所有

                }

}

 

[root@localhost conf.d]# nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@localhost conf.d]# systemctl restart nginx

 

//136访问测试

[root@localhost ~]# hostname -I

192.168.221.136

[root@localhost ~]# curl  -I http://192.168.221.138

HTTP/1.1 200 OK

Server: nginx/1.24.0

Date: Sun, 30 Jul 2023 23:38:18 GMT

Content-Type: text/html

Content-Length: 615

Last-Modified: Tue, 11 Apr 2023 17:22:34 GMT

Connection: keep-alive

ETag: "6435975a-267"

Accept-Ranges: bytes

 

//130访问测试

[root@localhost ~]# hostname -I

192.168.221.130

[root@localhost ~]# curl  -I http://192.168.221.138

HTTP/1.1 403 Forbidden

Server: nginx/1.24.0

Date: Mon, 31 Jul 2023 00:51:10 GMT

Content-Type: text/html

Content-Length: 153

Connection: keep-alive

 

//windows宿主机curl测试

C:\Users\TZH>curl -I http://192.168.221.138

HTTP/1.1 403 Forbidden

Server: nginx/1.24.0

Date: Mon, 31 Jul 2023 00:51:49 GMT

Content-Type: text/html

Content-Length: 153

Connection: keep-alive

3. 基于用户的信任登录

基于用户的信任登录模块:http_auth_basic_module有时候我们会有这么一种需求,就是你的网站的某些页面不希望对所有人公开访问,我们希望的是某些特定的客户端可以访问。
那么我们可以在访问时要求进行身份认证,就如给你自己的家门加一把锁,以拒绝那些不速之客。

3.1. 配置语法

Syntax:auth_basic string | off;
default:auth_basic off;
Context:http,server,location
auth_basic string; 设置基本验证的描述信息

Syntax:auth_basic_user_file file;
default:默认无
Context:http,server,location
file:设置存储用户名密码信息的文件。
密码文件格式 username:password

例:

1

2

3

4

5

6

server {

  location /private {

    auth_basic "Private area";

    auth_basic_user_file /etc/nginx/passwords;

  }

}

这就要求用户访问 /private 路径时输入密码才能访问。
用户密码文件也可以放在其他位置,只要 auth_basic_user_file 指令指向正确的文件即可。

3.2. 配置示例

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

[root@localhost conf.d]# hostname -I

192.168.221.138

 

[root@localhost ~]# vim /etc/nginx/conf.d/auth_mod.conf

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@localhost conf.d]# mkdir /var/www/html/admin    //创建目录

[root@localhost conf.d]# echo "auth..."  > /var/www/html/index.html/admin    //创建文件

 

[root@localhost conf.d]# nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@localhost conf.d]# systemctl restart nginx

 

//做好本地windows的host文件解析

192.168.221.138 www.Jltauth.com

auth_basic不为 off 时,开启登录验证功能,auth_basic_user_file 加载账号密码文件。
浏览器访问测试

3.3. 创建登录的口令文件

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

[root@localhost ~]# yum install -y httpd-tools

//htpasswd 是开源 http 服务器 apache httpd 的一个命令工具,用于生成 http 基本认证的密码文件

 

[root@localhost conf.d]# htpasswd -cm /etc/nginx/auth_conf jack

New password:

Re-type new password:

Adding password for user jack

//jack:用户

//-c: 创建一个新的密码文件,如果密码文件已存在,则会直接覆盖。

//-m: 在已有的密码文件中添加用户,不会覆盖已有用户。

//该命令中,会创建密码文件 auth_conf,并写入用户 jack 的密码记录

 

[root@localhost conf.d]# htpasswd -m /etc/nginx/auth_conf tom

New password:

Re-type new password:

Adding password for user tom

//该命令中,会在已有的密码文件 auth_conf 中追加用户 tom 的密码记录

 

[root@localhost nginx]# cat /etc/nginx/auth_conf

jack:$apr1$YmpHMEkH$OtjswnIL5F.E7HUGKBi6U/

tom:$apr1$S8Q.Csg.$JQ6hQSExltiB9x/vlQURb0

浏览器访问测试

原文链接:
相关文章
最新更新