LoginSignup
6
3

More than 5 years have passed since last update.

RailsにNginxのproxy cacheを設定する

Posted at

railsにnginxのproxy cacheをしたくなったので、「rails nginx proxy cache」とググって以下のサイトを参考にしたが、どうしてもキャッシュができなかったので記載する

[ バージョン情報 ]
nginx: 1.10.2
rails: 4.2.6

[ 参考サイト]
https://mattbrictson.com/nginx-reverse-proxy-cache
http://www.mk-mode.com/octopress/2014/04/23/nginx-proxy-cache-setting/

nginxでproxy cacheを設定する

先に正解を掲載。
後に掲載する参考サイトとの差分は
1. proxy_ignore_headers "Set-Cookie" "Cache-Control"; でSet-Cookieヘッダーも無視している
2. proxy_cache_validを設定している

http {
  proxy_cache_path  /var/cache/nginx levels=1:2 keys_zone=default:8m max_size=1000m inactive=30d;
  proxy_temp_path   /var/cache/nginx/tmp;
}

upstream rails {
  server unix:/path/to/.unicorn.sock fail_timeout=0;
}

server {
  listen 80 default deferred; # for Linux
  root /path/to/app/current/public;
  try_files $uri/index.html $uri.html $uri @app;

  location @app {
    # Standard reverse proxy stuff
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://rails;

    # Reverse proxy cache
    proxy_ignore_headers "Set-Cookie" "Cache-Control";
    proxy_cache default;
    proxy_cache_lock on;
    proxy_cache_use_stale updating;
    proxy_cache_valid  any      10m;
    add_header X-Cache-Status $upstream_cache_status;
  }

  error_page 500 502 503 504 /500.html;
  location = /500.html {
    root /path/to/app/current/public;
  }
}

参考サイトの設定

参考: https://mattbrictson.com/nginx-reverse-proxy-cache

nginxの設定

http {
  proxy_cache_path  /var/cache/nginx levels=1:2 keys_zone=default:8m max_size=1000m inactive=30d;
  proxy_temp_path   /var/cache/nginx/tmp;
}

upstream rails {
  server unix:/path/to/.unicorn.sock fail_timeout=0;
}

server {
  listen 80 default deferred; # for Linux
  root /path/to/app/current/public;
  try_files $uri/index.html $uri.html $uri @app;

  location @app {
    # Standard reverse proxy stuff
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://rails;

    # Reverse proxy cache
    proxy_cache default;
    proxy_cache_lock on;
    proxy_cache_use_stale updating;
    add_header X-Cache-Status $upstream_cache_status;
  }

  error_page 500 502 503 504 /500.html;
  location = /500.html {
    root /path/to/app/current/public;
  }
}

railsの設定

rails
class MyController < ApplicationController
  before_action :allow_page_caching

  # controller actions...

  private

  def allow_page_caching
    expires_in(5.minutes) unless Rails.env.development?
  end
end

解説

参考サイトにおいて、railsではデフォルトでレスポンスヘッダーに

Cache-Control: max-age=0, private, must-revalidate

が付与されてしまう。なので、rails側で expires_in(5.minutes) とすることで

Cache-Control: max-age=300, public

に変更している
nginxで

proxy_ignore_headers Cache-Control;

としてもよい。すると、Railsが何を返そうが、キャッシュされるようになるはずだった。

しかし、どうやらrailsは毎回Set-Cookieも返すらしい。そいつも無視しておかないとキャッシュが設定されないそうだ。
そして、nginxのバージョン差異のせいか、 proxy_cache_valid も設定しておかないとダメみたいだ。

もっと詳しい情報をお持ちの方、ぜひ教えてください。
よろしくお願いします。

6
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
3