Why can't nginx find my assets?

廉价感情. 提交于 2019-12-10 16:06:53

问题


I'm on rails 3.2 and my production setup is using nginx and unicorn.

I have a problem with some assets that a ruby gem called sidekiq uses. However those assets are not being served properly when I request them. My nginx config looks like this:

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

server {
  listen 80 default deferred;
  # server_name example.com;
  root /home/deployer/apps/myapp/current/public;

  if (-f $document_root/system/maintenance.html) {
    return 503;my
  }
  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;

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

From my browser I can see that it's
e.g. requesting http://www.myapp.com/admin/sidekiq/stylesheets/application.css.

If I ssh into the server and write:

ls /home/deployer/apps/myapp/current/public/admin/sidekiq/stylesheets
application.css  bootstrap.css

You can see that it's actually there. So why isn't it being served?.


回答1:


Solved it, It was all due to a wrong setup in my production.rb in rails, which made the default behavior fail, so the hack of putting the assets into /public manually isn't necessary anyways.

I had:

config.action_dispatch.x_sendfile_header = "X-Sendfile"

Which instead for nginx should be:

config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'

Thanks for all the help :-)




回答2:


Sorry, Niels, I think I am going to need more information in order to help you. Could you please enable logging, and then post the logged responses into your question.

To enable logging, please include the following lines in your config, and then either reload the config, or restart NginX.

    log_format my_log_format
        '$host $remote_addr - $remote_user [$time_local]  $status '
        '"$request" $body_bytes_sent "$http_referer" '
        '"$http_user_agent" "$http_x_forwarded_for"';

    error_log  /home/deployer/myapp_error.log;
    access_log /home/deployer/myapp_access.log my_log_format;

I will revise my answer according to what we find in the logs.



来源:https://stackoverflow.com/questions/13953454/why-cant-nginx-find-my-assets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!