27
Sep

Static files with Nginx + Passenger

posted by gchatz 51 comments nginx rails

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

http://www.mydomain.com/javascripts/prototype.js

will be served by Nginx , but

http://www.mydomain.com/javascripts/iamastupidrobot.js

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="http://assets.mydomain.com/javascripts/prototype.js"></script>

instead of

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

Then you can make a separate entry for nginx for assets.mydomain.com 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;
                break;
        }

The first condition sets $asset to ‘T’ if the requested file is a static file (or a file that Rails doesn’t care about).
The second condition sets $asset to ‘TT’ if the requested file doesn’t exist.
The third condition breaks the rewrite flow and returns a 404.

There you go!

Comments (51)

Pin Phi.Sanders said on Oct 15, 04:38 PM:

Awesome site, but I can't subscribe in my news reader since your RSS feed is broken...

Pin دردشه said on Jan 27, 04:25 PM:

Found it a great launch
I thank you very much
Appreciation and respect
Heck on the theme of Creative Commons
Kois gone by and Hloowoowo ..

Pin دردشة said on Feb 10, 07:26 PM:

Awesome site, but I can't subscribe in my news reader since your RSS feed is broken...

Pin New Gadgets said on Feb 19, 04:58 PM:

Yeah Paypal is quite usefull. But i find it even more impressive that people are constantly working to improve this genius payment system. Thank your for this tutorial, its very useful.

Pin منتديات said on Feb 23, 11:51 PM:

Found it a great launch
I thank you very much
Appreciation and respect
Heck on the theme of Creative Commons
Kois gone by and Hloowoowo ..

Pin Christian Louboutin said on Mar 29, 01:05 PM:

Everything from darling little slip on flats in Christian Louboutin’s fabulous Tattoo design with the Christian Louboutin name prominently displayed on the toe to the extremely classy.

Pin mp3 dinle said on Apr 10, 04:27 PM:

thanks for your help...

Pin Dental jobs said on Apr 12, 11:19 AM:

Found it a great launch
I thank you very much

Pin John Wilson said on Apr 21, 04:29 PM:

I think using Ngnix approach to serve is a good approach however every image tag has go to the impage_tag helper, which is slightly a discomfort but overall it is good.

Pin jacab said on Apr 22, 12:25 PM:

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

Pin Sports News said on May 10, 06:49 AM:

This blog provides something very useful. do I get a lot of knowledge from this blog. many new script that I learned from this blog. thanks for sharing knowledge.

Pin piano tutorial said on May 13, 10:56 AM:

Your spot on aa assett host is the solution but until now I did not know how to implement it.

Pin debt issues said on May 13, 10:59 AM:

This looks quite straight forward so I will give it a go.

Pin Rapid Prototyping said on May 14, 07:03 AM:

Makes sense!! Good solution to a problem I am sure, many people were facing! It is blogs like these that will help people to try out new ways and means to a solution. The configuration is also given so I am sure it will be easy for anyone to try it out. Blogs like these, I am sure, are a constant inspiration to people who are always striving to make innovations in the different languages, scripts and other areas especially in computers, to make our work easier! I really appreciate the dedication and efforts put into such projects and wish them luck in all their endeavors!!

Pin refurbished cubicles said on May 18, 04:28 PM:

Makes sense!! Good solution to a problem I am sure, many people were facing! It is blogs like these that will help people to try out new ways and means to a solution. The configuration is also given so I am sure it will be easy for anyone to try it out. Blogs like these, I am sure, are a constant inspiration to people who are always striving to make innovations in the different languages, scripts and other areas especially in computers, to make our work easier! I really appreciate the dedication and efforts put into such projects and wish them luck in all their endeavors!!

Pin Dental Jobs said on May 20, 11:19 AM:

very useful . thanks

Pin Bayern Tickets said on May 31, 08:14 PM:

Good find we are using Nginx and Passenger for several fussball und tickets projects as well. In German Da wir aus Bayern kommen ist sind Nginx und Passenger für uns sehr geläufig. Wir nutzen das Skript für verschiedene Ticket Seiten.

Pin Bayern Tickets said on May 31, 08:19 PM:

Just to add to the last comment, that I found the use of the asset host really useful for our Nginx and Passenger projects. We are using these protocolls also for other projects (not only tickets), so this is Blog is useful, thanks

(Comment in German). Da wir dieses Skript oft auch für unsere bayerischen Produkte und Spiele einsetzen, ist es sehr wertvoll. bei Bayern München Spielen hilft es die richtige IT Infrastruktur zu haben und dann auch gute Bayern Tickets zu verkaufen. Neben Fussball gibt es auch andere Projekte wo wir Nginx und Passenger einsetzen.

Pin cheap purses said on Jun 01, 10:05 AM:

