Images not showing after deployment with Passenger

2019-07-24 17:41发布

问题:

Hi I'm deploying my first Rails app to Ubuntu 16 server using Capistrano everything went smooth except the images are not showing in the production environment.

On the production server the images are located in this path : /myapp/current/public/assets

But if I look at this in the browser my broken images links gives me this(see picture), this is a broken link for the header image.

the strange thing is that there is a .svgfile in the /myapp/current/public/assets which is showing up perfectly in the browser, In the picture below is the path shown

this is my Capfile

# Load DSL and set up stages
 require "capistrano/setup"

# Include default deployment tasks
require "capistrano/deploy"


set :rbenv_type, :user # or :system, depends on your rbenv setup
set :rbenv_ruby, '2.3.1'


require 'capistrano/rbenv'

require 'capistrano/bundler'
require 'capistrano/rails'


# Load custom tasks from `lib/capistrano/tasks` if you have any defined
Dir.glob("lib/capistrano/tasks/*.rake").each { |r| import r }

This is the config/deploy.rb

# config valid only for current version of Capistrano
lock '3.6.1'

set :application, 'myapp'
set :repo_url, 'git@github.com:DadiHall/myapp.git'


 # Default deploy_to directory is /var/www/my_app_name
 set :deploy_to, '/home/deploy/myapp'


 set :linked_files, %w{config/database.yml config/secrets.yml}
 set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}

 namespace :deploy do

 desc 'Restart application'
 task :restart do
  on roles(:app), in: :sequence, wait: 5 do
   execute :touch, release_path.join('tmp/restart.txt')
  end
 end

 after :publishing, 'deploy:restart'
 after :finishing, 'deploy:cleanup'

end

Here is the environments/production.rb

Rails.application.configure do

config.cache_classes = true
config.consider_all_requests_local       = false
config.action_controller.perform_caching = true
config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?
config.assets.js_compressor = :uglifier


 # Do not fallback to assets pipeline if a precompiled asset is missed.
 config.assets.compile = false

 config.assets.digest = true
 config.assets.initialize_on_precompile = false

 # `config.assets.precompile` and `config.assets.version` have moved to config/initializers/assets.rb


 config.log_level = :debug

 config.i18n.fallbacks = true

 config.active_support.deprecation = :notify

 config.log_formatter = ::Logger::Formatter.new

 config.active_record.dump_schema_after_migration = false

Braintree::Configuration.environment = :sandbox
Braintree::Configuration.merchant_id = ENV['merchant_id']
Braintree::Configuration.public_key = ENV['public_key']
Braintree::Configuration.private_key = ENV['private_key']

end

In the /etc/nginx/sites-enabled/default I have following lines

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    server_name mydomain.com;
    passenger_enabled on;
    rails_env    production;
    root         /home/deploy/myapp/current/public;

    # redirect server error pages to the static page /50x.html
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}

this is the nginx Error log

 [ 2016-09-28 06:25:02.4500 1594/7f900ee89700 age/Sha/ApiServerUtils.h:794 ]:  Log file reopened.
 [ 2016-09-28 09:45:43.7508 1597/7f2326502700 age/Cor/CoreMain.cpp:819 ]:   Checking whether to disconnect long-running connections for process 1978,  application /home/deploy/hlinreykdal/current/public (production)
App 21337 stdout: 
App 21405 stdout: 
[ 2016-09-28 10:30:31.0631 1597/7f2326502700 age/Cor/CoreMain.cpp:819 ]:  Checking whether to disconnect long-running connections for process 21405,  application /home/deploy/hlinreykdal/current/public (production)
App 23240 stdout: 
App 23308 stdout: 
[ 2016-09-28 10:41:40.1769 1597/7f2326502700 age/Cor/CoreMain.cpp:819 ]: Checking whether to disconnect long-running connections for process 23308, application /home/deploy/hlinreykdal/current/public (production)
App 24329 stdout: 
App 24397 stdout: 

I have tried bundle exec rake assets precompile with out any luck.

I have deployed and restarted nginx again and again, with out any luck

I have tried almost every answer to similar questions here on stack overflow, but nothing seems to work.

Am I missing something here?

I'm sure this problem has something to do with the assets pipeline, but I'm not sure how to fix it.

Can anyone please take a look at this and advise me.

thanks in advance

回答1:

Ok if anyone is experiencing a similar problem you might want to check out the config.assets.compile In my case I only had to change the config/environments/production.rb config.assets.compile from false, I changed it to true and now everything worked.... It only took me two days to figure it out :D



回答2:

Note that public/assets is where the asset pipeline puts its stuff. If this is for static assets, I would put them in app/assets/images in order to use the asset pipeline, or choose another directory name.



回答3:

I was using html's img tag to serve image assets as following

<img src="/assets/AdminLTELogo.png" alt="AdminLTE Logo" class="brand-image im">

Just had to change it to following and it worked.

<%= image_tag 'AdminLTELogo.png' , alt: "AdminLTE Logo", class: "brand-image im %>

Cheers.