nginx 缓存图片的重写规则

nginx 缓存图片的重写规则

我们目前正在将所有服务从 apache2 迁移到 nginx,但目前仍受制于特定的重写规则。apache 中的规则是检查是否存在缓存的图像/缩略图,如果存在,则直接提供输出。否则,它将重写对图像处理器的请求。

# if cached version exists, output directly
RewriteCond %{QUERY_STRING} ^(Guest|Member|avatar|tagged|thumb)$
RewriteCond %{DOCUMENT_ROOT}/cache/$1/%{QUERY_STRING}.jpg -f
RewriteRule (.*)\.jpg$ /cache/$1/%{QUERY_STRING}.jpg [L]

# otherwise redirect all jpg-image-requests to processing
# script if they are not in cache dir
RewriteCond %{REQUEST_URI} !^/cache
RewriteRule \.jpg$ /image_processing.php [QSA,L]

我们尝试了几种 try_files 方法,但迄今为止都失败了。有人可以帮忙将此重写规则移植到 nginx 吗?

非常感谢!

答案1

如果您向我们展示您已经尝试过的方法,这会容易得多,但据我所知,这样的方法应该可以解决问题(假设您有一个 vhost 范围的root参数集;如果没有,您无论如何都需要修复它):

location ~ ^.*/.*\.jpg$ {
    try_files $uri @image_processor;
}

location @image_processor {
    include /etc/nginx/fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $documentroot/image_processing.php;
    fastcgi_pass unix:/home/user/something/php.sock;
}

我不知道您在第一个块中想要实现什么;我无法理解为什么您要使用那样的查询字符串。

相关内容