我在windows10中使用Nginx1.8.0,这是我试图重写请求的核心配置
server {
listen 80;
server_name localhost;
rewrite ^/qt/(\w+)/(\d+)/(\d+)/(\d+)_(\d+).(\w+)$ /qtfile/$1/$4/$5.$6 last;
# /qt/zoom/x_plus/y_plus/x_y.type ==> /qtfile/zoom/x/y.type
location /qtfile {
access_log logs/images.log;
alias D:/tiles/;
}
location / {
root D:/www;
}
}
链接http://localhost/qtfile/s/214/7645/102.jpg
将返回正确的图片,但链接http://localhost/qt/s/214/323/34254/7645_102.jpg
会抛出错误:
2015/10/10 10:40:09 [error] 1420#11676: *1 CreateFile() "D:/www/qt/s/214/323/34254/7645_102.jpg" failed (3: The system cannot find the path specified), client: 127.0.0.1, server: localhost, request: "GET /qt/s/214/323/34254/7645_102.jpg HTTP/1.1", host: "localhost"
似乎请求被loation /
段抓取。
有什么不对吗?
答案 0 :(得分:1)
看起来您的正则表达式输入不正确。您为示例http://localhost/qt/s/214/323/34254/7645_102.jpg
提供了正则表达式^/qt/(\w+)/(\d+)/(\d+)/(\d+)_(\d+).(\w+)$
。您的正则表达式中只有两个数字段,但URL中有三个。
根据网址,您的正则表达式应为:
^/qt/(\w+)/\d+/\d+/\d+/(\d+)_(\d+).(\w+)$
您必须将重写的第二部分调整为
/qtfile/$1/$2/$3.$4