如果找不到图像,则 nginx 显示 image404.php

如果找不到图像,则 nginx 显示 image404.php

必须将 .httacess 移入 nginx 版本:nginx/1.11.2,大部分都成功了,但是有一个重写仍然困扰着我。

##Return default placeholder image when image file on server not found
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule \.(gif|jpe?g|png) /image404.php [NC,L]

这是我所做的但没有效果

location ~* ^.+.(jpg|jpeg|gif|png|ico)$ {
    try_files $uri $uri/;
    error_page 404      /image404.php; #not working either /core/images/placeholder.jpg
    access_log      off;
    log_not_found       off;
    aio         on;
    sendfile        on;
    etag            on;
    expires         max;
    add_header      Pragma 'public, must-revalidate, proxy-revalidate';
    add_header      X-Frame-Options SAMEORIGIN;
}

/core/images/placeholder.jpg 存在,并且可以通过 http 轻松访问,image404.php 如下所示

<?php

$file = $_SERVER['DOCUMENT_ROOT'] . '/core/images/placeholder.jpg';
$type = 'image/jpeg';
$filesize = filesize($file);
header("HTTP/1.0 404 Not Found");
header("Content-Type: $type");
header("Content-Length: $filesize");
readfile($file);

这是我的 php 配置文件

location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
try_files $uri $uri/ index.php;
fastcgi_pass   unix:var/run/php5-fpm.sock; #/var/run/php-fpm.sock;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
proxy_connect_timeout  600s;
proxy_send_timeout  600s;
proxy_read_timeout  600s;
fastcgi_send_timeout 600s;
fastcgi_read_timeout 600s;
include        fastcgi_params;
}

我一直在寻找不同的解决方案,但没有一个能帮助我。nginx 显示未找到的图像但只返回相同的结果:(

在日志文件中我只看到这个

tail -F /var/log/nginx/error.log | grep image404.php
2016/07/22 17:27:21 [error] 4906#4906: *34 rewrite or internal redirection cycle while internally redirecting to "index.php", client:  1.2.3.4, server: exsample.com, request: "GET /image404.php HTTP/1.1", upstream: "fastcgi://unix:var/run/php5-fpm.sock", host: "exsample.com"
2016/07/22 17:27:21 [error] 4906#4906: *30 rewrite or internal redirection cycle while internally redirecting to "index.php", client:  1.2.3.4, server: exsample.com, request: "GET /favicon.ico HTTP/1.1", host: "exsample.com", referrer: "http://exsample.com/image404.php"

相关内容