Ruby on rails 3 - 为什么 NGinX 没有重定向到我的缓存文件?



我有一个带子干线的rails应用程序。

我已经更改了应用程序中的默认缓存位置,以便将模板写入:

[app_dir]/public/cache/[subdomain]/[path]

因此,请求:

http://france.mysite.com/recipes.html

将写入:

[my_app]/public/cache/france/recipes.html

工作正常,文件正在写入服务器上的正确位置。

我的问题是NGinx没有提供这些缓存文件。

我在NGinx配置中添加了以下内容:

    # catch requests to www and remove the 'www'
    server {
      server_name www.mysite.com;
      rewrite ^ $scheme://mysite.com$request_uri permanent;
    }

    server {
      server_name mysite.com *.mysite.com;
      access_log /home/deploy/mysite/staging/current/log/access.log;
      error_log /home/deploy/mysite/staging/current/log/error.log;
      root   /home/deploy/mysite/staging/current/public;
      passenger_enabled on;
      rails_env staging;
      client_max_body_size 400M;
      client_body_buffer_size 128k;
        if (-f $request_filename) {
          break;
        }
        # Check / files with index.html
        if (-f $document_root/cache/$host/$uri/index.html) {
          rewrite (.*) /cache/$host/$1/index.html break;
        }
        # Check the path + .html
        if (-f $document_root/cache/$host/$uri.html) {
          rewrite (.*) /cache/$host/$1.html break;
        }
        # Check directly
        if (-f $document_root/cache/$host/$uri) {
          rewrite (.*) /cache/$host/$1 break;
        }

    }

有人能指出我哪里错了吗

所以我能够解决这个问题。。。

NGinx中的$host变量指的是整个主机(我错误地将其视为子域:/(

两种解决方法:

a( 更改缓存目录以匹配完整的主机名:http://france.mysite.com=>/public/cache/france.mysite.com

b( 从主机上获取子域,并在if块中使用它:

server {
  server_name www.mysite.com;
  rewrite ^ $scheme://mysite.com$request_uri permanent;
}

server {
  server_name mysite.com *.mysite.com;
  access_log /home/deploy/mysite/staging/current/log/access.log;
  error_log /home/deploy/mysite/staging/current/log/error.log;
  root   /home/deploy/mysite/staging/current/public;
  passenger_enabled on;
  rails_env staging;
  client_max_body_size 400M;
  client_body_buffer_size 128k;
  # if the maintenance file exists, redirect all requests to it
  if (-f $document_root/system/maintenance.html) {
    rewrite ^(.*)$ /system/maintenance.html break;
  }
  # if the host has a subdomain, set $subdomain
  if ($host ~* "(.*).mysite.com"){
    set $subdomain $1;
  }
  # Rewrite index.html.
  if (-f $document_root/cache/$subdomain/$uri/index.html) {
    rewrite ^(.*)$ /cache/$subdomain/$uri/index.html break;
  }
  # Rewrite other *.html requests.
  if (-f $document_root/cache/$subdomain/$uri.html) {
    rewrite ^(.*)$ /cache/$subdomain/$uri.html break;
  }
  # Rewrite everything else.
  if (-f $document_root/cache/$subdomain/$uri) {
    rewrite ^(.*)$ /cache/$subdomain/$uri break;
  }
}

我选择了选项b(,因为我觉得它更整洁

相关内容

  • 没有找到相关文章

最新更新