nginx 禁止某些客户端ip访问

1. 在   /etc/nginx/nginx.conf  配置文件的server模块里添加匹配  ( 和location同一级 )

       if ($remote_addr !~ ^(192.168.0.*|127.0.0.1)) {
         rewrite ^.*$ /errorPage/upgrade.html last;
        }

用这种方法, 除去上面包含的ip外的所有客户端的请求都不可以. 包括 POST请求.

2. 在   /etc/nginx/nginx.conf  配置文件的server里location里做限制

    server {
       listen 80;
       server_name tty.bszhihui.com;
       location / {
                allow 111.198.29.223;
                deny all;
            root   /data/wu/web/;
            index  default.php  index.php index.html;
                }

        error_page  404 403     /error.html;

        location = /error.html {
        }
        }

用这种方法, 除了允许的ip可以,拒绝的ip不可以, 规则是从上往下. 但是禁止的 GET请求, POST 不限制. 

猜你喜欢

转载自www.cnblogs.com/mingetty/p/10883888.html