求教 nginx 内无法使用 include 调用含有 if 的配置?

2021-06-24 17:31:52 +08:00
 stille

分别在 docker nginx 和 yum install nginx 两个环境下测试都报错.

在 conf.d 内创建了域名 xxx.com.conf 并在配置中使用了 if 语句,测试通过,reload 后也生效.

但是,当在 conf.d 创建 2.conf ,将 if 语句放在这里.
并在 xxx.com.conf 中 include conf.d/2.conf; 测试均报错

nginx: [emerg] "if" directive is not allowed here in /etc/nginx/conf.d/2.conf:1
nginx: configuration file /etc/nginx/nginx.conf test failed

奇怪的是 使用军哥 LNMP.org 安装的环境却成功了.

求教大神是否是什么模块需要编译安装还是说有其他配置?

if 语句就是简单的正则过滤.

    if ($request_uri ~* "(aaa|bbb|cdf|223|sasss)") {
        return 301 https://xxxx.com$request_uri;
    }
1062 次点击
所在节点    NGINX
4 条回复
eason1874
2021-06-24 17:38:47 +08:00
有 if 可以 include

你的问题应该是在 nginx.conf 写了 include conf.d/*.conf;

所以 2.conf 不仅被 xxx.com.conf 载入了,也被 nginx.conf http 载入了,而 http 段是不支持 if 的
stille
2021-06-24 17:42:34 +08:00
@eason1874 噢,感谢回复,那最佳解决方案是换个目录放这个 2.conf 吧,只要不是 nginx.conf include 的到就行...然后单独给域名.conf include 到就行
stille
2021-06-24 17:54:51 +08:00
@eason1874 谢谢.测试成功了...
ryd994
2021-07-03 05:15:05 +08:00
BTW, 你这不需要 if 啊,加一个 regex location 不就好了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/785593

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX