【问题标题】:How can I write an nginx location directive that matches the url /%ef?如何编写与 url /%ef 匹配的 nginx 位置指令?
【发布时间】:2015-03-30 00:45:12
【问题描述】:

我想匹配GET /%ef HTTP/1.1 之类的请求。所以我创建了这个配置:

location /%ef {
    return 400;
}

我用调试编译了 nginx,并做了curl -v 'localhost:8080/%ef'。我得到 404 而不是我预期的 400。这是日志的相关部分:

2015/03/29 17:40:43 [debug] 48043#0: *5 http request line: "GET /%ef HTTP/1.1"
2015/03/29 17:40:43 [debug] 48043#0: *5 s:0 in:'2F:/'
2015/03/29 17:40:43 [debug] 48043#0: *5 s:1 in:'25:%'
2015/03/29 17:40:43 [debug] 48043#0: *5 s:4 in:'65:e'
2015/03/29 17:40:43 [debug] 48043#0: *5 s:5 in:'66:f'
2015/03/29 17:40:43 [debug] 48043#0: *5 s:1 in:'EF:ï'
2015/03/29 17:40:43 [debug] 48043#0: *5 http uri: "/ï"
2015/03/29 17:40:43 [debug] 48043#0: *5 http args: ""
2015/03/29 17:40:43 [debug] 48043#0: *5 http exten: ""
2015/03/29 17:40:43 [debug] 48043#0: *5 http process request header line
2015/03/29 17:40:43 [debug] 48043#0: *5 http header: "User-Agent: curl/7.37.1"
2015/03/29 17:40:43 [debug] 48043#0: *5 http header: "Host: localhost:8080"
2015/03/29 17:40:43 [debug] 48043#0: *5 http header: "Accept: */*"
2015/03/29 17:40:43 [debug] 48043#0: *5 http header done
2015/03/29 17:40:43 [debug] 48043#0: *5 event timer del: 4: 1427676103018
2015/03/29 17:40:43 [debug] 48043#0: *5 rewrite phase: 0
2015/03/29 17:40:43 [debug] 48043#0: *5 test location: "/"
2015/03/29 17:40:43 [debug] 48043#0: *5 test location: "50x.html"
2015/03/29 17:40:43 [debug] 48043#0: *5 using configuration "/"

因此,根据2015/03/29 17:40:43 [debug] 48043#0: *5 http uri: "/ï" 行,nginx 将此请求与 /ï 匹配,这就是 %ef 在 url 解码时的含义。好的。我更新了这样的配置:

location /%ef {
    return 400;
}
location /ï {
    return 400;
}

我重复了我的请求。现在我明白了:

2015/03/29 17:37:48 [debug] 48018#0: *4 http request line: "GET /%ef HTTP/1.1"
2015/03/29 17:37:48 [debug] 48018#0: *4 s:0 in:'2F:/'
2015/03/29 17:37:48 [debug] 48018#0: *4 s:1 in:'25:%'
2015/03/29 17:37:48 [debug] 48018#0: *4 s:4 in:'65:e'
2015/03/29 17:37:48 [debug] 48018#0: *4 s:5 in:'66:f'
2015/03/29 17:37:48 [debug] 48018#0: *4 s:1 in:'EF:ï'
2015/03/29 17:37:48 [debug] 48018#0: *4 http uri: "/ï"
2015/03/29 17:37:48 [debug] 48018#0: *4 http args: ""
2015/03/29 17:37:48 [debug] 48018#0: *4 http exten: ""
2015/03/29 17:37:48 [debug] 48018#0: *4 http process request header line
2015/03/29 17:37:48 [debug] 48018#0: *4 http header: "User-Agent: curl/7.37.1"
2015/03/29 17:37:48 [debug] 48018#0: *4 http header: "Host: localhost:8080"
2015/03/29 17:37:48 [debug] 48018#0: *4 http header: "Accept: */*"
2015/03/29 17:37:48 [debug] 48018#0: *4 http header done
2015/03/29 17:37:48 [debug] 48018#0: *4 event timer del: 3: 1427675928222
2015/03/29 17:37:48 [debug] 48018#0: *4 rewrite phase: 0
2015/03/29 17:37:48 [debug] 48018#0: *4 test location: "/"
2015/03/29 17:37:48 [debug] 48018#0: *4 test location: "50x.html"
2015/03/29 17:37:48 [debug] 48018#0: *4 test location: "ï"
2015/03/29 17:37:48 [debug] 48018#0: *4 using configuration "/"

它仍然不匹配我的位置。根据2015/03/29 17:37:48 [debug] 48018#0: *4 test location: "ï" 行,我的新位置似乎被以一种意想不到的方式解释了。

如何编写与 url /%ef 匹配的位置指令?

【问题讨论】:

    标签: nginx configuration routing webserver


    【解决方案1】:

    我试图匹配的其余路线是/%EF%BF%BD。这是replacement character 的 utf-8 字节表示。当我重写 location 指令以实际包含替换字符本身时,nginx 匹配它:

    location /� {
      return 400;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-15
      • 1970-01-01
      • 2020-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-20
      • 2012-11-09
      相关资源
      最近更新 更多