nginx访问权限设置

nginx的访问控制

1、前提准备

安装nginx:

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

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

[root@server ~]# cd /usr/local/nginx/conf/
[root@server 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访问
[root@server conf]# echo 123 > ../html/index.html 
[root@server conf]# systemctl restart nginx
[root@server conf]# curl www.example.com
123
[root@server conf]# 

外面效果:

3、用户认证访问

[root@server ~]# cd /usr/local/nginx/conf/
[root@server 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";  //账户密码文件
        }
}
[root@server conf]# systemctl restart nginx
[root@server conf]# yum install -y httpd-tools
[root@server conf]# htpasswd -c -m /usr/local/nginx/conf/passwd user
New password: 123
Re-type new password: 123
Adding password for user user
[root@server conf]# systemctl restart nginx

效果:

用户:user

密码:123

posted @ 2020-06-15 22:08  -东皇太一-  阅读(674)  评论(0编辑  收藏  举报