+nginx x-accel rubyonrails

Signed-off-by: Nico Schottelius <nico@freiheit.schottelius.org>
This commit is contained in:
Nico Schottelius 2014-09-22 14:28:41 +02:00
parent 721182b98b
commit b932bdb5c4
2 changed files with 57 additions and 0 deletions

View File

@ -0,0 +1,56 @@
[[!meta title="Nginx: Use X-Accel with conflicting regular expressions"]]
## Background
At [[!ungleich]] we use [[!nginx]] for [[!rails]]
hostings of our customers. Nginx is configured to
deliver static files using [[!xaccel]].
We also want to have a longer expiry time for static files,
which is configured seperately in nginx.
## Configuration Options
To support X-Accel, we have added this configuration block into nginx:
# Support for X-Accel
location /protected/ {
internal;
root /home/app/app/shared;
}
To support longer expiry times, we have added this configuration
block:
location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
expires 1y;
# Need to enable proxying in this location as well
try_files $uri @unicorn;
}
## The Problem
Using the configuration as stated above, we encounter the problem that
if an application wants to send a JPEG using X-Accel, the regular
expression block is selected (it has higher priority and matches
the .jpeg ending) and thus the application delivers it, instead of nginx
directly.
## The Solution
Luckily though, [nginx supports giving the prefix based
location block precedence](http://nginx.org/en/docs/http/ngx_http_core_module.html#location):
Instead of using **location /protected/**, we can use **location ^~**. Thus our
previous block can be rephrased to:
location ^~ /protected/ {
internal;
root /home/app/app/shared;
}
And now the application can serve JPEG files via X-Accel.
[[!tag hosting nginx rubyonrails ungleich]]

View File

@ -24,3 +24,4 @@
* [[!shortcut name=libvirt desc="libvirt" url="http://www.libvirt.org"]]
* [[!shortcut name=openstack desc="openstack" url="https://www.openstack.org/"]]
* [[!shortcut name=dry desc="DRY principle" url="https://en.wikipedia.org/wiki/Don%27t_repeat_yourself"]]
* [[!shortcut name=rails desc="Ruby on Rails" url="http://rubyonrails.org/"]]