Static files with Nginx + Passenger

02/02/2010 engineering

Normally, when you setup Nginx and Passenger to serve your rails application, all static files are served by Nginx without hitting Passenger. What really happens is that all static files that do exist are served by Nginx and the rest are passed on to passenger.

For example

will be served by Nginx , but

will continue to Passenger going all the way through the Rails stack. Instead of a simple 1ms nginx process , you get a full stack rails request with a route recognition error. Not good.

Missing or outdated images (specially if your site is big on content) or stupid robots searching for certain .php (or .js, or _vti_bin(?) or whatever) vulnerable files is common place. Why would you want those requests to pass on to passenger?

Using an asset host

One solution is to use an asset host for all static files by using an absolute url like

<script type="text/javascript" src=""></script>

instead of

<script type="text/javascript" src="/javascripts/prototype.js"></script>

Then you can make a separate entry for nginx for with just a root directive to your public directory. This way static files will always be served by nginx, even the missing ones.

In addition you can specify multiple asset hosts and tell Rails to cycle through them. This is especially good for splitting the load among multiple servers and bypassing the 2 requests per ip browser constraint. What I don't like with this approach is that simple image tags have to go through the image_tag helper, causing some overhead for no good reason.

Configure Nginx

        set $asset F;

        if ($request_filename ~* ".*\.(gif|jpg|css|jpeg|png|bmp|asp|php|_vti_bin|js)$") {
             set $asset T;

        if (!-f $request_filename) {
             set $asset "${asset}T";

        if ($asset = TT)
                return 404;

Ποιοι είμαστε


Η Skroutz Α.Ε. ιδρύθηκε το 2005 και δραστηριοποιείται στην ανάπτυξη καινοτόμων υπηρεσιών τεχνολογίας, δημιουργώντας πρωτοποριακές πλατφόρμες ηλεκτρονικού εμπορίου και ιστοσελίδες υψηλής απόδοσης.

Με έδρα την Αθήνα, η Skroutz Α.Ε. είναι το κορυφαίο digital brand πίσω από τη δημιουργία και την εξέλιξη των δυνατοτήτων της πρωτοποριακής μηχανής αναζήτησης και σύγκρισης τιμών και προϊόντων Η εταιρεία παρέχει ένα εύρος χρηστοκεντρικών λύσεων λογισμικού και πλατφόρμων ηλεκτρονικού εμπορίου αξιοποιώντας νέες τεχνολογίες και μεθοδολογίες, που αναδεικνύουν το πάθος του ανθρώπινου δυναμικού της.