[nginx] 对UA为空的请求返回403

nginx blocking blank user agent .
sometime apps’ backgroud request always visit a url, and these requests’ user agent is black, so I want block them.

针对一个location的nginx配置

       location / {
            if ($http_user_agent = ""){
                access_log off;
                return 403;
            }

            default_type   text/html;
            content_by_lua ' ngx.say("<h1>你好 baby </h1>") ';
        }

模拟请求

lzz@ubuntu:conf$ curl 'http://localhost'
<h1>你好 baby </h1>
lzz@ubuntu:conf$ curl -H "User-Agent:" 'http://localhost'
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>ngx_openresty/1.2.6.1</center>
</body>
</html>

声明:
本文出自 “orangleliu笔记本” 博客,转载请务必保留此出处http://blog.csdn.net/orangleliu/article/details/44861585
作者orangleliu 采用署名-非商业性使用-相同方式共享协议

你可能感兴趣的:(nginx,请求,block,UA,阻止)