FAQ

Page Discussion Edit History

X-accel

Revision as of 15:46, 21 April 2012 by VBart (Talk | contribs)

(diff) 鈫 Older revision | Latest revision (diff) | Newer revision 鈫 (diff)

Contents

[edit] Synopsis

X-accel allows for internal redirection to a location determined by a header returned from a backend. This allows you to handle authentication, logging or whatever else you please in your backend and then have Nginx handle serving the contents from redirected location to the end user, thus freeing up the backend to handle other requests. This feature is commonly known as X-Sendfile.

This feature differs a bit from standard Nginx modules as it does not rely on directives but rather handles headers from upstream in a special way. The way it works is that you send the header x-accel-redirect with a URI. Nginx will match this URI against its locations as if it was a normal request. It will then serve the location that matches the defined root + URI passed in the header.

Example configuration, notice the difference between alias and root.

# Will serve /var/www/files/myfile.tar.gz
# When passed URI /protected_files/myfile.tar.gz
location /protected_files {
	internal;
	alias /var/www/files;
}
 
# Will serve /var/www/protected_files/myfile.tar.gz
# When passed URI /protected_files/myfile.tar.gz
location /protected_files {
	internal;
	root /var/www;
}

You can also proxy to another server.

location /protected_files { 
	internal;
	proxy_pass http://127.0.0.2; 
}

[edit] Special Headers

[edit] X-Accel-Redirect

syntax: X-Accel-Redirect uri

default: X-Accel-Redirect void

Sets the URI for Nginx to serve.

[edit] X-Accel-Buffering

syntax: X-Accel-Buffering [yes|no]

default: X-Accel-Buffering yes

Sets the proxy buffering for this connection. Setting this to "no" will allow unbuffered responses suitable for Comet and HTTP streaming applications. Setting this to "yes" will allow the response to be cached.

[edit] X-Accel-Charset

syntax: X-Accel-Charset charset

default: X-Accel-Charset utf-8

Sets the charset of the file.

[edit] X-Accel-Expires

syntax: X-Accel-Expires [off|seconds]

default: X-Accel-Expires off

Sets when to expire the file in the internal Nginx cache, if one is used.

[edit] X-Accel-Limit-Rate

syntax: X-Accel-Limit-Rate bytes [bytes|off]

default: X-Accel-Limit-Rate off

Sets the rate limit for this single request. Off means unlimited.

[edit] References

Original Documentation