CSS doesn't load in production - everything fine in dev (Rails 4.1, Capistrano 3; bootstrap), update: nginx config issue

喜欢而已 提交于 2019-12-22 14:58:31

问题


Problem is as follows: Everything works fine in development, but when I deploy my rails app to production, the site works in plain html, but CSS (bootstrap) doesn't get loaded (JS probably also not).

Sorry, I looked for 2 days at ~15 possible solutions and could not make it work...

Update: After following @rich-peck's advice, it looks like it's a server issue. Updated with nginx config

My setup:

  • Ruby 2.1.2, Rails 4.1
  • Rbenv
  • mysql2 as db
  • Bootstrap 3
  • Ubuntu 14.04 on Server with nginx and passenger
  • Capistrano 3 for deployment

Config as follows - production.rb:

config.cache_classes = true
config.eager_load = true
config.consider_all_requests_local       = false
config.action_controller.perform_caching = true
config.serve_static_assets = true
config.assets.compress = true
config.assets.compile = false
config.assets.digest = true
config.assets.version = '1.0'
config.log_level = :info
config.assets.precompile << /(^[^_\/]|\/[^_])[^\/]*$/
config.i18n.fallbacks = true
config.active_support.deprecation = :notify
config.log_formatter = ::Logger::Formatter.new
config.active_record.dump_schema_after_migration = false

Capfile:

require 'capistrano/setup'
require 'capistrano/deploy'
require 'capistrano/bundler'
require 'capistrano/rails'
require 'capistrano/rbenv'
set :rbenv_type, :user
set :rbenv_custom_path, '/home/deploy/.rbenv/'
set :rbenv_ruby, '2.1.2'
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }

in application.rb:

...
# Enable the asset pipeline
config.assets.enabled = true
...

application.css.scss:

/* ...
*= require_self
*= require_tree .
*/

@import "bootstrap";
@import "custom";
@import "exchange_rates";
@import "contacts";

This is what I tried (among many others):

# on dev machine and server /current:
RAILS_ENV=production bundle exec rake assets:precompile

see Link Custom CSS and Javascript to Rails After Deployment

in configs:

config.serve_static_assets = true # tried false
config.assets.compile = false     # tried true

see rails application doesn't load css/js/images in production-environment

production.log doesn't throw any errors, except for pages with js.

Also read a lot about the Rails Asset Pipeline and nginx server setup, but couldn't figure it out... Any help would be greatly appreciated!!

Update: nginx server config

Originally, I followed the very helpful and clear deploy tutorial at gorails: https://gorails.com/deploy/ubuntu/14.04

However, nginx wasn't starting correctly so I updated the config files (with help from SO) as follows:

/etc/nginx/nginx.conf:

events {
  worker_connections  4096;  ## Default: 1024
}

http {
    ##
    # Phusion Passenger
    ##
    # Uncomment it if you installed ruby-passenger or ruby-passenger-enterprise
    ##
    passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
    passenger_ruby /home/deploy/.rbenv/shims/ruby;
    passenger_nodejs /usr/bin/nodejs;
    server {
            listen 80 default_server;
            listen [::]:80 default_server ipv6only=on;

            server_name <URL>;
            passenger_enabled on;
            rails_env    production;
            root         /home/deploy/<PROJECT_NAME>/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;
            }
    }
}

(URL and PROJECT_NAME redacted in question)

/etc/nginx/sites-enabled/default I could keep empty, and with this I got nginx to run smoothly - except for this problem with the assets. Does any one have any advice?


回答1:


Several things to test:

  • When you precompile your assets, do the required files appear in your public directory?

When you precompile your assets, you basically tell the Rails asset pipeline to merge & compile all your assets in the public directory. This means if you're trying to include a particular file in your production asset pipeline, it should be precompiled

I would run the rake assets:precompile RAILS_ENV=production command, and then look at all your assets in the public folder to check if they're there

--

  • If the assets are present in your public folder, are they being pushed to the server?

You should SSH into your server & browse to the public directory of your app (in /current). This will allow you to see if your assets have been uploaded to your server as you need

If they are not there, the problem is with Capistrano's deploy process; if they are there, it means there's a problem with your Rails server

--

  • If the assets are in the public folder on your server, it will mean the server is at fault somehow

Probably the best way to ensure it's not a server issue is to reload the server. With apache, you'll typically use service reload apache2, but I'm not sure about nginx.

This should be accompanied by looking at your Rails installation - is it calling the correct assets as defined in your config files?




回答2:


This is a bit off topic, but for organization's sake you might want to put your Nginx server block in /etc/nginx/sites-available/default and then make sure you have it linked to /etc/nginx/sites-enabled/default.

It should work fine with the server block in /etc/nginx/nginx.conf but it's best practice to put it in the default host or create another virtual host if you so desire.

Glad you were able to get things working.



来源:https://stackoverflow.com/questions/24355795/css-doesnt-load-in-production-everything-fine-in-dev-rails-4-1-capistrano-3

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