It is really interesting and beautiful,what you have said and described is just what I am dreaming for,I am happy to see this.

Pin Computer Repair said on Jun 14, 12:08 PM:

Thanks for the snippets of code.

Pin توبيكات said on Jun 18, 04:00 PM:

your text is nice .. thanx for u

Pin TuaTua said on Jun 19, 05:45 PM:

Well, we're using Nginx and people for football and other projects, and tickets. In German, since Bavarian COME Nginx and passengers to U. S. is very familiar Sindh. We take advantage of the script for the business pages.

Pin Tua said on Jun 21, 08:40 AM:

I think it has to serve with Ngnix approach is a good approach, but each image tag to go impage_tag helper, which is a bit 'uncomfortable, but overall is good.

Pin العاب said on Jun 22, 10:30 AM:

oh very good very good

Pin العاب said on Jun 22, 01:44 PM:

I will be checking into this website again

Pin شات said on Jun 22, 01:45 PM:

thank u my dae

Pin منتديات said on Jun 24, 11:31 PM:

thank u my dae

Pin ايجي said on Jul 01, 05:30 PM:

Cool blog, I will share it my relatives. Thanks for this info!

Pin منتديات said on Jul 03, 02:11 AM:

Very cool article a special thank you

Pin womens winter clothes said on Jul 03, 02:09 PM:

Blogs like these, I am sure, are a constant inspiration to people who are always striving to make innovations in the different languages, scripts and other areas especially in computers, to make our work easier!

Pin اعلانات مبوبه said on Jul 05, 06:04 PM:

oh very good very good

Pin Cheap ipods said on Jul 06, 08:32 PM:

Well, we're using Nginx and people for football and other projects, and tickets. In German, since Bavarian COME Nginx and passengers to U. S. is very familiar Sindh. We take advantage of the script for the business pages.

Pin Global financial crisis said on Jul 10, 04:29 AM:

useful tutorial to use static files with Nginx Passenger, thank you

Pin vehicle transport said on Jul 10, 05:33 AM:

A little while ago we started redesigning our flag-ship product, a price aggregator. We drew drafts on paper and handed them over to the graphics department to make them look good, and some time later we had working prototypes.

Pin vpills said on Jul 10, 09:05 PM:

vpills marketim.

Pin çiçekçi said on Jul 12, 05:41 AM:

Devlet Bakanı ve Başbakan Yardımcısı Cemil Çiçek, Meslek Yüksek Okulları temel atma törenlerine katılmak için geldiği Yozgat’ta devlet olarak eğitime yeteri kadar önem verilmediğini ilginç bir örnekle anlattı. Cumhurbaşkanı Abdullah Gül’ün Nijerya ziyaretine katıldığını aktaran Cemil Çiçek, orada Türk okulları olduğunu, Nijeryalıların Türkçeyi öğrendiklerini fakat, Hakkari’dekine, Diyarbakır’dakine halen Türkçe’yi öğretemediklerini söyledi. Çiçek, bu durumun devletin eğitime yeteri kadar önem vermemesinden kaynaklandığını ve bedelini de ağır ödediğini söyledi.

Pin منتديات said on Jul 14, 08:29 AM:

thanks a lot's

Pin مركز مخزن said on Jul 15, 01:52 AM:

asfsadfasdfsadf

Pin مركز تحميل صور said on Jul 15, 01:55 AM:

poihpoikjpioj

Pin wedding websites said on Jul 26, 07:29 PM:

nice thanks

Pin Nude TV said on Jul 26, 08:35 PM:

What really happens is that all static files that do exist are served by Nginx and the rest are passed on to passenger.

Pin tratamento de estrias said on Jul 26, 10:28 PM:

Very nice post and thanks for sharing it.

Pin صدفة said on Aug 05, 10:15 AM:

Pin loan software said on Aug 06, 10:28 PM:

I am working with Javascript files and I think its quite an interesting language for using as a language.

Pin door canopies said on Aug 09, 03:07 AM:

interesting. I like what you have to say about nginx

Pin rent villa said on Aug 13, 04:00 PM:

That's great , thanks for your fine article.

Pin huu said on Aug 19, 08:52 AM:

Pin farmville said on Aug 24, 09:28 AM:

Keep updating good content, I’ll just subscribe to the feed.

Pin Chicago movers said on Aug 25, 01:47 PM:

Nginx and Passenger - The two new topics that I have seen today.

Pin Chicago movers said on Aug 25, 01:47 PM:

Nginx and Passenger - The two new topics that I have seen today.

Pin شات كتابي said on Sep 03, 12:39 AM:

شات -
دردشه - دردشة -
شات كتابي -
دردشه كتابيه -
العاب فلاش

Drop a comment: