Nginx 未知指令 "if"
Nginx unknown directive "if"
在我的 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 version: nginx/1.4.6 (Ubuntu) built by gcc 4.8.4 (Ubuntu
4.8.4-2ubuntu1~14.04.3) TLS SNI support enabled configure arguments: --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_module --with-mail --with-mail_ssl_module
if 是 http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#if 的一部分。您需要将重写模块添加到您的 nginx 中。
或者更好的是我们来自 nginx 的官方包他们自己
看起来你在 nginx 配置文件的某个地方有这样一行
include /etc/nginx/*.conf;
所以你的文件 /etc/nginx/include.conf 不仅包含在 /etc/nginx/sites-available/site1.conf 中,还包含在其他地方其中 "if" 指令无效,您收到错误消息。
您使用旧的 nginx 1.4 版本。在现代 1.10.2 上,您的配置工作正常。
我检查了 nginx 的来源。 "include" 指令不只是替换为包含文件的内容。它根据上下文进行不同的处理。因此,您可以放入包含文件中的内容肯定有一些限制。至少在你的 nginx 版本中。
正如nginx documentation所说
Included files should consist of syntactically correct directives and
blocks.
在我的 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 version: nginx/1.4.6 (Ubuntu) built by gcc 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.3) TLS SNI support enabled configure arguments: --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_module --with-mail --with-mail_ssl_module
if 是 http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#if 的一部分。您需要将重写模块添加到您的 nginx 中。
或者更好的是我们来自 nginx 的官方包他们自己
看起来你在 nginx 配置文件的某个地方有这样一行
include /etc/nginx/*.conf;
所以你的文件 /etc/nginx/include.conf 不仅包含在 /etc/nginx/sites-available/site1.conf 中,还包含在其他地方其中 "if" 指令无效,您收到错误消息。
您使用旧的 nginx 1.4 版本。在现代 1.10.2 上,您的配置工作正常。
我检查了 nginx 的来源。 "include" 指令不只是替换为包含文件的内容。它根据上下文进行不同的处理。因此,您可以放入包含文件中的内容肯定有一些限制。至少在你的 nginx 版本中。 正如nginx documentation所说
Included files should consist of syntactically correct directives and blocks.