nginx访问权限设置

nginx的访问控制

1、前提准备

安装nginx:

https://www.cnblogs.com/liuzhijun666/p/13124033.html

2、根据Ip来限制主机访问

[ ~]# cd /usr/local/nginx/conf/
[ conf]# vim nginx.conf
  server {
        listen       80;	#端口号
        server_name  www.example.com;	#设置访问域名
        location / {
            root   html;				#文件位置默认html
            index  index.html index.htm;
            allow 192.168.7.10/32;  #允许访问的Ip(这是本机Ip),或者网段
           deny all;			#deny禁止,all所有
        }
          
       }
       
       
 #本机ip访问
[ conf]# echo 123 > ../html/index.html 
[ conf]# systemctl restart nginx
[ conf]# curl www.example.com
123
[ conf]#

外面效果:

nginx访问权限设置

3、用户认证访问

[ ~]# cd /usr/local/nginx/conf/
[ conf]# vim nginx.conf
server {
        listen       80;
        server_name  www.example.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
            auth_basic "欢迎信息";
            auth_basic_user_file "/usr/local/nginx/conf/passwd";  //账户密码文件
        }
}
[ conf]# systemctl restart nginx
[ conf]# yum install -y httpd-tools
[ conf]# htpasswd -c -m /usr/local/nginx/conf/passwd user
New password: 123
Re-type new password: 123
Adding password for user user
[ conf]# systemctl restart nginx

效果:

用户:user

密码:123

nginx访问权限设置

nginx访问权限设置

相关推荐