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: