V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
Joming
V2EX  ›  NGINX

求关于 nginx 配置 if 判断载入自定义配置的方法

  •  
  •   Joming · Jun 18, 2016 · 3812 views
    This topic created in 3605 days ago, the information mentioned may be changed or developed.
    想实现 nginx 自动判断网站根目录下是否存在.htaccess 文件,如果根目录存在.htaccess 则 include 该规则,如果不存在该文件就忽略。

    if (-f \.htaccess) {
    include \.htaccess;
    }

    上面肯定不对,求V友指教!
    6 replies    2016-06-27 22:42:15 +08:00
    msg7086
        1
    msg7086  
       Jun 18, 2016
    nginx 不认 htaccess 规则。
    if 不能随便用。
    而且 nginx 不能自动重载配置吧?
    notgod
        2
    notgod  
       Jun 18, 2016
    你这是从头错到脚了

    你要明白的问题在于
    Nginx 的 rewrite 的规则属于配置文件 更新以后必须 reload nginx, 配置文件才会生效
    你这个 if 的前提条件就死了 因为不 reload 根本无效
    我们就算你设置监控脚本 监控如果有 nginx rewrite 的配置文件被上传 就 reload 一次
    那么你如何保证这个 rewrite rules 是写法正确的勒? 如果错了 nginx ==== down
    根本启动不了

    就算在监控 使用 nginx -t 如果 pass 才 reload

    但是你这问题都问出来了 自己写 就不太现实了
    而且这个脚本写了 不难 难在维护和保证稳定性 并且保证其逻辑一直要正确.......
    fcicq
        4
    fcicq  
       Jun 18, 2016
    @orzfly 不要指望活在 apache 托管 php 时代的人去学 openresty.
    yeyeye
        5
    yeyeye  
       Jun 18, 2016
    @notgod 另外放一个 nginx 专门用于测试规则是不是有效的 哈哈哈
    Joming
        6
    Joming  
    OP
       Jun 27, 2016
    @notgod
    @fcicq
    感谢!我目前在使用 Directadmin ,里头有默认的 nginx 规则,因为每次使用 directadmin 编辑一次会重新 reload 一次,并且更新 nginx 的配置,现在想实现目录下没有.htaccess 文件就忽略,有就按照.htaccess 执行 rewrite 。现在直接 include .htaccess 能够正常执行 rewrite ,但删除.htaccess 就会报 403 的错误。所以就来讨教了。
    @orzfly 正在努力学习中,感谢!
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2482 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 07:53 · PVG 15:53 · LAX 00:53 · JFK 03:53
    ♥ Do have faith in what you're doing.