使用Nginx 0.7.65,我花了几个小时试图获得一个简单的nginx重写工作。 但是,这两个正斜杠似乎不能正常工作:
url之前: http : //www.testme.com/uploads//image.jpg
url之后: http : //www.testme.com/uploads/300/image.jpg
我在我的nginx.conf中使用了下面的重写部分:
#try 1 location ~* \.jpg { (.*)//(image.jpg) $1/300/$2 permanent; }
它没有工作。 任何想法如何让nginx重写正确的工作与两个正斜杠? 我尝试使用//跳出斜杠,但这也没有帮助。 我应该尝试更新到Nginx 0.8,并应该修复它?
我也试过这些(分别),他们没有工作。 我被redirect到一个404错误页面:
#try 2 location ~* \.jpg { rewrite "^(.*)([/]{2})image\.jpg$" $1/300/image.jpg permanent; } #try 3 location ~* \.jpg { rewrite (.+)//image\.jpg $1/300/image.jpg permanent; } #try 4 location ~* \.jpg { rewrite (.+)//(image.jpg) $1/300/$2 permanent; }
原因是rewrite
模块在parsing请求时自动修剪斜杠。 如果你看看错误日志,你会看到这样的东西:
客户端:127.0.0.1,服务器:本地主机,请求:“GET / uploads //
uploads /
image.jpg”5883#0:1“(。+)//(image.jpg) //
image.jpg HTTP / 1.1“,主机:”localhost“
但是,您可以使用$request_uri
variables来保留从客户端收到的原始请求URI:
location ~* \.jpg { if ($request_uri ~ "(.+)\/\/(.+\.jpg)") { set $folder_uri $1; set $file_uri $2; rewrite .* $folder_uri/300/$file_uri permanent; } }
你的正则expression式似乎是错误的,因为只有两个捕获组。 尝试这个:
(.+)//(image.jpg) $1/300/$2
我也做了* a +,因为你可能想在斜杠前面加点东西 。