Nginx 404(未找到)仅适用于AWS上的rails图像

时间:2017-08-30 12:15:31

标签: ruby-on-rails amazon-web-services nginx

我在生产模式下测试我的应用程序并遇到一个奇怪的问题。图片资产未在网页上加载。所有的css和javascript都正常加载,但任何图片都会从nginx获得404 (Not Found)。我用Docker将我的应用程序推送到AWS服务。当我预编译资产时,图像包含在输出中,因此它们应该在那里:

I, [2017-08-30T12:10:50.092655 #7]  INFO -- : Writing /livingrecipe/public/assets/default-user-d39e051d757a0f45602b6065147cec0a307c6a2a070834f6fe6fc2b0fc914207.jpg
I, [2017-08-30T12:10:50.104846 #7]  INFO -- : Writing /livingrecipe/public/assets/default_recipe-be8d509a8e34dfec514c097b119753d4aaadbfd1dd6747e3c868ed454b8a36b3.jpg
I, [2017-08-30T12:10:50.108486 #7]  INFO -- : Writing /livingrecipe/public/assets/edit-4e381318428d71d55ed0cbdc32b4ce99db6622140c6e7c751ae0f1bfb40d3963.png
I, [2017-08-30T12:10:50.111606 #7]  INFO -- : Writing /livingrecipe/public/assets/favorite-91432751e505915d80cf4e24d7a8d0abd3493d05d2d447c1cebb055d99757ac8.png
I, [2017-08-30T12:10:50.115720 #7]  INFO -- : Writing /livingrecipe/public/assets/icon_email-203dcd7bdcf7559d742f76a9d8c21e98694d45b9556b29ab9ce286a7dd37af11.png
I, [2017-08-30T12:10:50.118457 #7]  INFO -- : Writing /livingrecipe/public/assets/icon_facebook-0140437ec6cb29bc6ac6f8505f05f11603a430298e48c1ea483f7de390ba34a9.png
I, [2017-08-30T12:10:50.121393 #7]  INFO -- : Writing /livingrecipe/public/assets/icon_google-2a98026430dddc53539d283c04a2ad4b50536c93d0becadb03f3f61443e52c9c.png
I, [2017-08-30T12:10:50.125228 #7]  INFO -- : Writing /livingrecipe/public/assets/star-half-db15fb9b3561d5c741d8aea9ef4f0957bd9bc51aa1caa6d7a5c316e083c1abd5.png
I, [2017-08-30T12:10:50.127714 #7]  INFO -- : Writing /livingrecipe/public/assets/star-off-6aaeebdaab93d594c005d366ce0d94fba02e7a07fd03557dbee8482f04a91c22.png
I, [2017-08-30T12:10:50.130083 #7]  INFO -- : Writing /livingrecipe/public/assets/star-on-fd26bf0ea0990cfd808f7540f958eed324b86fc609bf56ec2b3a5612cdfde5f5.png
I, [2017-08-30T12:10:50.132442 #7]  INFO -- : Writing /livingrecipe/public/assets/unfavorite-7000a53c81f55cf94c88031a8529f847e5a6abd7460e0e54ae0b581b0cde85a3.png

这是我的nginx的default.conf文件:

upstream PLACEHOLDER_BACKEND_NAME {
  # The web app.
  server PLACEHOLDER_BACKEND_NAME:PLACEHOLDER_BACKEND_PORT;
}

# Redirect 'www' addresses to the non-www version, and also take care of
# redirects to HTTPS at the same time.
server {
  listen 80;
  server_name www.PLACEHOLDER_VHOST;
  return 301 https://$host$request_uri;
}

server {
  # "deferred" reduces the number of formalities between the server and client.
  listen 80 default deferred;
  server_name PLACEHOLDER_VHOST;

  # Static asset path, which is read from the PLACEHOLDER_BACKEND_NAME
  # container's VOLUME.
  root /PLACEHOLDER_BACKEND_NAME/public;

  # Serve static assets.
  #
  # gzip_static is enabled because the assets are already gzipped to a maximum
  # level due to Rail's asset pipeline.
  #
  # Add headers to set the maximum amount of cache time.
  #
  # We can do this because the Ruby on Rails asset pipeline md5 hashes all of
  # the file names for us. When a file changes, its md5 will change, and the
  # cache will be automatically busted. Other frameworks can do this as well.
  location ~ ^/assets/ {
    gzip_static on;

    # Set a maximum cache time period and null out a few headers to address
    # certain browsers from occasionally requesting cached content.
    expires max;
    add_header Cache-Control public;
    add_header Last-Modified "";
    add_header ETag "";
  }

  # Ensure timeouts are equal across browsers.
  keepalive_timeout 60;

  # Disallow access to hidden files and directories.
  location ~ /\. {
    return 404;
    access_log off;
    log_not_found off;
  }

  # Allow optionally writing an index.html file to take precedence over the upstream.
  try_files $uri $uri/index.html $uri.html @PLACEHOLDER_BACKEND_NAME;

  # Attempt to load the favicon or fall back to status code 204.
  location = /favicon.ico {
    try_files /favicon.ico = 204;
    access_log off;
    log_not_found off;
  }

  # Force SSL connections on agents (browsers) who support this header.
  add_header Strict-Transport-Security "max-age=31536000; includeSubDomains;";

  # Load the web app back end with proper headers.
  location @PLACEHOLDER_BACKEND_NAME {
    proxy_set_header Host $http_host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Ssl on;
    proxy_redirect off;

    if ($http_x_forwarded_proto = "http") {
      return 301 https://$host$request_uri;
    }

    proxy_pass http://PLACEHOLDER_BACKEND_NAME;
  }
}

2 个答案:

答案 0 :(得分:1)

我将从经验中回答,尽管我遇到了与Nginx + Django相同的问题。保存我的情况的是给一个运行服务器的用户(可能是www-data,在我的情况下是我)访问图像的权限。是的,它听起来很愚蠢,因为它对其他文件有权限,但这有帮助。尝试

sudo chmod -R 750 /root/of/website

这将以递归方式向该目录中的所有文件授予权限750。希望它有所帮助!

注意:这可能不是问题,所以如果它没有用,请不要downvote。

答案 1 :(得分:0)

所以引用资产的错误方法是一个简单的错误。在轨道我使用这个:

<%= asset_path('/assets/image.png') %>

在开发模式下仍然显示资产,但在生产中它无法找到它,因为它已经添加了资产文件夹,因此查看资产/资产/图像。将所有内容更改为:

<%= asset_path('image.png') %>

就像应该的那样,它运作得很好。