Welcome! Log In Create A New Profile

Advanced

How to redirect only if a file exists ?

Posted by kuteninja 
How to redirect only if a file exists ?
October 27, 2009 05:11PM
I'm using an url cache for some files, this are their url:

/pr/index.php?url=URL
/pr/cache/URL

Where the second is the first page cached (once per hour)
I'm now using the apache to know if there's the cached version, then print it, else, built it with the PHP.

It works, but I was wondering if I can use nginx to serve it since it's a static file (nginx redirects the php files to apache)

It should redirect the first URL to the second URL, ONLY if it exists, if it doesn't exist, it should do nothing
Re: How to redirect only if a file exists ?
October 29, 2009 09:48AM
With Drupal we use a static file caching module called Boost. I assume you're doing something like that or Wordpress's WP-Supercache. We use extra @cache and @dynamic locations to check for "logged in" cookies, to set expires headers and to simplify logic for other locations:

http://drupal.org/node/244072#comment-1756574

However, in its simplest form, what you're asking for is this:

[code]
location / {
try_files /pr/cache/$uri $uri /pr/index.php?q=$1;
}

location ~ \.php$ {
...
}
[/code]

Make sure you're not using an old 6.x branch.



Edited 1 time(s). Last edit at 10/29/2009 09:49AM by brianmercer.
Re: How to redirect only if a file exists ?
October 29, 2009 11:46AM
Small error in there. That would have to be:
[code]
location / {
try_files /pr/cache/$uri $uri /pr/index.php?q=$uri;
}

location ~ \.php$ {
...
}
[/code]
or :
[code]
location / {
try_files /pr/cache/$uri $uri @dynamic;
}

location @dynamic {
rewrite ^/(.*)$ /index.php?q=$1 last;
}

location ~ \.php$ {
...
}
[/code]
We have to use the second version with Drupal because Drupal sometimes flakes on the leading slash in $uri. Go figure.
If you're just using nginx for the static files and want to proxy to Apache or something else instead of using fastcgi/php for dynamic requests it'd be something like:
[code]
location / {
try_files /pr/cache/$uri $uri @dynamic;
}

location @dynamic {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://127.0.0.1:8080;
}
[/code]
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 152
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