VPS侦探论坛

 找回密码
 注册
查看: 4160|回复: 2

nginx: [emerg] "if" directive is not allowed here

[复制链接]
发表于 2019-9-15 19:53:46 | 显示全部楼层 |阅读模式

本帖最后由 great_andy_ 于 2019-9-15 19:59 编辑

军哥您好!

我在为虚拟机添加防爬虫代码的时候遇到一个错误提示,想请教下怎样解决。

我安装了最新的LNMP1.6,然后查看到NGINX的版本是 nginx/1.16.1

虚拟机安装成功了的,我添加防爬虫的方法如下:


# cd /usr/local/nginx/conf/vhost
# vi agent_deny.conf


然后添加代码,保存:
if ($http_user_agent ~* "qihoobot|Baiduspider|Googlebot|Googlebot-Mobile|Googlebot-Image|Mediapartners-Google|Adsbot-Google|Feedfetcher-Google|Yahoo! Slurp|Yahoo! Slurp China|YoudaoBot|Sosospider|Sogou spider|Sogou web spider|MSNBot|ia_archiver|Tomato Bot|Catall Spider|AcoiRobot")
{     
    return 403;
}

if ($http_user_agent ~ "WinHttp|WebZIP|FetchURL|node-superagent|java/|FeedDemon|Jullo|JikeSpider|Indy Library|Alexa Toolbar|AskTbFXTV|AhrefsBot|CrawlDaddy|Java|Feedly|Apache-HttpAsyncClient|UniversalFeedParser|ApacheBench|Microsoft URL Control|Swiftbot|ZmEu|oBot|jaunty|Python-urllib|lightDeckReports Bot|YYSpider|DigExt|HttpClient|MJ12bot|heritrix|EasouSpider|Ezooms|BOT/0.1|YandexBot|FlightDeckReports|Linguee Bot|iaskspider^$")
{   
    return 403;            
}

if ($request_method !~ ^(GET|HEAD|POST)$)
{   
    return 403;
}

if ($http_user_agent ~* (Python|Java|Wget|Scrapy|Curl|HttpClient|Spider))
{
    return 403;
}


最后在虚拟机的配置文件中加入上面这个 agent_deny.conf 文件:


server {
    listen       80;
    server_name  *******;
    access_log   /home/wwwlogs/access.log  main;

     ## 这个就是反爬虫文件
     include /usr/local/nginx/conf/vhost/agent_deny.conf;        
     location / {
      ........

但是我在重启LNMP的时候,提示:

Stoping nginx... nginx: [emerg] "if" directive is not allowed here in /usr/local/nginx/conf/vhost/agent_deny.conf:1
failed. Use force-quit



请问怎样添加才是正确的啊?谢谢军哥!













美国VPS推荐: 遨游主机LinodeLOCVPS主机云搬瓦工80VPSVultr美国VPS主机中国VPS推荐: 阿里云腾讯云。LNMP付费服务(代装/问题排查)QQ 503228080
发表于 2019-9-16 08:51:52 | 显示全部楼层


/usr/local/nginx/conf/vhost 目录是自动include的,仅供存放虚拟主机配置文件,你放任何其他的配置文件都会报错
Linux下Nginx+MySQL+PHP自动安装工具:https://lnmp.org
 楼主| 发表于 2019-9-16 22:36:12 | 显示全部楼层

licess 发表于 2019-9-16 08:51
/usr/local/nginx/conf/vhost 目录是自动include的,仅供存放虚拟主机配置文件,你放任何其他的配置文件都 ...

好的,谢谢军哥!
我把文件放在 /usr/local/nginx/conf 目录下就可以了!
美国VPS推荐: 遨游主机LinodeLOCVPS主机云搬瓦工80VPSVultr美国VPS主机中国VPS推荐: 阿里云腾讯云。LNMP付费服务(代装/问题排查)QQ 503228080
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|VPS侦探 ( 鲁ICP备16040043号-1 )

GMT+8, 2024-9-22 10:28 , Processed in 0.026335 second(s), 16 queries .

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表