Static files with Nginx + Passenger
posted by gchatz 51 commentsNormally, 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)
Awesome site, but I can't subscribe in my news reader since your RSS feed is broken...
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 ..
Awesome site, but I can't subscribe in my news reader since your RSS feed is broken...
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.
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 ..
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.
thanks for your help...
Found it a great launch
I thank you very much
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.
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.
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.
Your spot on aa assett host is the solution but until now I did not know how to implement it.
This looks quite straight forward so I will give it a go.
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!!
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!!
very useful . thanks
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.
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.
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.
Thanks for the snippets of code.
your text is nice .. thanx for u
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.
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.
oh very good very good
I will be checking into this website again
thank u my dae
thank u my dae
Cool blog, I will share it my relatives. Thanks for this info!
Very cool article a special thank you
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!
oh very good very good
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.
useful tutorial to use static files with Nginx Passenger, thank you
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.
vpills marketim.
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.
thanks a lot's
asfsadfasdfsadf
poihpoikjpioj
nice thanks
What really happens is that all static files that do exist are served by Nginx and the rest are passed on to passenger.
Very nice post and thanks for sharing it.
http://games.sodfah.com/
I am working with Javascript files and I think its quite an interesting language for using as a language.
interesting. I like what you have to say about nginx
That's great , thanks for your fine article.
huang101 http://www.chanelsonline.com
Keep updating good content, I’ll just subscribe to the feed.
Nginx and Passenger - The two new topics that I have seen today.
Nginx and Passenger - The two new topics that I have seen today.
شات -
دردشه - دردشة -
شات كتابي -
دردشه كتابيه -
العاب فلاش
Drop a comment: