4

I'm getting tons of the below errors, all the errors are pointing to images that don't actually exist at the location that an error is being given, they are rewrites in Nginx that were converted from Apache.

All was working fine in Apache, it's just since I switched over to Nginx that the images aren't displaying, all other rewrite rules which are just urls all work fine, only the images are breaking?!

Error

2017/04/02 23:15:16 [error] 27629#0: *6 open() "/var/www/html/media/images/blog/10_1.png" failed (2: No such file or directory), client: xxx.xxx.xxx.xxx, server: www.website.com, request: "GET /media/images/blog/10_1.png HTTP/1.1", host: "www.website.com", referrer: "https://www.website.com/blog/"

Apache Rewrite Rules:

## Images
RewriteRule ^media/images/([^/]+)/([^/]+)_([^-]+)_([^-]+)\.png$ /img.php?prefix=$1&refId=$2&thumb=$3&iid=$4 [L]
RewriteRule ^media/images/([^/]+)/([^/]+)_([^-]+)\.png$ /img.php?prefix=$1&refId=$2&thumb=$3 [L]

## Blog Pages
RewriteRule ^blog/$ /?action=blog [L]
RewriteRule ^blog/([a-zA-Z0-9-]+)/$ /?action=blog&category=$1 [L]
RewriteRule ^blog/([a-zA-Z0-9-]+)/([0-9]+)-([a-zA-Z0-9-]+)/$ /?action=blog&category=$1&id=$2&title=$3 [L]

Nginx Rewrite Rules

location /media { 
    rewrite ^/media/images/([^/]+)/([^/]+)_([^-]+)_([^-]+)\.png$ /img.php?prefix=$1&refId=$2&thumb=$3&iid=$4 last; 
    rewrite ^/media/images/([^/]+)/([^/]+)_([^-]+)\.png$ /img.php?prefix=$1&refId=$2&thumb=$3 last; 
}
location /blog { 
    rewrite ^/blog/$ /?action=blog last;
    rewrite ^/blog/([a-zA-Z0-9-]+)/$ /?action=blog&category=$1 last; 
    rewrite ^/blog/([a-zA-Z0-9-]+)/([0-9]+)-([a-zA-Z0-9-]+)/$ /?action=blog&category=$1&id=$2&title=$3 last; 
}

Fix

location ^~ /media/images { 
    rewrite ^/media/images/([^/]+)/([^/]+)_([^-]+)_([^-]+)\.png$ /img.php?prefix=$1&refId=$2&thumb=$3&iid=$4 last; 
    rewrite ^/media/images/([^/]+)/([^/]+)_([^-]+)\.png$ /img.php?prefix=$1&refId=$2&thumb=$3 last; 
}
location /blog/ { 
    rewrite ^/blog/$ /?action=blog last;
    rewrite ^/blog/([a-zA-Z0-9-]+)/$ /?action=blog&category=$1 last; 
    rewrite ^/blog/([a-zA-Z0-9-]+)/([0-9]+)-([a-zA-Z0-9-]+)/$ /?action=blog&category=$1&id=$2&title=$3 last; 
}
llanato
  • 2,508
  • 6
  • 37
  • 59
  • add `/` to location prefixes `location /media/` and precise regexps for the filename, eg `^/media/images/([^/]+)/(\d+)_(\d+)\.png$` – Deadooshka Apr 03 '17 at 17:38

1 Answers1

3

You probably have a conflicting location block in your configuration file, which matches any URI ending with .png.

You can make the location /media block have a higher precedence than regular expression location blocks by adding a ^~ modifier.

For example:

location ^~ /media { 
    rewrite ^/media/images/([^/]+)/([^/]+)_([^-]+)_([^-]+)\.png$ /img.php?prefix=$1&refId=$2&thumb=$3&iid=$4 last; 
    rewrite ^/media/images/([^/]+)/([^/]+)_([^-]+)\.png$ /img.php?prefix=$1&refId=$2&thumb=$3 last; 
}
location ^~ /blog { 
    rewrite ^/blog/$ /?action=blog last;
    rewrite ^/blog/([a-zA-Z0-9-]+)/$ /?action=blog&category=$1 last; 
    rewrite ^/blog/([a-zA-Z0-9-]+)/([0-9]+)-([a-zA-Z0-9-]+)/$ /?action=blog&category=$1&id=$2&title=$3 last; 
}

See this document for more.

Richard Smith
  • 45,711
  • 6
  • 82
  • 81
  • Thanks, you pointed me in the right direction, I've updated my question with the fix that worked for me. – llanato Apr 19 '17 at 15:54