【问题标题】:Rails page cache multiple servers with nginx unicornRails 页面使用 nginx 独角兽缓存多台服务器
【发布时间】:2013-12-19 05:01:18
【问题描述】:

我在 Rails 4 上使用页面缓存 gem。我有一个运行 nginx 的 Web 服务器,运行 unicorn 和 rails 的应用服务器,以及运行 postgre 的 db 服务器。

在应用服务器上生成页面缓存时,nginx 不会提供静态文件。只有在我设置之后

config.serve_static_assets = true

页面缓存在生产环境中的作用。我认为这并不理想,尽管现在 rails 正在为静态文件提供服务。

如何让 nginx 为位于应用服务器上的页面缓存提供服务?

这是我的 nginx 配置:

upstream unicorn {
  server <%= app_private_ip %>:8080 fail_timeout=0;
}

server {

  # listen [::]:80 ipv6only=on default_server;

  listen 80 default deferred;
  server_name <%= domain %>;
  # rewrite ^(.*) https://<%= domain %>$1 permanent;


  root <%= current_path %>/public;
  sendfile on;

  if (-f $document_root/system/maintenance.html) {
    return 503;
  }
  error_page 503 @maintenance;
  location @maintenance {
    rewrite  ^(.*)$  /system/maintenance.html last;
    break;
  }

  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  try_files $uri/index.html $uri @unicorn;
  location @unicorn {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://unicorn;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 10;
  server_tokens off;

}

server {
  listen                443;
  server_name           <%= domain %>;
  ssl                   on;
  ssl_certificate       /home/<%= user %>/ssl/<%= domain %>.pem;
  ssl_certificate_key   /home/<%= user %>/ssl/<%= domain %>.key;


  root <%= current_path %>/public;
  sendfile on;

  if (-f $document_root/system/maintenance.html) {
    return 503;
  }
  error_page 503 @maintenance;
  location @maintenance {
    rewrite  ^(.*)$  /system/maintenance.html last;
    break;
  }


  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  try_files $uri/index.html $uri @unicorn;
  location @unicorn {
    proxy_set_header  X-Real-IP       $remote_addr;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header  X-Forwarded-Proto https;
    proxy_set_header  Host $http_host;
    proxy_redirect    off;
    proxy_pass        http://unicorn;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 10;
  server_tokens off;
}

【问题讨论】:

    标签: ruby-on-rails caching nginx unicorn


    【解决方案1】:

    nginx try_files 指令允许您设置级联方式来解析不同位置/后端的 URI 的静态文件。

    看起来您需要将您的 try_files 指令移动到 location 块中才能正常工作:

    location / {
      try_files $uri @unicorn;
    }
    

    这应该告诉 nginx 在将请求传递到您的独角兽后端之前尝试通过 URI 本地解析路径。

    【讨论】:

      【解决方案2】:

      页面缓存 gem 要求您将缓存目录设置为“public/cache” 应用程序.rb;

      config.action_controller.page_cache_directory = "#{Rails.root.to_s}/public/cache"
      

      所以你的 try_files 行应该是;

      try_files /cache/$uri/index.html /cache/$uri @unicorn;
      

      否则,您可以将 page_cache_directory 设置为; "#{Rails.root.to_s}/public" 并且不要更改您当前的 nginx 配置。

      【讨论】:

        猜你喜欢
        • 2011-01-27
        • 2011-08-08
        • 2015-09-27
        • 1970-01-01
        • 2012-03-15
        • 1970-01-01
        • 2011-12-01
        • 2020-10-17
        • 2013-02-20
        相关资源
        最近更新 更多