TONYHEAD
V2EX  ›  问与答

关于 Nginx 下的 rewrite 规则

  •  
  •   TONYHEAD · Sep 1, 2014 · 2491 views
    This topic created in 4362 days ago, the information mentioned may be changed or developed.
    Nginx规则,把所有http://abc.com/archive/tag/苹果 交给 http://abc.com/cgi-bin/mt/mt-search.cgi?IncludeBlogs=1&tag=苹果 处理

    rewrite /archive/tag/(.+)$ /cgi-bin/mt/mt-search.cgi?IncludeBlogs=1&tag=$1 last;

    但我在http://abc.com/archive/tag/下有一个http://abc.com/archive/tag/index.html,请问如何改写rewrite使得http://abc.com/archive/tag/会去访问该目录下的index.html文件而不是去请求一个http://abc.com/cgi-bin/mt/mt-search.cgi?IncludeBlogs=1&tag=index.html 呢?

    谢谢。
    3 replies    2014-09-01 22:31:09 +08:00
    sophymax
        1
    sophymax  
       Sep 1, 2014   ❤️ 1
    rewrite太过晦涩,你需要的是 rewrite_by_lua 用代码的方式来判断,这篇文章的最后一段对你会有帮助 http://www.codezhai.com/code/openresty-lua-%E5%85%A8%E7%AB%99%E9%9D%99%E6%80%81%E8%B5%84%E6%BA%90copy-%E6%8A%93%E7%AB%99%E7%A8%8B%E5%BA%8F-%E6%95%B4%E7%AB%99%E6%8A%93%E5%8F%96/
    lnkisi
        2
    lnkisi  
       Sep 1, 2014   ❤️ 1
    将index.html放在另一个目录不就好了
    rewrite ^/archive/tag/index.html$ /archive/real_tag/index.html last
    rewrite ^/archive/tag/(.+)$ /cgi-bin/mt/mt-search.cgi?IncludeBlogs=1&tag=$1 last;
    TONYHEAD
        3
    TONYHEAD  
    OP
       Sep 1, 2014
    @sophymax 感谢,超过了楼主的知识。

    @lnkisi 好主意,试验成功,谢谢。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5293 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 08:57 · PVG 16:57 · LAX 01:57 · JFK 04:57
    ♥ Do have faith in what you're doing.