Rails assets missing after Capistrano deploy

2019-04-09 09:13发布

I'm building a VPS, and it's deployed via Capistrano, database connected etc, but there are no assets available to the page - it is basic html only.

The assets appear to be compiled, and exist in the shared directory.

From the page html:

<link href="/assets/application-a1b5d69aeaff709fd3dce163c559b38b.css" media="all" rel="stylesheet" type="text/css" />
<script src="/assets/application-0ebd07089629f8c6880dddfb688d525d.js" type="text/javascript"></script>

The asset files appear to exist in the shared directory:

assay@assaypipeline:~/apps/assay/shared/assets$ ls application-  a1b5d69aeaff709fd3dce163c559b38b.css
application-a1b5d69aeaff709fd3dce163c559b38b.css

When I view, source and then click on the hyperlink to the asset path, I get a 404 not found from Nginx.

SOLUTION

Thanks to Martin M (accepted answer) for help. The steps I took, from the ~/apps/(app name)/current directory on the server.

$ bundle install
$ RAILS_ENV=production bundle exec rake assets:precompile
$ sudo service nginx restart

Obviously it would be better to include this in the Capistrano recipe.

*EDIT - Capfile *

load 'deploy'
load 'deploy/assets'
load 'config/deploy'

2条回答
我命由我不由天
2楼-- · 2019-04-09 09:39

Your precompiled assets should reside in public/assets, see rails guides
normally you create them by running

RAILS_ENV=production bundle exec rake assets:precompile

as part of your deployment.
The shared stuff is to provide old stuff over several deploys.

See also this question

查看更多
Ridiculous、
3楼-- · 2019-04-09 10:00

The problem might not be in asset compilation and deployment. Try changing nginx root /home/deploy/app_name/public; to /home/deploy/app_name/current/public; in nginx configuration file /etc/nginx/sites-enabled/default.

sudo nano /etc/nginx/sites-enabled/default

following is my configuration file

upstream app {
  # Path to Puma SOCK file, as defined previously
  server unix:/home/deploy/app_name/shared/tmp/sockets/puma.sock fail_timeout=0;
}
server {
  listen 80;
  server_name localhost;

  root /home/deploy/app_name/current/public;

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

  location / {
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $host;
    proxy_redirect off;
    proxy_http_version 1.1;
    proxy_set_header Connection '';
    proxy_pass http://app;
  }

  location ~ ^/(assets|fonts|system)/|favicon.ico|robots.txt {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

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

Hope this helps

查看更多
登录 后发表回答