V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
iyuyue
V2EX  ›  问与答

nginx 路由大小写适配

  •  
  •   iyuyue · 2015-01-19 11:16:59 +08:00 · 3648 次点击
    这是一个创建于 3398 天前的主题,其中的信息可能已经有所发展或是发生改变。
    请教一个问题,如果想让某个目录的路由不对大小写敏感。比如example.com/cool 和 example.com/Cool 映射到 cool 目录,应该怎么配置呢?

    我自己尝试失败了,
    location ~* /cool/ {
    index index.html;
    php ....
    }
    18 条回复    2015-02-10 14:24:29 +08:00
    ryd994
        1
    ryd994  
       2015-01-19 13:13:07 +08:00   ❤️ 1
    大小写混用是什么奇葩坏习惯?
    ~*只是说/cool/ 和/Cool/都会由这个location块处理,但实际处理时就会找不到文件
    如果你只是需要一个quick and dirty的话,可以ln -s cool Cool
    ylhawj
        2
    ylhawj  
       2015-01-19 14:02:23 +08:00   ❤️ 1
    windows是不区分大小写的,所以需要程序中将url统一按照小写或者大写处理就好了。
    iyuyue
        3
    iyuyue  
    OP
       2015-01-19 14:19:21 +08:00
    @ryd994 额,想做到不区分大小写,这样用户可以按自己喜欢的方式来输入。类似于Github。可以用alias来实现么?我试了下会陷入循环= =
    iyuyue
        4
    iyuyue  
    OP
       2015-01-19 14:21:36 +08:00
    @ylhawj 在Mac和Linux用户能看到大小写的情况下,nginx可以把url统一处理成小写的么?
    jasontse
        5
    jasontse  
       2015-01-19 14:23:08 +08:00 via iPad   ❤️ 1
    @iyuyue
    Github 不是系统路径,而是参数查询数据库吧。
    iyuyue
        6
    iyuyue  
    OP
       2015-01-19 14:24:35 +08:00
    @jasontse 嗯是的。。看来要用lua,虽然这个问题的确很奇葩 = =
    ylhawj
        7
    ylhawj  
       2015-01-19 14:24:40 +08:00   ❤️ 1
    @iyuyue 嗯,这样的处理方法很多种,比如说Google下,http://www.myhack58.com/Article/sort099/sort0102/2014/51879.htm,这样的。
    iyuyue
        8
    iyuyue  
    OP
       2015-01-19 14:28:45 +08:00
    @ylhawj 哦哦,多谢。我搜到过这篇...当时看到要重新编译就直接跳过了= =
    ryd994
        9
    ryd994  
       2015-01-20 09:40:55 +08:00 via Android   ❤️ 1
    @iyuyue 如果你只有这一个或者几个路径的话可以rewrite解决。用location ~* 然后里面rewrite到小写,但是要注意加last 防止死循环
    http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite
    不过按照location的匹配顺序,路径是优先于regex,似乎不加也可以……你自己试试吧……
    iyuyue
        10
    iyuyue  
    OP
       2015-01-20 12:33:15 +08:00
    @ryd994 抱歉,我试了一下rewrite .* /cool/ last; 然后500了。 能再说的详细一点么?我对nginx的正则有点晕= =
    ryd994
        11
    ryd994  
       2015-01-21 23:38:04 +08:00 via Android
    @iyuyue 500看error log啊
    ryd994
        12
    ryd994  
       2015-01-21 23:40:45 +08:00 via Android
    你要有两个location啊,一个正则的用来改写,一个静态小写的用来实际提供服务
    处理的时候小写的因为是静态规则所以会优先匹配
    ryd994
        13
    ryd994  
       2015-01-21 23:42:14 +08:00 via Android
    或者你可以试试用break而不是last
    iyuyue
        14
    iyuyue  
    OP
       2015-02-10 10:57:24 +08:00
    @ryd994 抱歉。。后来忙别的忘了回复。最后实现的是这样的
    location ~ /cool/ {
    index index.html;
    ....
    }

    location ~* /cool/ {
    rewrite .* /cool/ last;
    }
    ryd994
        15
    ryd994  
       2015-02-10 13:07:54 +08:00
    @iyuyue 这…………两次regex你不感到蛋疼么…………
    location ^~ /cool/ {
    index index.html;
    ....
    }

    location ~* /cool/ {
    rewrite .* /cool/ last;
    }
    这样就最多只需要一次regex,如果进来就是小写的话就不会检查regex
    iyuyue
        16
    iyuyue  
    OP
       2015-02-10 13:33:58 +08:00
    @ryd994 哦哦 是哦~ 多谢!
    iyuyue
        17
    iyuyue  
    OP
       2015-02-10 13:37:07 +08:00
    @ryd994 另外加了这两个之后,nginx就不会再自动补全目录名称后面的 / 了。。。你有什么好办法么?
    ryd994
        18
    ryd994  
       2015-02-10 14:24:29 +08:00
    @iyuyue

    >If a location is defined by a prefix string that ends with the slash character, and requests >are processed by one of proxy_pass, fastcgi_pass, uwsgi_pass, scgi_pass, or >memcached_pass, then the special processing is performed. In response to a request with >URI equal to this string, but without the trailing slash, a permanent redirect with the >code 301 will be returned to the requested URI with the slash appended.

    照例说应该是这样,为什么没有我也不知道。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4157 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 05:11 · PVG 13:11 · LAX 22:11 · JFK 01:11
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.