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

  

  

nginx访问限制的更多相关文章

  1. Nginx 访问日志轮询切割

    Nginx 访问日志轮询切割脚本 #!/bin/sh Dateformat=`date +%Y%m%d` Basedir="/application/nginx" Nginxlog ...

  2. 按日期切割nginx访问日志--及性能优化

    先谈下我们需求,一个比较大的nginx访问日志,根据访问日期切割日志,保存在/tmp目录下. 测试机器为腾讯云机子,单核1G内存.测试日志大小80M. 不使用多线程版: #!/usr/bin/env ...

  3. 一、基于hadoop的nginx访问日志分析---解析日志篇

    前一阵子,搭建了ELK日志分析平台,用着挺爽的,再也不用给开发拉各种日志,节省了很多时间. 这篇博文是介绍用python代码实现日志分析的,用MRJob实现hadoop上的mapreduce,可以直接 ...

  4. Python正则表达式,统计分析nginx访问日志

    目标: 1.正则表达式 2.oop编程,统计nginx访问日志中不同IP地址出现的次数并排序 1.正则表达式 #!/usr/bin/env python # -*- coding: utf-8 -*- ...

  5. Nginx访问控制模块

    一.Nginx访问控制模块 Nginx默认安装的模块http_access_module,可以基于来源IP进行访问控制. 1.模块安装 nginx中内置ngx_http_access_module,除 ...

  6. logstash收集nginx访问日志

    logstash收集nginx访问日志 安装nginx #直接yum安装: [root@elk-node1 ~]# yum install nginx -y 官方文档:http://nginx.org ...

  7. 使用python找出nginx访问日志中访问次数最多的10个ip排序生成网页

    使用python找出nginx访问日志中访问次数最多的10个ip排序生成网页 方法1:linux下使用awk命令 # cat access1.log | awk '{print $1" &q ...

  8. nginx访问不到

    nginx访问不到 今天,一朋友的一台linux服务器上部署了nginx,但是外部(公网)就是不能访问,于是协助其排查.整体思路如下: 1.确认nginx配置是否ok. 2.确认网络是否可达. 3.是 ...

  9. Nginx 访问日志配置

    一.Nginx 访问日志介绍 Nginx 软件会把每个用户访问网站的日志信息记录到指定的日志文件里,供网站提供者分析用户的浏览行为等,此功能由 ngx_http_log_module 模块负责. 二. ...

  10. Nginx访问日志、 Nginx日志切割、静态文件不记录日志和过期时间

    1.Nginx访问日志 配制访问日志:默认定义格式: log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_loc ...

随机推荐

  1. [LeetCode] 393. UTF-8 Validation 编码验证

    A character in UTF8 can be from 1 to 4 bytes long, subjected to the following rules: For 1-byte char ...

  2. Magisk —— 安卓新一代的第三方拓展,systemless模式

    Magisk由宝岛台湾学生 topjohnwu 开发, XDA主贴:https://forum.xda-developers.com/apps/magisk 使用方法:第三方rec刷入zip 介绍: ...

  3. 单调队列优化DP(超详细!!!)

    一.概念 1.单调队列定义: 其实单调队列就是一种队列内的元素有单调性(单调递增或者单调递减)的队列,答案(也就是最优解)就存在队首,而队尾则是最后进队的元素.因为其单调性所以经常会被用来维护区间最值 ...

  4. 把 DataTable 输出到 excel 文件

    ''' <summary> ''' 把 DataTable 输出到 excel 文件 ''' </summary> ''' <param name="dt_da ...

  5. leetcode 354. 俄罗斯套娃信封问题(二维排序有关)

    题目描述 给定一些标记了宽度和高度的信封,宽度和高度以整数对形式 (w, h) 出现.当另一个信封的宽度和高度都比这个信封大的时候,这个信封就可以放进另一个信封里,如同俄罗斯套娃一样. 请计算最多能有 ...

  6. MongoDB出现The default storage engine 'wiredTiger' is not available之问题解决

    问题描述:低版本MongoDB存在该问题(版本为3.x),高版本则无该问题. 参考解决问题链接:MongoDB学习—(1)安装时出现The default storage engine ‘wiredT ...

  7. OpenShift下的JVM监控

    去年写过一篇基于jmx监控的文章,这次在Openshift上实现,发现确实不少变化.主要重点问题在 1. prometheus jmx exporter的改进,不再需要运行一个独立的进程,不需要把数据 ...

  8. 喜大普奔,又拍云全新产品 WebSocket 上线啦

    作为一款现象级 MMORPG,魔兽世界迎来了自己的 9.0 版本.巧的是,又拍云最近也更新发布了一款产品--WebSocket.你可能会说,两者有个 mao 关系,不着急.我们先来聊聊魔兽世界~ 熟悉 ...

  9. HTML5 下拉控件绑定数据

    <select id="CommunityList" class="form-control" > <option>請選擇社團</ ...

  10. 彻底搞懂B树、B+树、B*树、R 树

    出处:http://blog.csdn.net/v_JULY_v . 第一节.B树.B+树.B*树1.前言: 动态查找树主要有:二叉查找树(Binary Search Tree),平衡二叉查找树(Ba ...