rrichards
12/23/2013 - 6:24 AM

refinerycms_nginx_proxy_cache

Refinery CMS:
Caching images generated by Dragonfly with Nginx cache_proxy instead of Rack:Cache

Before:
Static image served by Nginx: 4409.40 req/sec (mean)
Dragonfly generated images from Rack:Cache: 286.66 req/sec (mean)

After:
Dragonfly generated images from Nginx proxy_cache: 4099 req/sec (mean)


Static file, Nginx:
100Kb file
ab -n 2000 -c 500 http://ecavbeckov.local/assets/banners/ecav_beckov_banner_bg-e33e778bbee7f14148349165da37ba03.jpg

Concurrency Level:      500
Time taken for tests:   0.454 seconds
Complete requests:      2000
Failed requests:        0
Requests per second:    4409.40 [#/sec] (mean)
Time per request:       113.394 [ms] (mean)
Time per request:       0.227 [ms] (mean, across all concurrent requests)

-----
Rack:Cache

ab -n 2000 -c 500 http://ecavbeckov.local/system/images/BAhbBlsHOgZmSSJXMjAxMi8wOS8wNi8yMF8yN18zNl85NjdfZWNhdl9iZWNrb3ZfYmFubmVyX2JnX2UzM2U3NzhiYmVlN2YxNDE0ODM0OTE2NWRhMzdiYTAzLmpwZwY6BkVU/ecav_beckov_banner_bg-e33e778bbee7f14148349165da37ba03.jpg

Concurrency Level:      500
Time taken for tests:   1.239 seconds
Complete requests:      2000
FAILED REQUESTS:        1754
   (Connect: 0, Receive: 0, Length: 1754, Exceptions: 0)
Write errors:           0
Non-2xx responses:      1754
Requests per second:    1613.96 [#/sec] (mean)
Time per request:       309.797 [ms] (mean)
Time per request:       0.620 [ms] (mean, across all concurrent requests)

----
Without failing requests:

Concurrency Level:      149
Time taken for tests:   6.977 seconds
Complete requests:      2000
Failed requests:        0
Write errors:           0
Total transferred:      206916000 bytes
HTML transferred:       205688000 bytes
Requests per second:    286.66 [#/sec] (mean)
Time per request:       519.777 [ms] (mean)
Time per request:       3.488 [ms] (mean, across all concurrent requests)

=> 15times slower than static file serving with Nginx
------------------


USING Nginx proxy_cache:

sudo mkdir /opt/nginx/cache
sudo chown nobody:root /opt/nginx/cache
sudo mkdir /opt/nginx/cache/dragonfly
sudo chown nobody:root /opt/nginx/cache/dragonfly

Then configure nginx.conf (https://groups.google.com/forum/?fromgroups=#!searchin/dragonfly-users/nginx$20cache/dragonfly-users/-E3b5znLwek/7LXbnt7DGrwJ )

http { 
    passenger_root /usr/local/lib/ruby/gems/1.9.1/gems/ 
passenger-3.0.7; 
    passenger_ruby /usr/local/bin/ruby; 

    server { 
        listen 127.0.0.1:8080; 
        server_name localhost; 

        root /YOUR_APP/public; 
        passenger_enabled on; 
    } 

    proxy_cache_path /opt/nginx/cache/dragonfly levels=2:2 
keys_zone=dragonfly:100m inactive=30d max_size=1g; 

    server { 
        listen 80; 
        server_name YOUR_DOMAIN; 

        location / { 
            proxy_pass http://127.0.0.1:8080; 
	    proxy_set_header Host $host;
        }

	location /assets { 
            proxy_pass http://127.0.0.1:8080; 
            proxy_cache dragonfly; 
            proxy_cache_valid      200  30d; 
        }  

        location /system/images { 
            proxy_pass http://127.0.0.1:8080; 
            proxy_cache dragonfly; 
            proxy_cache_valid      200  30d; 
        } 
    } 
}

See Nginx documentation for more info: http://wiki.nginx.org/NginxHttpProxyModule

RESULTS:

ab -n 2000 -c 500 http://ecavbeckov.local/system/images/BAhbBlsHOgZmSSJXMjAxMi8wOS8wNi8yMF8yN18zNl85NjdfZWNhdl9iZWNrb3ZfYmFubmVyX2JnX2UzM2U3NzhiYmVlN2YxNDE0ODM0OTE2NWRhMzdiYTAzLmpwZwY6BkVU/ecav_beckov_banner_bg-e33e778bbee7f14148349165da37ba03.jpg

Concurrency Level:      500
Time taken for tests:   0.488 seconds
Complete requests:      2000
Failed requests:        0
Write errors:           0
Total transferred:      206812000 bytes
HTML transferred:       205688000 bytes
Requests per second:    4099.44 [#/sec] (mean)
Time per request:       121.968 [ms] (mean)
Time per request:       0.244 [ms] (mean, across all concurrent requests)