Nginx forbids the method of specifying UA access
This article describes the configuration in which Nginx forbids specified UA access.
UA, or Http User Agent, uses the built-in variable $http_user_agent in Nginx to indicate that this information is sent to webserver as part of request header. Therefore, for webserver, one way to prohibit access is to judge the UA.
Prohibit search engine crawlers
Because, you know, I don't really want crawlers like Baidu to come to my website, so I use if to judge UA in Nginx:
If ($http_user_agent ~ * "qihoobot | Baidu | Baiduspider | Baiduspider-image | Baiduspider-video | Baiduspider-news | Baiduspider-favo | Baiduspider-cpro | Baiduspider-ads | Baiduboxapp | YisouSpider | EasouSpider | YodaoBot | YoudaoBot | Sosospider | Sogou | ^ $") {return 444;}
Prohibit downloading tools
If ($http_user_agent ~ * "Scrapy | HttpClient | Curl | Wget | Idm | Aria2 | Axel | Thunder | Youtube-dl | Movgrab | rtorrent | ctorrent | Transmission-cli | vuze") {return 444;}
Access to domestic browsers is prohibited
If ($http_user_agent ~ * | 360SE | 360EE | 2345Explorer | maxthon | sogou | qiyu | green | qq | qqbrowser | tt | liebao | lbbrowser | tao | taobao | coolnovo | saayaa | mi | xiaomi | baidu | yandex | wechat | weibo | douban | suning | iqiyi | alipay | ali-ap | ali-ap-pd | ali-am | ali-tb | ali-tb-pd | ali-tb-pd | ali-tb-pd ") {ali-tm 444;}
Disable access to the specified system
There are also some domestic systems, such as aliyun os, which I also want to ban:
If ($http_user_agent ~ * "yunos") {return 444;}
Conclusion
As you can see from the above, to judge UA, the rule is:
If ($http_user_agent ~ * "UA keyword") {.}
However, the disadvantage of this method is obvious, because UA is too easy to forge, but it is OK to guard against requests without forgery (such as domestic search crawlers). Of course, there are some more reliable ways, such as the verification of session.
The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.