首页 > 其他 > 详细

nginx限制ip访问(转)

时间:2015-06-15 12:48:52      阅读:180      评论:0      收藏:0      [点我收藏+]

一、服务器全局限IP

#vi nginx.conf

    allow 10.57.22.172;  #允许的IP
    deny all;

 

二、站点限IP

#vi vhosts.conf

站点全局限IP:
location / {
    index  index.html index.htm index.php;
    allow 10.57.22.172;
    deny all;

    location ~ \.php$ {
        if ( $fastcgi_script_name ~ \..*\/.*php ) {return 403;}
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/htdocs$fastcgi_script_name;
        include fastcgi_params;
    }
}

站点IP限目录:
location / {
    index  index.html index.htm index.php;
}

location ^~ /test/ {
    allow 10.57.22.172;
    deny all;

    if ( $fastcgi_script_name ~ \..*\/.*php ) {return 403;}
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /var/www/htdocs$fastcgi_script_name;
    include fastcgi_params;
}

location ~ \.php$ {
    if ( $fastcgi_script_name ~ \..*\/.*php ) {return 403;}
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /var/www/htdocs$fastcgi_script_name;
    include fastcgi_params;
}

 

注意事项:

  1. deny 一定要加一个ip,否则直接跳转到403,不往下执行了;如果403默认页是同一域名下,会造成死循环访问;

  2. allow的ip段

  从允许访问的段位从小到大排列,如127.0.0.0/24 下面才能是10.10.0.0/16

  24表示子网掩码:255.255.255.0

  16表示子网掩码:255.255.0.0

  8表示子网掩码:255.0.0.0

  3. deny all;结尾 表示除了上面allow的其他都禁止

如:

           deny 192.168.1.1;
           allow 127.0.0.0/24;
           allow 192.168.0.0/16;
           allow 10.10.0.0/16;
           deny all;

nginx限制ip访问(转)

原文:http://www.cnblogs.com/qq78292959/p/4576793.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!