Nginx重写的多个条件

时间:2014-05-22 10:16:43

标签: nginx url-rewriting

我的nginx配置如下

  location /eicnews/ {
  set $con "";

  if ($request_uri ~ ^/eicnews\/(.*)$) {
    set $con  A;
  }

  if ($request_uri ~ ^/eicnews\/$) {
    set $con  "${con}B";
  }

  if ($con = A) {
rewrite ^/eicnews\/eicnewscontent_(.*)\.shtml$ http://news.abc.cn/eic/news_$1.shtml permanent;
        }

        if ($con = AB) {
rewrite ^/eicnews\/(.*)$ http://news.abc.cn/eic permanent;

        }

}

我的配置意味着 1.如果客户要求直接命名" / eicnews /"将被重写为url / dir 2.如果客户要求包含名为" / eicnews /"的直接文件。将重写为url / dir / files

但现在有两种文件名在" / eicnews /" ,eicnewscontent_xxxx.shtml和eicnewslist_xxxx.shtml,我想如果前缀" eicnewscontent"文件被重写为http://news.abc.cn/eic/news_ $ 1.shtml和前缀" eicnewslist"将被重新改写为http://news.abc.cn/eic

谢谢大家!!!!

我的意思是,在directoty中有2种前缀文件,例如eicnewslist_xxxxx.shtml和eicnewscontent_xxxxx.shtml,前缀" eicnewslist"重写为一个网址,前缀" eicnewscontent"重写到另一个url.I想要它如下。语法是可以的。但它不起作用。

location =  /eicnews/ {

        rewrite  ^/eicnews/eicnewslist(_.*\.shtml)$  http://news.smartjs.cn/eic$1 permanent;
    }
    location /eicnews/ {
        rewrite ^/eicnews/eicnewscontent(_.*\.shtml)$ http://news.smartjs.cn/eic/news_xx.shtml$1 permanent;
    }

1 个答案:

答案 0 :(得分:1)

尽可能避免if s。阅读http://wiki.nginx.org/IfIsEvil

在你的情况下,我会做以下事情:

location = /eicnews/ {
    rewrite ^ http://news.abc.cn/eic permanent;
}

location /eicnews/ {
    rewrite ^/eicnews/eicnewscontent(_.*\.shtml)$ http://news.abc.cn/eic/news$1 permanent;
}

我不确定您对/eicnews/other-path的请求有什么看法。