Nginx禁止指定user_agent

[root@daixuan vhosts]# vim test.conf

server

{

    listen 80;

    server_name www.test.com www.aaa.com www.bbb.com;

    if ($host != 'www.test.com'){

        rewrite ^/(.*)$ http://www.test.com/$1 permanent;

    }

    index index.html index.htm index.php;

    root /data/www;

    access_log /tmp/access.log daixuan;

    allow 192.168.101.0/24;

    deny all;

    if ($http_user_agent ~ 'curl|baidu|111111')

    {

        return 403;

    }

}


[root@daixuan vhosts]# /usr/local/nginx/sbin/nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

[root@daixuan vhosts]# /etc/init.d/nginx reload

重新载入 Nginx:      

[root@daixuan vhosts]# curl -A "daixuanbaidu" -x192.168.101.230:80 www.test.com -I

HTTP/1.1 403 Forbidden

Server: nginx/1.8.0

Date: Wed, 13 Jan 2016 08:20:23 GMT

Content-Type: text/html

Content-Length: 168

Connection: keep-alive


~* 不区分大小写匹配,BAIDU和baidu

    if ($http_user_agent ~* 'curl|baidu|111111')

    {

        return 403;

    }