Nginx 未知指令“如果”

Posted

技术标签:

【中文标题】Nginx 未知指令“如果”【英文标题】:Nginx unknown directive "if" 【发布时间】:2017-07-28 18:18:49 【问题描述】:

在我的 nginx 服务器上,为了节省时间,我制作了 etc/nginx/include.conf 并将这一行放在 etc/nginx/sites-available/site1.conf 中:

location / 
    include /etc/nginx/include.conf;
    try_files $uri $uri/ /index.php?page=$uri;

include.conf 的内容:

if ($http_referer ~* (badreferers))  
        return 403; 

在测试conf文件的时候,出现这个错误: [emerg] /etc/nginx/include.conf:1 中的未知指令“if”

当我将if语句直接放在etc/nginx/sites-available/site1.conf中时,它不会报错。

这里有什么问题?

更新: nginx -V 给:

nginx版本:由gcc 4.8.4(Ubuntu)构建的nginx/1.4.6(Ubuntu) 4.8.4-2ubuntu1~14.04.3) 启用 TLS SNI 支持的配置参数:--with-cc-opt='-g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror= format-security -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro' --prefix=/usr/share/nginx --conf-path=/etc /nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var /lock/nginx.lock --pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var /lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp- path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_addition_module --with -http_dav_module --with-http_geoip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_spdy_module --with-http_sub_module --with-http_xslt_mod ule --with-mail --with-mail_ssl_module

【问题讨论】:

它可能对空格敏感,因此请尝试删除多余的空格。 if ($http_referer ~* ...) 嗨,感谢您的建议,测试时不会改变问题。 @C.A.Vuyk 你检查了***.com/questions/19165976/… 这个线程的坏字符解决方案吗? 是的,为了确定,我什至重新输入了整个声明 @C.A.Vuyk 你是自己编译 nginx,还是来自 OS 发行版/repo 或其他一些现成的编译源?请更新您的问题并包含nginx -V 的输出。 【参考方案1】:

您使用旧的 nginx 版本 1.4。在现代 1.10.2 上,您的配置工作正常。

我已经检查了 nginx 的来源。 “include”指令不只是替换为包含文件的内容。它根据上下文进行不同的处理。因此,您可以在包含文件中放入的内容肯定有一些限制。至少在您的 nginx 版本中。 正如nginx documentation 所说

包含的文件应包含语法正确的指令和 块。

【讨论】:

【参考方案2】:

如果 是 http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#if 的一部分。您需要将 rewrite 模块添加到您的 nginx 中。

或者更好的是我们自己的 nginx 官方包

http://nginx.org/en/linux_packages.html#stable

【讨论】:

没有启用 nginx http_rewrite_module 的指令,但是有一个禁用它的指令,它是 --without-http_rewrite_module,它不包含在 OP 的 nginx 版本中。这就是我要求查看nginx -V 输出的原因。有关他的 nginx 版本使用的指令,请参阅 OP 的更新答案。 Aleks,我不认为这是问题所在。我使用了很多重写,还可以在我的问题下方查看 Christos 的评论。测试返回良好的 403。 感谢您的提示。我已经读过了。好吧,我已经保存了您的 nginx -V 输出,通过 perl 将其拆分并搜索 rew。未找到重写模块。 "perl -MData::Dumper -ane 'print Dumper(@F),"\n";' nginx.org/en/docs/debugging_log.html 所述 调试日志给出:2017/03/11 11:03:18 [emerg] 13364#0: /etc/nginx/include.conf:1 中的未知指令“if” 嗯。这是真正的路线还是以某种方式混淆了? "if ($http_referer ~* (badreferers)) ...'。我的意思是,'(badreferers)' 用作正则表达式,'(' 和 ')' 应该是 '(' ')'。是可以使用比“2014 年 3 月 4 日”nginx.org/en/CHANGES-1.4 更年轻的版本吗?【参考方案3】:

看起来你在 nginx 配置文件行中的某个地方像

include /etc/nginx/*.conf;

因此,您的文件 /etc/nginx/include.conf 不仅包含在 /etc/nginx/sites-available/site1.conf 中,而且还包含在“ if" 指令无效,您会收到错误。

【讨论】:

唯一可能包含的地方会干扰,是在文件 /etc/nginx/nginx.conf 中,不是吗?那里不包括在内。 我建议使用 grep 进行搜索: grep -R "conf" /etc/nginx/* 以获取所有包含字符串 "conf" 的文件。或者只是尝试将 include.conf 重命名为 include.inc 并检查是否有帮助。 Alexander,刚刚这样做了,不幸的是,没有任何可能引发错误的隐藏包含... 如果是这样,可能只是旧的 nginx 版本。包含指令有一些限制。如果我记得正确的包含文件可能仅包含诸如位置或服务器之类的块,或者仅包含没有块的简单指令行。在现代 1.10.2 上,您的配置工作正常。 好的,那么请给出这个答案。我无法检查这个,因为我不想干预我的 nginx,因为它是一个实时服务器

以上是关于Nginx 未知指令“如果”的主要内容,如果未能解决你的问题,请参考以下文章

NGINX未知指令流

重启 nginx:未知指令“listen”

Nginx 与 Docker : nginx: [emerg] 未知指令“启用”在 /etc/nginx/nginx.conf

无法启用 geoip 阻止 nginx - [emerg] 未知指令“geoip_country”

nginx:[emerg] 未知指令“”在 /etc/nginx/sites-enabled/example.com:3

尝试启动nginx时出现未知指令“lua_package_path”