如何在 nginx 配置中阻止特定的用户代理 [英] How to block a specific user agent in nginx config

查看:74
本文介绍了如何在 nginx 配置中阻止特定的用户代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 nginx 阻止用户代理.到目前为止,我有这样的事情:

How do I block a user agent using nginx. so far I have something like this:

if ($http_user_agent = "Mozilla/5.0 (Linux; Android 4.2.2; SGH-M919 Build/JDQ39) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.169 Mobile Safari/537.22") {
return 403;}

这是来自关于此堆栈溢出的类似线程.

this is from a similar thread on this stack overflow.

我运行 nginx 作为cherrypy 服务器的反向代理.我打算单独使用 nginx 过滤某个用户代理,但上面的代码在我的服务器上不起作用.

I run nginx as a reverse proxy for cherrypy server. I intend to filter a certain user agent using nginx alone but the above code doesn't work on my server.

这是正确的方法吗?它没有包含在 nginx 配置的任何块中.我应该将它添加到http"块还是server"块

is that the correct way to do this? It wasn't included in any block in the nginx config. Should I add it to the "http" block or the "server" block

推荐答案

为了阻止特定的用户代理,我将此代码包含在服务器"块中:

in order to block the specific user agent I included this code in the "server" block:

if ($http_user_agent = "Mozilla/5.0 (Linux; Android 4.2.2; SGH-M919 Build/JDQ39) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.169 Mobile Safari/537.22"){
    return 403;
}

它按预期工作.

这篇关于如何在 nginx 配置中阻止特定的用户代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