【问题标题】:NGINX config regex issueNGINX 配置正则表达式问题
【发布时间】:2012-06-15 06:42:19
【问题描述】:

我有点迷路了。在我的 nginx conf 文件中,我想要一个声明如下的规则:

if $request_uri does not contain following words (css|images|js) then rewrite all to /index.php

这是我得到的,但它不起作用:

if ($request_uri ~ !(images|css|js)) {
    rewrite $ /index.php;
}

我认为正则表达式不匹配?

编辑:这就是解决方案

if ($request_uri !~* (images|css|js)) {
    rewrite $ /index.php;
}

【问题讨论】:

    标签: regex configuration nginx


    【解决方案1】:
    1. IfIsEvil 和昂贵的。
    2. 改用正逻辑。

    最好将所有静态内容放在一个目录中。

    -static 
     |--images
     |--css
     |--js
    
    
    server {
      server_name www.foo.com;
      root /your_nginx_root/;
    
      location / {
        index  index.php;
      }
    
      location /static {
        add_header Cache_control "public";
        expires max;
      }
    
      error_page 301 302 400 401 402 403 404 405      /index.php;
      error_page 406 408 409 410 411 412 413 414      /index.php;
      error_page 415 416 495 496 497 500 501 502      /index.php;
      error_page 503 504 507                          /index.php;
    }
    


    *编辑:*试试这个

    if ($request_uri !~* (images|css|js)) {
        rewrite $ /index.php;
    }
    

    【讨论】:

    • 我知道,我的静态文件位于静态文件夹中,但最终正则表达式匹配必须是这样的 (js|css|sitemap|images|fonts|media|uploads|pdf|callbacks )
    • 如果他们在目录中最好使用这种方法,如果没有,让我检查你的条件。
    • @Bundy 进行了编辑,请检查是否有效,我无法在这里测试。
    • 谢谢,它几乎成功了 :) 在我的问题中添加了(对我而言)有效的重写
    猜你喜欢
    • 1970-01-01
    • 2022-06-10
    • 2017-01-03
    • 2014-11-11
    • 2021-09-05
    • 1970-01-01
    • 2011-07-17
    相关资源
    最近更新 更多