我是nginx新手,我想要做的就是捕获所有的图像请求(* .jpg | png | gif),并将它们转发到根目录之外的php脚本。
我目前的工作脚本是这样的:
server { index index.php; # PHP-FPM location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php5-fpm.sock; } # Images handler root /var/www/images-handler; location ~* \.(gif|jpg|png) { rewrite ^ /img.php last; } }
问题是我必须将rootvariables更改为/var/www/website-contents 。 由于website-contents和images-handler没有相同的父目录,我添加一个rootvariables到我的location段像这样:
root /var/www/website-contents; location ~* \.(gif|jpg|png) { root /var/www/images-handler; rewrite ^ /img.php last; }
但它不起作用。 所有的图像返回一个404响应。
我必须以/var/www为根,并分别在自己的location段和~* \.(gif|jpg|png)添加website-contents和images-handler ~* \.(gif|jpg|png) 我应该为/var/www/website-contents /var/www/images-handler创build一个符号链接吗?
有没有更好的解决scheme,所有这一切?
非常感谢。
目前,所有的php脚本都是由你location ~ \.php$ block执行的,并以你提供的root 。 如果你想用一个不同的根执行一个php脚本,你需要覆盖那个位置。 有很多方法可以做到这一点。 只是一个PHP脚本,一个location =将是最简单的。 例如:
root /var/www/website-contents; location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php5-fpm.sock; } location ~* \.(gif|jpg|png) { rewrite ^ /img.php last; } location = /img.php { root /var/www/images-handler; include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php5-fpm.sock; }