Nginx访问控制

Nginx访问控制

基于IP的访问控制 - http_access_module

基于用户的信任登录 - http_auth_basic_module

#语法1(允许那些IP可以访问)

Syntax:allow address | CIDR | unix: | allk;     

Default:——

Context:http,server,limit_except

#语法2(不允许那些IP访问)

Syntax:deny address | CIDR | unix: | all;

Default:——

Context:http,server,limit_except

演示

1.配置不允许指定ip访问,其他ip都可以访问

location ~ ^/admin.html {                #自动配置到admin.html
        root   /opt/app/code;            #不允许访问ip
        deny 192.168.96.135;           #允许访问的ip,all代表所有
        allow all;
        index  index.html index.htm;
    }

测试一下

使用192.168.96.135的主机访问,如限制外网ip,百度ip就能找到对应的ip

curl http://192.168.96.188/admin.html

​访问不到报403,Forbidden代表限制

使用其他ip访问,访问成功

2.配置允许指定ip访问,其他ip都不可以访问

location ~ ^/admin.html {
        root   /opt/app/code;
        allow 192.168.96.135/24;            #允许指定ip访问,可以加上段落
        deny all;                    #其他ip都不可以访问
        index  index.html index.htm;
    }

使用192.168.96.135主机访问,可以正常访问

curl http://192.168.96.188/admin.html

使用其他ip的主机就无法访问了

3.http_access_module局限性

这种情况下,nginx做限制只会是ip2,不会对ip1起作用,准确性不是很高

4.解决方法,使用http_x_forwarded_for变量模块

.与http_access_module的区别

代理方式

5.解决http_access_module局限性

访问网页密码登录,http_auth_hasic_module模块

#语法1

Syntax:auth_basic string | off;

Default:auth_basic off;

Context:http,server,location,limit_except

#语法2

Syntax:auth_basic_user_file file;

Default:——

Context:http,server,location,limit_except

可到官方网站查看使用方法

安装httpd-tools

yum -y install httpd-tools

生成密码文件

htpasswd -c ./auth_conf joy

配置.conf文件

vi /etc/nginx/conf.d/auth_mod.conf

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;
 
    location / {
        root   /opt/app/code;
        index  index.html index.htm;
    }
	
#    location ~ ^/admin.html {
#        root   /opt/app/code;
#	deny 192.168.96.135;
#	allow all;
#        index  index.html index.htm;
#    }

    location ~ ^/admin.html {
        root   /opt/app/code;
	auth_basic "Auth access test! input your password";   # 访问前的提示信息
	auth_basic_user_file /etc/nginx/auth_conf;        # 密码文件位置
        index  index.html index.htm;
    }



    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

 

重启nginx后,浏览器访问的效果

 

http_auth_basic_module局限性

一、用户信息依赖文件方式

二、操作管理机械,效率低下

posted @ 2020-01-08 09:51  Cheney!努力才会幸运  阅读(227)  评论(0编辑  收藏  举报