Welcome! Log In Create A New Profile

Advanced

Apache file exists rewrite to try_files migration

Posted by Simon 
Apache file exists rewrite to try_files migration
November 29, 2011 12:38AM
I have the following Apache rewrite code to check for requests for image files in my photo gallery which are no longer there (because they have been moved to a CDN server), and rewrite the request to a PHP script which interprets the request and subsequently redirects the request to the new CDN location:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^gallery/data/([0-9]*)/(.*)$ showphoto.php?cat=$1&file=$2 [R=301,L]

I'm wondering if I can achieve the same in nginx using try_files?

There are two parameters I capture - the first is a "category id", which corresponds to a directory in the gallery (numeric - hence the [0-9]), the second being the filename itself. I could re-write the logic of showphoto.php if that data can only be passed in another format (eg "999/filename.jpg").
Re: Apache file exists rewrite to try_files migration
November 29, 2011 06:40AM
location ~ /gallery/data/(?<catid>\d+)/(?<filename>.*)$ {
try_files $uri /showphoto.php?cat=$catid&file=$filename;
}
Re: Apache file exists rewrite to try_files migration
November 29, 2011 07:10AM
Thanks locojohn ... I can see the logic behind what you've written - makes sense. However, nginx doesn't like it :(

'Restarting nginx: [emerg]: unknown "catid" variable'

Any suggestions?
Re: Apache file exists rewrite to try_files migration
November 29, 2011 07:21AM
Sure, you seem to have an old PCRE library installed. Try the folllowing:


location ~ /gallery/data/(?P<catid>\d+)/(?P<filename>.*)$ {
try_files $uri /showphoto.php?cat=$catid&file=$filename;
}


This is because:

The PCRE library supports named captures using the following syntax:

?<name> Perl 5.10 compatible syntax, supported since PCRE-7.0
?'name' Perl 5.10 compatible syntax, supported since PCRE-7.0
?P<name> Python compatible syntax, supported since PCRE-4.0

As stated in http://nginx.org/en/docs/http/server_names.html

Andrejs
Re: Apache file exists rewrite to try_files migration
November 29, 2011 04:57PM
I checked my setup, pretty sure I have PCRE v7.8 installed ... either way, the ?P<name> syntax didn't work either - same error.
Re: Apache file exists rewrite to try_files migration
November 29, 2011 05:30PM
nginx version?
Re: Apache file exists rewrite to try_files migration
November 29, 2011 05:31PM
nginx 0.7.65
Re: Apache file exists rewrite to try_files migration
November 29, 2011 05:32PM
This is the culprit. PCRE capturing seems to have appeared in later versions. Are you willing to upgrade?

Andrejs
Re: Apache file exists rewrite to try_files migration
November 29, 2011 10:24PM
ahh - I'm running Ubuntu v10.04 ... I just told it to install nginx and that is what it gave me.

I've upgraded to nginx 1.0.10 now ... the config no longer fails, and from what I can tell, the matching is working fine - thanks.

However, there is some conflict with something else in my config which is preventing it from working completely. Perhaps you can shed some light on it for me?

I'm running vBulletin + vbSEO ... which rewrites all files through vbseo.php.

When I try and access the image from the gallery, I get redirected to a vbSEO error page, which indicates to me that it's somehow getting to vbseo.php rather than stopping with showphoto.php?

Suggestions?

server {
server_name www.zoochat.com;
access_log /var/log/nginx/zoochat.com_access;
error_log /var/log/nginx/zoochat.com_error;
root /srv/www/zoochat.com;

location ~ /gallery/data/(?<catid>\d+)/(?<filename>.*)$ {
try_files $uri /showphoto.php?cat=$catid&file=$filename;
}

location / {
rewrite ^/((urllist|sitemap_).*\.(xml|txt)(\.gz)?)$ /vbseo_sitemap/vbseo_getsitemap.php?sitemap=$1 last;

if (!-e $request_filename) {
rewrite ^/(.*)$ /vbseo.php last;
}
}

if ($request_filename ~ "\.php$" ) {
rewrite ^/(.*)$ /vbseo.php last;
}

location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/php-fastcgi/php-fastcgi.socket;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

}
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 301
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready