0

Having to move a .httacess into nginx version: nginx/1.11.2, most of it with success but one rewrite is still troubling me.

##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]

Here is what I have done, but not working

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;
}

The /core/images/placeholder.jpg exist and very accessible trough http the image404.php looks like this

<?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);

Here is my php in the conf file

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;
}

I have been looking at different solutions, but none of them helps me out ex. nginx display a not found image but only to return same result :(

in the log file I only see this

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"
Joakim
  • 39
  • 2
  • 7
  • Take a look at http://serverfault.com/questions/505098/what-does-this-nginx-error-rewrite-or-internal-redirection-cycle-mean your 404 error page request is being handled by your php location, which is doing a try_files and ending up with a request for index.php – Drifter104 Jul 22 '16 at 15:57
  • Thx Dritter104 that removed the internal redirect errors but my /image404.php is still not served if image not found, but one error down :) – Joakim Jul 22 '16 at 16:10
  • Try `try_files $uri $uri/ =404;` in the first block. You have no default action (or rather the wrong default action). See [this document](http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files). – Richard Smith Jul 22 '16 at 17:08
  • Hi Richard Smith... the try_file is set in the global part, and as I've understood the guide, this will be used otherwise something else is set in location directive, but the fun part is (playing a bit with the php diretives, it's redirect into /subdir1/subdir2/image404.php rather than /image404.php. and trying to access /image404.php returns as supposed a nice blank page. – Joakim Jul 22 '16 at 18:07
  • message to long so devided into two: And accessing a site where no images is to be found, it returns a blank image holder, not the image404.php image. changing the line error_page 404 /core/images/placeholder.jpg; now returns the expected placeholder.jpg :/ How do I get this right to image404.php ? – Joakim Jul 22 '16 at 18:07

0 Answers0