Nginx重写问题


nginx rewrite issues

我刚刚从apache切换到nginx,只是为了测试,我遇到了以下问题。我在nginx上使用这个配置

location ~ '.php$ {
        root           html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;
        include        fastcgi_params;

}现在我有一个php脚本makeththumb .php自动调整图像显示在我的网站上。使用apache,工作得很好。使用nginx,我得到这个错误:

2011/12/29 15:13:17 [error] 15548#0: *9 open() "/usr/share/nginx/html/makethumbs.php/0737438664-22.jpg" failed (20: Not a directory), client: 193.138.192.81, server: www.escortele.eu, request: "GET /makethumbs.php/0737438664-22.jpg?width=48&height=64&image=/members/escorte/0737438664-22.jpg HTTP/1.1", host: "escortele.eu:88", referrer: "http://escortele.eu:88/"

问题是它把makethumbs.php看作一个目录,它应该是一个脚本而不是目录。

我不知道该使用什么重写规则,只针对makethumbs.php,所以它的行为像一个脚本,而不像一个目录。

您应该粘贴配置文件的其余部分,因为您所粘贴的内容与您想要的内容无关。

这个错误是因为你有"try_files $uri $uri//index.php;"在你的配置某处。您需要从其中删除$uri/来修复您粘贴的错误。

问题不在

location / {
    try_files $uri $uri/ /index.php;
}

那部分很好。你需要它。

既然你没有发布你的整个配置,我不能确定你的问题是什么,但我可以告诉你我的问题是在同一类型的情况下。根据其他人的建议,我为图像文件定义了如下位置:

location ~* '.(jpg|jpeg|png|gif|css|js|ico)$ {
     expires max;
     log_not_found off;
}

这就是问题所在。因为Nginx使用最特定的匹配位置,以。jpg结尾的URL将匹配这个位置,而这个位置不会告诉它使用index.php。我刚刚去掉了这个位置,它成功了。

额外的提示:

  • 在server块中定义你的根,而不是在location块中。
  • 我认为你不需要"fastcgi_index index.php;"
  • 你不需要硬编码你的文档根目录:

改变
fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;

fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

这是我使用缩略图的脚本:http://escorte.pro:88/makethumbs.txt

我的nginx配置如下:http://escorte.pro:88/nginx.txt

我得到的错误是:

2014/08/27 14:59:07 [error] 20986#0: *86 open()"/usr/share/nginx/html/escorte.pro/makethumbs.php/0737835261-79.jpg"失败(20:不是目录),客户端:83.166.220.234,服务器:escorte. jpg。pro, request: "GET/makethumbs.php/0737835261-79.jpg?width=48&height=64ℑ/members/escorte/0737835261-79.jpg HTTP/1.1", host: "escorte. php/0737835261-79.jpg "专业:88",推荐人:"http://escorte.pro:88/"2014/08/27 14:59:07 [error] 20986#0: *87 open()"/usr/share/nginx/html/escorte.pro/makethumbs.php/0743844296-60.jpg" failed (20: Not a directory), client: 83.166.220.234, server: escorte. jpg。pro, request: "GET/makethumbs.php/0743844296-60.jpg?width=48&height=64ℑ/members/escorte/0743844296-60.jpg HTTP/1.1", host: "escorte. php "。Pro:88", referer: "http://escorte.pro:88/"

makethumbs.php脚本在apache上运行良好

什么线索吗?