nginx不会提供我的所有资产(Ruby on Rails 4)

时间:2014-02-09 18:59:44

标签: ruby-on-rails nginx ruby-on-rails-4

我有一个在unicorn / nginx下运行的Ruby on Rails应用程序。问题是nginx不会提供我的所有资产。 CSS&似乎加载了JS文件,但没有提供图像。

这是我的nginx conf文件:

upstream unicorn {
  server unix:/tmp/unicorn.aiccrr.sock fail_timeout=0;
}

server {
  listen 80 default deferred;
  server_name exemple.com;

  root /home/aiccrr/aiccrr/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;
  }

  location ~ \.(js|css|png|jpg|jpeg|gif|ico|html)$ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 1G;
  keepalive_timeout 10;
}

unicorn.rb文件:

root = "/home/aiccrr/aiccrr"

working_directory root
pid "#{root}/tmp/pids/unicorn.pid"
stderr_path "#{root}/log/unicorn.log"
stdout_path "#{root}/log/unicorn.log"

listen "/tmp/unicorn.aiccrr.sock"
worker_processes 2
timeout 30

我做了rake资产:今天至少预编译10次并将此行添加到production.rb:

config.assets.precompile += %w[*.png *.jpg *.jpeg *.gif]

我的想法已经不多了。你有什么想法吗?

提前谢谢。

1 个答案:

答案 0 :(得分:0)

尝试以下方法:

禁用Rails资产服务器

# config/production.rb
# Disable Rails's static asset server (Apache or nginx will already do this).
  config.serve_static_assets = false

尝试对你的`nginx.conf

进行更改
upstream unicorn {
  server unix:/tmp/unicorn.aiccrr.sock fail_timeout=0;
}

server {
  listen 80 default deferred;
  server_name exemple.com;

  location / {
    root /home/aiccrr/aiccrr/public;

    location ~ \.(js|css|png|jpg|jpeg|gif|ico|html)$ {
      expires max;
      gzip_static on;
      add_header Cache-Control public;
      break;
    }

    # If the file exists as a static file serve it directly without
    # running all the other rewrite tests on it
    if (-f $request_filename) {
      break;
    }

    if (!-f $request_filename) {
      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 1G;
  keepalive_timeout 10;
}

如果仍然无效,请回复日志文件&的结果。也是示例nginx的URL,您可以在其中复制此问题。感谢