Are you eager to elevate your security skills and safeguard your applications
against cyber threats? I created a Rails Security course is designed
specifically for developers like you who aim to build robust, secure Rails
applications!
Buy my course: Security for Rails Developers.
The built-in page caching has been extracted to a separate gem in Rails 4 and here is a guide how to use it.
First we need to add the gem to our Gemfile:
Buy my course: Security for Rails Developers.
gem 'actionpack-page_caching'
class ApplicationController < ActionController::Base
include ActionController::Caching::Pages
self.page_cache_directory = "#{Rails.root.to_s}/public/page_cache"
end
class ArticleController < ApplicationController
caches_page :index, :show
# Rest of the file omitted.
end
upstream puma_server_domain_tld {
server unix:/path/to/the/puma/socket;
}
server {
listen 80;
server_name domain.tld;
root /path/to/the/app;
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
# try the $uri, than the uri inside the cache folder, than the puma socket
try_files $uri /page_cache/$uri /page_cache/$uri.html @puma;
}
location @puma{
proxy_pass http://puma_server_domain_tld;
break;
}
# set the expire date to max for assets
location ~ "^/assets/(.*/)*.*-[0-9a-f]{32}.*" {
gzip_static on;
expires max;
add_header Cache-Control public;
}
}
gem 'rails-observers'
class ArticleSweeper < ActionController::Caching::Sweeper
observe Article
def after_save(record)
expire_page(articles_path)
expire_page("/#{record.url}")
end
end
Or follow me on Twitter
I run an indie startup providing vulnerability scanning for your Ruby on Rails app.
It is free to use at the moment, and I am grateful for any feedback about it.If you would like to give it a spin, you can do it here: Vulnerability Scanning for your Ruby on Rails app!