Rails资产子域服务主页面

时间:2017-04-17 13:55:33

标签: ruby-on-rails nginx

使用Rails 4.3。我在production.rb中有以下行:

#启用资产服务器中的图像,样式表和JavaScripts服务。   config.action_controller.asset_host =" https://assets.example.com"

以下example.conf nginx:

upstream example {
  server unix:/home/deployer/example/shared/tmp/sockets/puma.sock fail_timeout=0;
}

server {
  listen 80;
  server_name example.com;

  client_max_body_size 4G;
  keepalive_timeout 10;

  error_page 500 502 503 504 /500;

  root /home/deployer/example/current/public;

  try_files $uri/index.html $uri.html $uri @example;

  location @example {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    proxy_pass http://example;
  }

  location = /50x.html {
    root html;
  }

  location = /404.html {
    root html;
  }

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

  if ($request_method !~ ^(GET|HEAD|PUT|PATCH|POST|DELETE|OPTIONS)$ ){
    return 405;
  }

  if (-f $document_root/system/maintenance.html) {
    return 503;
  }
}

我注意到我的子域http://assets.example.com是我的主域https://example.com的镜像,而不仅仅是提供资产。例如,https://assets.example.com/blog/1https://www.example.com/blog/1相同。

我该如何预防?我只想要https://assets.example.com来提供静态资产。

1 个答案:

答案 0 :(得分:1)

我认为你应该将你的nginx server部分分成两部分,一部分用于网络,另一部分用于静态资产网站。

网络的服务器部分应与原始帖子基本相同,它应该只对完整的主机名作出反应:

server_name www.example.com;

静态站点服务器部分应该是主站点部分的修订副本,具有以下主要区别:

  • server_name应包含assets.example.com
  • 部分应包含NO proxy_pass指令,静态资产应默认提供,即直接由nginx提供,而不必通过Rails
  • root将与主网站相同
  • 资产文件将直接从root提供,相反,如果您的网址中存在/assets/(可能存在),则应直接从您的资源中找到并提供资源public/assets物理目录。

更新:示例nginx配置:

upstream example {
  server unix:/home/deployer/example/shared/tmp/sockets/puma.sock fail_timeout=0;
}

# main site config
server {
  listen 80;
  server_name www.example.com;

  client_max_body_size 4G;
  keepalive_timeout 10;

  error_page 500 502 503 504 /500;

  root /home/deployer/example/current/public;

  try_files $uri/index.html $uri.html $uri @example;

  location @example {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    proxy_pass http://example;
  }

  location = /50x.html {
    root html;
  }

  location = /404.html {
    root html;
  }

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

  if ($request_method !~ ^(GET|HEAD|PUT|PATCH|POST|DELETE|OPTIONS)$ ){
    return 405;
  }

  if (-f $document_root/system/maintenance.html) {
    return 503;
  }
}

server {
  listen 80;
  server_name assets.example.com;

  client_max_body_size 4G;
  keepalive_timeout 10;

  root /home/deployer/example/current/public;

  location = /404.html {
    root html;
  }

  if ($request_method !~ ^(GET|HEAD|PUT|PATCH|POST|DELETE|OPTIONS)$ ){
    return 405;
  }

  if (-f $document_root/system/maintenance.html) {
    return 503;
  }
}