nginx 基于IP和用户的访问

nginx的下载

复制代码
yum install nginx.x86_64 -y

启动服务

复制代码
systemctl enable --now nginx.service

查看服务目录

复制代码
[root@webserver ~]# rpm -ql nginx
/usr/bin/nginx-upgrade
/usr/lib/systemd/system/nginx.service
/usr/share/man/man3/nginx.3pm.gz
/usr/share/man/man8/nginx-upgrade.8.gz
/usr/share/man/man8/nginx.8.gz
/usr/share/nginx/html/404.html
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/icons
/usr/share/nginx/html/icons/poweredby.png
/usr/share/nginx/html/index.html
/usr/share/nginx/html/nginx-logo.png
/usr/share/nginx/html/poweredby.png
/usr/share/nginx/html/system_noindex_logo.png
/usr/share/vim/vimfiles/ftdetect/nginx.vim
/usr/share/vim/vimfiles/ftplugin/nginx.vim
/usr/share/vim/vimfiles/indent/nginx.vim
/usr/share/vim/vimfiles/syntax/nginx.vim

修改默认发布

复制代码
echo 172.25.254.100 > /usr/share/nginx/html/index.html

结果

基于IP的访问限制

复制代码
vim /etc/nginx/nginx.conf
 server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        #root         /usr/share/nginx/html;
​
        root         /var/www/html;
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
​
        error_page 404 /404.html;
        location = /404.html {
        }
​
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
        location = /timinglee/ {
                allow 172.25.254.100;
                deny all;
        }
    }

allow 172.25.254.100;

deny all;只允许172.25.254.100访问

结果

100的主机

复制代码
[root@webserver ~]# curl 172.25.254.100/timinglee/
timinglee

200的主机

复制代码
[root@server200 ~]# curl 172.25.254.100/timinglee/
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>

基于用户的访问

上面的实验做完,恢复环境

复制代码
[root@webserver ~]# mkdir -p /usr/share/nginx/html/timinglee
[root@webserver ~]# echo timinglee > /usr/share/nginx/html/timinglee/index.html
[root@webserver ~]# curl 172.25.254.100/timinglee/
timinglee
建立隐藏的认证文件,以.开头
下载所需的工具
[root@webserver ~]# dnf install httpd-tools -y
创建所需的认证文件
[root@webserver ~]# htpasswd -cm /etc/nginx/.htpasswd admin
New password:
Re-type new password:
Adding password for user admin
这里的用户并不是系统真实的用户
[root@webserver ~]# cat /etc/nginx/.htpasswd
admin:$apr1$.BwhiHQk$uSJKKScJdh6T.XQTnRxmX.
第二次创建
[root@webserver ~]# htpasswd -m /etc/nginx/.htpasswd timinglee
New password:
Re-type new password:
Adding password for user timinglee
[root@webserver ~]# cat /etc/nginx/.htpasswd
admin:$apr1$.BwhiHQk$uSJKKScJdh6T.XQTnRxmX.
timinglee:$apr1$b9AI5qvA$seGe4elxYymRfVTRwob0a1

打开主配置文件

复制代码
[root@webserver ~]# vim /etc/nginx/nginx.conf
server {
    listen       80;
    listen       [::]:80;
    server_name  _;
    root         /usr/share/nginx/html;
​
    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;
​
    error_page 404 /404.html;
    location = /404.html {
    }
​
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
    }
    location /timinglee/ {
            auth_basic on;
            auth_basic_user_file "/etc/nginx/.htpasswd";
    }
}
进行重启
[root@webserver ~]# systemctl restart nginx.service

验证

复制代码
[root@webserver ~]# curl 172.25.254.100/timinglee/ -uadmin:123456
timinglee

俩种验证方式任选择

相关推荐
半梦半醒*2 分钟前
Linux网络管理
linux·运维·网络·centos·运维开发
破刺不会编程10 分钟前
linux线程概念和控制
linux·运维·服务器·开发语言·c++
神秘人X70726 分钟前
Linux网络配置全攻略:IP、路由与双机通信
linux·网络·tcp/ip
翟天保Steven1 小时前
Ubuntu-安装Epics教程
linux·ubuntu·epics
Nightwish51 小时前
Linux随记(二十一)
linux·运维·服务器
獭.獭.3 小时前
Linux -- 文件【上】
linux·运维·服务器·进程·pcb
Johny_Zhao5 小时前
Centos8搭建hadoop高可用集群
linux·hadoop·python·网络安全·信息安全·云计算·shell·yum源·系统运维·itsm
杜子不疼.6 小时前
Linux的生态与软件安装
linux·运维·服务器
Tipriest_7 小时前
离线进行apt安装的过程(在只能本地传输的ubuntu主机上使用apt安装)
linux·运维·ubuntu·apt·install·deb
QMCY_jason7 小时前
Ubuntu 1804 编译ffmpeg qsv MediaSDK libva 遇到的问题记录
linux·ubuntu·ffmpeg