X-Accel-Redirect

hi! (sorry for my english)

that header only be used with files that are on the hard disk, true ?

because I have a script to resize the size of some images with php,

in the script i use… getimagesize($image) , then, depending on the
size:

  1. $thumb=ImageCreateTrueColor($new_x,$new_y)
    $im=@imagecreatefromjpeg($image);
    imagecopyresampled($thumb,$im,0,0,0,0,$new_x,$new_y,$old_x,$old_y);
    header(“Content-type: image/jpeg”); imagejpeg($thumb,‘’,70);

or

  1. header(“Content-type: image/jpeg”); readfile($image);

– In the first case X-Accel-Redirect not working because the image is
in memory?
– If I change the second case to:

header(“Content-type: image/jpeg”); header(“X-Accel-Redirect: /$image”);
readfile($image);

how can I know if the header X-Accel-Redirect is working?

thanks!!!

Posted at Nginx Forum:

x accel redirect also works with proxied url locations, in addition to
on disk. i don’t know about in memory items.

– In the first case X-Accel-Redirect not working because the image is in
memory? – If I change the second case to:

header(“Content-type: image/jpeg”); header(“X-Accel-Redirect: /$image”);
readfile($image);

how can I know if the header X-Accel-Redirect is working?

The X-Accel-Redirect header basically says:

Dear nginx (or some other frontend),

I know you’re talking to me with fastcgi or http (proxy), but now I’ve
figured out that I’m just going to be sending the client a file. You
can
do this better than me (by talking directly to the kernel and disk,
and
not sending the file over the fastcgi or proxy connection), so here’s
the
name of the file.

Love,

Backend Application

So, what you want to do in your backend is this:

header(“Content-type: image/jpeg”);
if ( X_ACCEL_DIRECT ) { // this shoudl configurable in a productised
app
header(“X-Accel-Redirect: /$image”);
exit;
}
readfile($image); // send the image if we’re not using
X-Accel-Redirect

The initial path should probably be mildly more unique though, so you
can
deal with it separately in your nginx configuration. But not
necessarily.

Short answer: X-Accel-Redirect is helpful only when the file is on disk.

:slight_smile:

  • Jeff

in short, how can I verify that X-Accel-Redirect is working?

I looked at the headers with the plugin for firefox live http headers but
I see no differences

Add a header to an exclusive chunk of your PHP code or nginx
configuration.
For example:

if ( X_ACCEL_REDIRECT ) {
header(“X-Accel-Redirect: /$image”); // nginx will strip this
header(“X-For-The-Win: win”); // nginx won’t strip this though
exit;
}
readfile($image); // won’t ever have the X-For-The-Win header

You could also do something on the nginx side, but this lets you know
that
your app is at least doing the right thing.

If you’re proxy_pass-ing to the app, you can also just use curl or wget
to
talk to it directly, to see which headers it’s passing to nginx.

(Note that you can configure nginx to do anything based on the URL it
gets
from the backend -> you could proxy_pass again, fastcgi_pass, whatever.
But
it is most useful when you tell nginx to just serve up a static file.)

  • Jeff

Jeff W. Wrote:

working?
the kernel and disk, and

The initial path should probably be mildly more

many thanks to both for responding

in short, how can I verify that X-Accel-Redirect is working?

I looked at the headers with the plugin for firefox live http headers
but I see no differences

thanks!!

Posted at Nginx Forum:

X-Accel-Redirect is for sending files already in the hard drive,
rather than dinamically generated ones. The point is to do some logic
in a scripting language and to send a file to the user without them
knowing the real location on the server. Most of the time with:

location /protected {
internal;
}

X-Accel-Redirect is for sending files already in the hard drive, rather
than dinamically generated ones.

It’s designed for this, sure, but the way it’s handled with nginx, you
can
do any kind of Crazy Shit ™ at the front end if you wish. :slight_smile:

location ^~ /protected {
proxy_pass http://www.disney.com/; # ← Crazy Shit ™
}

:wink:

  • Jeff

thank you very much for helping!

X-Accel-Redirect header seems to work correctly :wink:

I still have some things to fix but overall I’m very happy with the
performance of nginx as frontend of apache.

Posted at Nginx Forum:

Hello!

On Wed, Sep 30, 2009 at 06:59:26PM +1000, Jeff W. wrote:

The X-Accel-Redirect header basically says:

readfile($image); // send the image if we’re not using X-Accel-Redirect

The initial path should probably be mildly more unique though, so you can
deal with it separately in your nginx configuration. But not necessarily.

Short answer: X-Accel-Redirect is helpful only when the file is on disk.

Not really. It may as well mean “Dear nginx, I’m really sorry but
I have no file in question available here. But I’m sure it should
be available from this URI (that will map to different backend),
try it.”

But sending file contents along with X-Accel-Redirect header is of
course meaningless.

Maxim D.

Most of that is right, but, the file does not have to be on disk. If
you accel-redirect using an http:// link instead of a file path, that
will also work.