Home  >  Article  >  Backend Development  >  [nginx] Return 403 for request with empty UA

[nginx] Return 403 for request with empty UA

WBOY
WBOYOriginal
2016-08-08 09:26:451486browse

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

nginx configuration for one location

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

            default_<span>type</span>   text/html;
            content_by_lua <span>'
                ngx.say("<h1>你好 baby </h1>")
            '</span>;
        }</code>

Simulated request

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

Statement:
This article comes from the "orangleliu Notebook" blog. Please be sure to keep this source for reprinting http://blog.csdn.net/orangleliu/article/details/44861585
Author orangleliu adopts Attribution-NonCommercial-ShareAlike License

The above introduces [nginx] returning 403 for requests with empty UA, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn