views:

3783

answers:

4

I've worked my way through a number of interesting routing problems - turning a request URL into a hash, etc., but just out of curiosity, is there a way to tell the routing system that you want anything that comes under a certain url subpath to be served literally - without going through a controller?

For instance, if I have /home/me/public_html/rails_proj/images/foo.jpg, and .../rails_proj/images/other/bar.jpg, can I insert a route that says "anything under images should just be served as an object of the default mime type?"

Might be interesting.

+5  A: 

If you put the "images" directory into the "public" folder of the Rails app (for example: /public/images/) then you shouldn't have any problems with MIME types unless your web server is configured wrongly.

According to your examples, you want the images dir in the root of the app. I don't think there is a way though Rails to make those images visible, but if you really wanted to you could use mod_rewrite to make it work. Once again, it would be up to the web server to make sure the images had the correct MIME type.

Nick
I'm fine with moving images to the public folder. I'll do some googling around - placing assets there is still giving me a "No route matches ............. with {:method=>:get}
The problem with the public folder is I was attempting the url /public/... I jettisoned the explicit use of /public/ and it worked just fine.
+1  A: 

Things that are served out of the public directory will not go through Rails - they'll just be handled by your server (probably apache). The only reason why you would need to serve images through the rails system is if you wanted some kind of control on who could access them. Just put everything else in public and access ala: siteurl.whatever/images/*.jpg

Allyn
A: 

Caveat: if your request URL matches a static resource WEBrick, mongrel or whatever will happily serve it. At any cost, you don't want this in production: if your traffic is high enough your app will be brought to its knees just because its mongrels will be busy serving static content.

So make sure that your web server is properly configured for all kinds of static content as the previous commenters have pointed out.

pantulis
+1  A: 

I typically use nginx as a frontend and Apache/Passenger as a backend. Ngingx proxies all Rails requests to Apache but handles all static content itself. Check out the examples on the English nginx wiki. Here is a small excerpt for nginx config:

server {
    listen 80;
    server_name www.domain.com;
    location ~* \.(jpg|jpeg|gif|png|ico|css|bmp|js)$ {
        root   /path/to/static/assets/dir;
    }
    location / {
        proxy_pass http://127.0.0.1:81;
    }
}

So have apache listen on port 81 to handle Rails requests proxied by nginx and let nginx deliver static content. Not only is nginx supposedly faster than Apache at delivering static content, but this also offloads your Rails application for every image, stylesheet, javascript or whatever other static content.

ronaldevers