nginx的访问控制

1.http_access_module   基于ip的访问控制

允许的访问配置

不允许的访问配置

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

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

    location ~ ^/admin.html {
        root   /opt/app/code;
        deny 222.128.189.17;(限制ip)
        allow all;(允许其他所有ip)
        index  index.html index.htm;
    }

    location ~ ^/admin.html {
        root   /opt/app/code;
        allow 222.128.189.0/24;  (允许访问ip)
        deny all;  (不允许访问)
        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 404  /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;
    #}
}

利用x_forwarded_for进行访问限制

location / {
        if ( $http_x_forwarded_for !~* "^116\.62\.103\.228"(不是这个ip的返回403)) {
            return 403;
        }
        root   /opt/app/code;
        index  index.html index.htm;

2.http_auth_basic_module   基于用户的信任登入

存储用户信息文件

1.安装httpd-tools

yum -y install httpd-tools

2.设置认证账号密码

htpasswd -c ./auth_conf(文件名称) yoyo(账号)
根据提示输入密码

3.配置文件

location ~(匹配文件) ^/admin.html {
        root   /opt/app/code;
        auth_basic 'auth access test! input you password!';
        auth_basic_user_file /etc/nginx/auth_conf;
        index  index.html index.htm;
    }

4.查看语法是否正确

nginx -t  -c /etc/nginx/nginx.conf

5.重启配置

nginx -s reload -c /etc/nginx/nginx.conf

  

  

最新文章

  1. WPF 自定义ListBox
  2. xcode8 上传ipa文件无法构建版本
  3. KMP 算法
  4. BZOJ2981 : [Poi2002]括号
  5. 缓存插件 Spring支持EHCache缓存
  6. centOS6.5安装SUN-jdk7
  7. [ActionScript 3.0] AS3动态改变注册点
  8. ecshop 改变sitemap.xml的位置
  9. APUE学习心得
  10. Python简要学习笔记
  11. js中的事件缓存机制
  12. 简单介绍什么是协程及其在ES6中的实现方式
  13. Flask基础
  14. ArcticCore重构-问题列表1
  15. CF1155F Delivery Oligopoly
  16. 浅谈百度地图API的坑
  17. xampp运行MySQL shutdown unexpectedly解决方案
  18. python后端将svc文件数据读入数据库具体实现
  19. vue-awesome-swiper轮播的使用
  20. 使用github oauth 出现 OpenSSL::SSL::SSLError - SSL_connect SYSCALL returned=5 errno=0 state=SSLv2/v3 解决

热门文章

  1. Webpack 一些核心概念了解
  2. 可以在shell脚本中使用哪些类型的变量?
  3. Visual Studio 调试系列8 查找导致程序崩溃的 DLL(使用模块窗口)
  4. mysql Duplicate entry '9223372036854775807' for key 'PRIMARY'
  5. for...in 、for...of 、forEach 的区别
  6. 【2019年07月08日】A股最便宜的股票
  7. HTML连载30-CSS显示模式&模式转换
  8. python笔记 面向对象编程从入门到高级
  9. Java8 新特性 Stream() 创建流
  10. SQL --------------- between 和< >