我正在尝试学习如何使用nginx以及如何使用它的重写function
Nginx似乎在做重写:
2012/03/27 16:30:26 [notice] 16216#0: *3 "foo.php" matches "/foo.php", client: 61.90.22.223, server: localhost, request: "GET /foo.php HTTP/1.1", host: "domain.com" 2012/03/27 16:30:26 [notice] 16216#0: *3 rewritten data: "img.php", args: "", client: 61.90.22.223, server: localhost, request: "GET /foo.php HTTP/1.1", host: "domain.com"
但在我的访问日志中,我得到以下内容:
61.90.22.223 - - [27/Mar/2012:16:26:54 +0000] "GET /foo.php HTTP/1.1" 404 31 "-" "Mozilla/5.0 (Windows NT 6.1; rv:11.0) Gecko/20100101 Firefox/11.0" 61.90.22.223 - - [27/Mar/2012:16:30:26 +0000] "GET /foo.php HTTP/1.1" 404 31 "-" "Mozilla/5.0 (Windows NT 6.1; rv:11.0) Gecko/20100101 Firefox/11.0"
在根目录中有一个img.php,所以我不知道为什么我得到一个404错误
这是configuration块的一部分:
rewrite foo.php img.php last; location / { try_files $uri $uri/ /index.html; } location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi_params; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # location ~ /\.ht { deny all; }
您需要提供完整的path(相对于文档根目录)作为目标:
rewrite foo.php /img.php last;
在img.php之前加上斜线就足够了(在Nginx 1.0.14 / CentOS 6上testing)。
Nginx将采取它生成的path,如果它以http://开头,将执行301或302redirect,否则将追加到文档根目录。 在后一种情况下,新path需要以斜杠开始。
此外,如果您只想匹配文档根目录中的“foo.php”,则应该在该参数前加一个斜杠(不仅更具体,而且匹配速度更快):
rewrite ^/foo.php$ /img.php last;