nginx的访问控制(一)

本文详细介绍了如何在Nginx环境中设置目录访问权限,通过实例展示了如何使用黑白名单模式控制不同IP对特定目录的访问,并演示了如何配置允许特定服务器和宿主机浏览器访问。
摘要由CSDN通过智能技术生成

湖蓝几何球体LinkedIn Banner.png
转载说明:如果您喜欢这篇文章并打算转载它,请私信作者取得授权。感谢您喜爱本文,请文明转载,谢谢。


nginx的目录可以控制访问权限,配置有点类似上一篇文章《ssh黑白名单的那些事儿》里面的黑白名单。

基础环境准备:

1、硬件环境准备(vmware虚拟机)

2、nginx安装

这里是编译安装的,nginx源码包下载链接:http://nginx.org/en/download.html
2.1 安装依赖包:

yum -y install gcc gcc++ gcc-c++ pcre pcre-devel openssl-devel zlib zlib-devel gd gd-devel automake zlip openssl

2.2 创建nginx用户

useradd -s /sbin/nologin -M nginx

2.3 上传nginx安装包到服务器并解压到/usr/local目录下,开始编译:

cd /usr/local/nginx-1.18.0
./configure --prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-pcre \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-http_image_filter_module \
--with-http_slice_module \
--with-mail \
--with-threads \
--with-file-aio \
--with-stream \
--with-mail_ssl_module \
--with-stream_ssl_module \

然后执行make和make install完成编译安装

make
make install

2.4 启动nginx:

cd /usr/local/nginx/sbin
./nginx

查看进程:

[root@test101 ~]# ps -ef|grep nginx
root      17365      1  0 22:50 ?        00:00:00 nginx: master process ./nginx
nginx     17366  17365  0 22:50 ?        00:00:00 nginx: worker process
root      43788   2202  0 23:32 pts/0    00:00:00 grep --color=auto nginx
[root@test101 ~]#

访问nginx首页,验证搭建成功:
在这里插入图片描述

配置nginx访问控制

1、准备测试目录:nginx.control

创建nginx.control工程目录,并写一个index.html放在该目录下:

[root@test101 ~]# mkdir /usr/local/nginx/html/nginx.control -p
[root@test101 nginx.control]# ll /usr/local/nginx/html/nginx.control/
total 12
-rw-r--r--. 1 root root  276 Jun 20 15:26 index.html
-rw-r--r--. 1 root root 8012 Jun 20 15:03 bgx.jpg   #这个bgx.jpg是index.html插入的一张图片
[root@test101 nginx.controll]#

在浏览器访问http://10.0.0.101/nginx.control/, 就能看到我们自己写的这个html内容了:
在这里插入图片描述

2、配置访问控制

编辑nginx.conf,在location / 模块后面加入访问控制的配置内容:

location /nginx.control {
    allow 10.0.0.101; #放开test101本机内网IP:10.0.0.101
    deny all;
}

添加位置如下图:
在这里插入图片描述

然后重启nginx或者重新加载配置文件:

[root@test101 conf]# killall -s HUP nginx  #如果killall命令不存在,可自行安装:yum install psmisc -y

如上的配置,结果是test101本机能curl访问http://10.0.0.101/nginx.control:
在这里插入图片描述

但是test102和浏览器都不能访问:
在这里插入图片描述

在test102服务器上curl访问,也是403:
在这里插入图片描述

如果想要让test102和浏览器能访问,就要在nginx.conf添加相应的权限:

location /nginx.control {
    allow 10.0.0.101;   #放开test101本机访问权限
    allow 10.0.0.102;   #放开test102服务器访问权限
    allow 10.0.0.1;     #放开宿主机浏览器访问廯
    deny all;     #除了以上三个IP,其余全部阻止访问
}

验证:
test101服务器访问正常:
在这里插入图片描述

test102服务器访问正常:
在这里插入图片描述

浏览器访问也正常,背锅侠正常出现:
在这里插入图片描述

ok,配置和验证完成。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值