您当前的位置: 首页 >  centos

小志的博客

暂无认证

  • 2浏览

    0关注

    1217博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Nginx——nginx的访问控制详解一(Centos7通过yum方式安装)

小志的博客 发布时间:2021-07-12 22:41:48 ,浏览量:2

目录
    • 一、实现访问控制的基本方式
    • 二、基于IP的访问控制语法(即http_access_module模块语法)
    • 三、http_access_module模块中的配置演示
      • 1、配置nginx访问html页面的路径
      • 2、配置nginx不允许本机ip访问html页面
      • 3、配置nginx允许本机ip访问html页面
    • 四、http_access_module的局限性(使用的是remote_addr变量)
    • 五、http_access_module的局限性解决方式(使用http_x_forwarded_for变量)
      • 1、http_x_forwarded_for的理解
      • 2、nginx的remote_addr控制访问和HTTP头信息控制访问http_x_forwarded_for区别
      • 3、如何解决http_access_module的局限性

一、实现访问控制的基本方式 模块作用http_access_module基于IP的访问控制(允许或者不允许某些ip访问)http_auth_basic_module基于用户的信任登录(提供登录认证的方式控制访问范围) 二、基于IP的访问控制语法(即http_access_module模块语法)

1、允许访问的控制语法

  • Syntax:allow address | CIDR | unix: | all ;allow address表示允许哪一个ip访问,allow CIDR 表示允许哪个网段可以访问,allow unix: 表示在linux上用到的socket的方式访问,allow all表示允许所有的访问
  • Default:—— 表示默认没有配置
  • Context:http,server,location,limit_except 表示需要在http块、或server块、或location块、limit_except块中进行配置

2、不允许访问的控制语法

  • Syntax:deny address | CIDR | unix: | all ;allow address表示不允许哪一个ip访问,allow CIDR 表示不允许哪个网段可以访问,allow unix: 表示在linux上用到的socket的方式访问,allow all表示不允许所有的访问
  • Default:—— 表示默认没有配置
  • Context:http,server,location,limit_except 表示需要在http块、或server块、或location块、limit_except块中进行配置
三、http_access_module模块中的配置演示 1、配置nginx访问html页面的路径
  • 创建title.html页面,上传到/opt/app/html目录下

    
    	
    		
    		titleone
    	
    	
    		标题
    	
    
    
  • 编辑nginx.conf配置文件,在http块的server块中配置访问/opt/app/html目录下的所有html页面,如下内容

    location / {
        root /opt/app/html;
        index index.html index.htm;
    }
    

    在这里插入图片描述

  • 启动nginx服务

    [root@localhost nginx]# systemctl start nginx.service
    [root@localhost nginx]# 
    

    在这里插入图片描述

  • 检查配置修改的配置文件是否正确,返回successful表示配置文件修改无错。

    [root@localhost nginx]# nginx -t -c /etc/nginx/nginx.conf
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful
    [root@localhost nginx]# 
    

    在这里插入图片描述

  • 重新加载配置文件

    [root@localhost /]# systemctl reload nginx
    [root@localhost /]# 
    

    在这里插入图片描述

  • 虚拟机中的浏览器访问如下图

    在这里插入图片描述

2、配置nginx不允许本机ip访问html页面
  • 编辑nginx.conf配置文件,在http块的server块中配置不允许本机ip访问并配置允许所有访问,添加如下内容

    #配置nginx不允许本机ip访问并允许所有访问
    location ~ ^/title.html {#配置跟目录下的访问路径
             root /opt/app/html;
             deny 192.168.3.10;
             allow all;
             index index.html index.htm;
    }
    

    在这里插入图片描述

  • 检查配置修改的配置文件是否正确,返回successful表示配置文件修改无错。

    [root@localhost nginx]# nginx -t -c /etc/nginx/nginx.conf
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful
    [root@localhost nginx]# 
    

    在这里插入图片描述

  • 重新加载配置文件

    [root@localhost /]# systemctl reload nginx
    [root@localhost /]# 
    

    在这里插入图片描述

  • 在浏览器中使用本机ip访问,查看是否可以访问,如下图说明本机ip不可访问,配置生效。

    在这里插入图片描述

3、配置nginx允许本机ip访问html页面
  • 编辑nginx.conf配置文件,在http块的server块中配置允许本机ip访问并配置不允许所有访问,添加如下内容

    #配置nginx允许本机ip访问并不允许所有访问
    location ~ ^/title.html {#配置跟目录下的访问路径
             root /opt/app/html;
             allow 192.168.3.10;
             deny all;
             index index.html index.htm;
    }
    

    在这里插入图片描述

  • 检查配置修改的配置文件是否正确,返回successful表示配置文件修改无错。

    [root@localhost nginx]# nginx -t -c /etc/nginx/nginx.conf
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful
    [root@localhost nginx]# 
    

    在这里插入图片描述

  • 重新加载配置文件

    [root@localhost /]# systemctl reload nginx
    [root@localhost /]# 
    

    在这里插入图片描述

  • 在浏览器中使用本机ip访问,查看是否可以访问,如下图说明本机ip可访问,配置生效。

    在这里插入图片描述

四、http_access_module的局限性(使用的是remote_addr变量)

在这里插入图片描述

  • 如上图,IP1是客户端,IP2是中间层代理,IP3是服务端;
  • 如上图,如果访问不是客户端与服务端直接连接,而是通过一层代理。【包括nginx代理、7lay LSB(7层的负载均衡)、CDN代理】,nginx是通过remote_addr = IP2这个变量来识别客户端的ip;
  • 只能通过$remote_addr控制信任;
  • 所以如果对客户端ip1做限制的话,就不会起作用而是对中间层的代理做的限制;
五、http_access_module的局限性解决方式(使用http_x_forwarded_for变量) 1、http_x_forwarded_for的理解
  • http_x_forwarded_for是nginx中http头变量中常用的变量
2、nginx的remote_addr控制访问和HTTP头信息控制访问http_x_forwarded_for区别

在这里插入图片描述

  • 如上图:nginx的控制访问是通过remote_addr = IP2这个变量来识别客户端的ip,即识别的代理服务的ip;
  • 如上图:HTTP头信息控制访问x_forwarded_for = IP1,IP2这个变量来识别客户端的ip,即识别的是客户端的ip1和代理服务的IP2;
3、如何解决http_access_module的局限性
  • 采用别的HTTP头信息控制访问,如http_x_forwarded_for;
  • 结合geo模块;
  • 通过HTTP自定义变量传递;
关注
打赏
1661269038
查看更多评论
立即登录/注册

微信扫码登录

0.2765s