116人参与 • 2024-09-08 • Nginx
访问控制是一种安全机制,旨在限制系统或网络资源的访问权限,确保只有经过授权的用户或系统可以访问这些资源。对访问控制的理解可能包括以下几个关键方面:
身份验证(authentication): 访问控制的第一步是确认用户或系统的身份。这通常涉及到使用用户名、密码、密钥或其他身份验证手段来验证用户的身份。合法的身份验证是访问控制的基础。
授权(authorization): 一旦用户身份得到验证,接下来的步骤是确定用户被授予的权限范围。这包括确定用户可以访问的资源、可以执行的操作以及其他相关权限。授权确保用户只能访问其被明确允许的资源和功能。
访问级别(access levels): 访问控制通常涉及定义不同的访问级别,如读取、写入、执行等。每个用户或系统被授予的访问级别取决于其角色和权限配置。精细的访问级别有助于确保安全性和数据完整性。
访问策略(access policies): 访问策略是一组规则和规范,用于定义系统中的访问控制规则。这可能包括规定谁可以访问什么资源、在什么条件下可以访问等。访问策略是实施访问控制的具体指南。
审计和监控(auditing and monitoring): 访问控制不仅仅是在用户尝试访问资源时的阻止和允许,还包括对访问活动的审计和监控。记录和分析访问日志有助于检测潜在的安全威胁或违规行为。
单点登录(single sign-on,sso): sso是一种访问控制方法,允许用户通过一次身份验证获得对多个系统的访问权限,而不需要在每个系统中单独登录。这提高了用户体验并简化了访问管理。
nginx 是一款高性能的 web 服务器,支持多种操作系统。通过 http 模块、tcp 模块、udp 模块等多种模块的支持,nginx 提供了很多灵活的访问控制配置选项。
nginx提供了2种最常用的访问控制方法
(1)基于ip的访问控制:http_access_module
可以使用 nginx 的 allow 和 deny 指令,来控制对来自特定 ip 地址的客户端的访问权限。
例: location /admin { allow 192.168.1.100; deny all; }
(2)基于用户的信任登录:http_auth_basic_module
可以使用 nginx 的 auth_basic
和 auth_basic_user_file
指令,来启用基于 http 认证的访问控制。
例: location /admin { auth_basic "restricted area"; auth_basic_user_file /path/to/password/file; }
#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或者网段
编辑/etc/nginx/conf.d/access_mod.conf
内容如下:
[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
allow 192.168.17.0/24; deny all;
,表示满足此网段的ip都可以访问。如果你想拒绝某个指定url地址的所有请求,只需要在location块中配置deny all
指令:
[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
基于用户的信任登录模块:http_auth_basic_module
有时候我们会有这么一种需求,就是你的网站的某些页面不希望对所有人公开访问,我们希望的是某些特定的客户端可以访问。
那么我们可以在访问时要求进行身份认证,就如给你自己的家门加一把锁,以拒绝那些不速之客。
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
例:
server { location /private { auth_basic "private area"; auth_basic_user_file /etc/nginx/passwords; } }
这就要求用户访问 /private 路径时输入密码才能访问。
用户密码文件也可以放在其他位置,只要 auth_basic_user_file
指令指向正确的文件即可。
[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
加载账号密码文件。
浏览器访问测试
[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
浏览器访问测试
到此这篇关于nginx 访问控制的原理及实现的文章就介绍到这了,更多相关nginx 访问控制内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论