【发布时间】:2014-09-22 16:41:10
【问题描述】:
我正在一个网站上工作,该网站有不同尺寸的相同图像的副本,例如
/images/200x100/someimage.jpg
/images/400x200/someimage.jpg
等等
图片由 Nginx 直接提供,只有 php 请求被传递给 fastcgi。
如果找不到图像,我想将请求传递给 fastcgi,以便我可以查看是否可以生成正确大小的图像版本,然后返回。
我只是无法让它工作 - 如果图像丢失,我可以让它调用我想要的脚本(而不是只返回 404)但它只是返回 php 脚本的源代码。
这是我的 conf 文件的相关部分:
location ~ (^|/)\. {
return 404;
}
location /imgs {
location ~ \.php$ {return 403;}
}
#Static Contents
location ~* ^.+.(jpg|jpeg|png|gif|bmp)$ {
try_files $uri /$uri @backend;
add_header Pragma "public";
add_header Cache-Control "public";
expires 1y;
access_log off;
log_not_found off;
}
# Static Contents
location ~* ^.+.(ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|eot|woff|svg|htc)$ {
add_header Pragma "public";
add_header Cache-Control "public";
expires 1y;
access_log off;
log_not_found off;
}
location / {
# Check if a file exists, or route it to index.php.
try_files $uri $uri/ /index.php?$query_string;
}
location ~\.php$ {
try_files $uri =404;
fastcgi_pass unix:/dev/shm/apache-php.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
location @backend {
#return 410;
rewrite ^(.*)$ /image.php?url=$1;
fastcgi_pass unix:/dev/shm/apache-php.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
然后,丢失的图像将传递到location @backend,但我无法将 $uri 传递给脚本。
我经历了各种各样的变化我做错了什么?任何建议不胜感激!谢谢。
更新
我已经在另一个 VM 中完美地工作了(我将后端块和图像块复制到另一个 conf 文件中)——唯一的区别是使用 Apache 而不是 fastcgi。
【问题讨论】:
-
尝试在
@backend的重写中添加最后一个关键字并删除之后的行。这应该使用 image.php 上的新位置重做整个过程,由location ~\.php$捕获 -
谢谢,但我想我早些时候在工作时尝试过。今天晚上我一直在另一个虚拟机(在我的笔记本电脑上)上解决这个问题,它运行良好。两个 conf 文件之间的唯一区别是,一个有效的是代理回 Apache 而不是 fastcgi
-
我认为只有在文件存在的情况下才会使用缓存标头,并且如果 try_files 使用后端,那么这将脱离该位置块进入后端块。这是不正确的吗?