Php Nginx向后端发送缺少图像的请求
我在一个网站上工作,它有不同大小的相同图片的副本,例如 /images/200x100/someimage.jpg /images/400x200/someimage.jpg 等等 图像由Nginx直接提供,只有php请求被传递到fastcgi 如果找不到图像,我希望将请求传递给fastcgi,以便查看是否可以生成正确大小的图像版本,然后返回该版本 我只是不能让它工作-如果图像丢失,我可以让它调用我想要的脚本(而不是仅仅返回404),但它只是返回php脚本的源代码 这是我的conf文件的相关部分:Php Nginx向后端发送缺少图像的请求,php,nginx,fastcgi,Php,Nginx,Fastcgi,我在一个网站上工作,它有不同大小的相同图片的副本,例如 /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;
}
然后,丢失的图像将传递到位置@backend
,但我无法将$uri传递给脚本
我经历了各种各样的变化我做错了什么?感谢您的建议!谢谢
更新
我已经在另一个VM中完美地实现了这一点(我将后端块和图像块复制到了另一个conf文件中)——唯一的区别是使用Apache而不是fastcgi 问题只是一个打字错误 我试图重写到
/image.php
它应该是/images.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 {
rewrite ^(.*)$ /images.php?url=$1;
}
尝试在
@backend
中的“重写”中添加最后一个关键字,然后删除后面的行。这应该用image.php上的一个新位置来重做整个过程,由location~\.php$
谢谢,但我想我之前在工作的时候试过了。今天晚上,我一直在另一台虚拟机(在我的笔记本电脑上)上解决这个问题,效果非常好。这两个conf文件之间的唯一区别是,有效的一个是代理回Apache而不是fastcgiI,它认为只有当文件存在时才会使用缓存头,如果try_文件使用后端,那么这将从该位置块中断到后端块。这是不对的吗?