Question

I have a rails 3.0 app, and it has a good amount of traffic, the app it's running through the combination of Nginx and Unicorn. The thing is that unicorn and it's workers consume a lot of resources, and because of the nature of my app, a lot of records are pulled from the database, then it's like serving almost static files generated with those database records

I was wondering if you can generate this kind of static files, cache them, serve them through nginx instead of the app through unicorn to use less resources and kind of reloading the cache after 1000 request

I'm beginning my research about that, I don't know a lot of server configuration, so I hope you guys have any advise for me, it would be great!

thanks!

Was it helpful?

Solution

I assume you mean How do I serve my static assets from nginx rather than Unicorn

I just solved this problem and here is a snippet of my nginx.conf

# Prefer to serve static files directly from nginx to avoid unnecessary
# data copies from the application server.
try_files $uri/index.html $uri.html $uri @app;

# Set Far Future Cache on Static Assets
# All requests starting with /xyz/ where xyz is 
# one of the options below (~* == case insensitive)
location ~* ^/(images|javascripts|stylesheets)/ {
    # Per RFC2616 - 1 year maximum expiry
    # http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
    expires 1y;
    add_header Cache-Control public;

    # Some browsers still send conditional-GET requests if there's a
    # Last-Modified header or an ETag header even if they haven't
    # reached the expiry date sent in the Expires header.
    add_header Last-Modified "";
    add_header ETag "";
    break;
}

location @app { ... }

I am using Rails 3.0.10 so you my need something like ^/assets/ instead. The ~* directive tells nginx to do a case in-sensitive reg-ex compare. Also you don't need to escape the backslashes as you would in other languages.

Here is the Nginx documentation on that: http://wiki.nginx.org/HttpCoreModule#location

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top