llanato
llanato

Reputation: 2503

Nginx: Rewrite Rules Not Working For Images

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

Upvotes: 4

Views: 2202

Answers (1)

Richard Smith
Richard Smith

Reputation: 49742

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.

Upvotes: 3

Related Questions