这是由Nginx + Passenger提供服务的Rails 4项目。在rails环境开发时工作完美。我认为压缩的javascript文件正确加载,因为应该在页面加载时执行的ajax调用工作正常。问题是没有任何CSS规则应用于文档。
我遇到的问题完全相同:CSS available but not rendering when push to production [Rails app],但我的css文件的mime类型是正确的。
这是我的config / environments / production.rb
config.eager_load = true
config.cache_classes = 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.css_compressor = :yui
资产已正确预编译。这是相关的服务html链接/脚本规范:
<link data-turbolinks-track="true" href="/assets/application-3d4e35bd2c14a288b7d2ff3f5dd8f5c4.css" media="all" rel="stylesheet" />
<script data-turbolinks-track="true" src="/assets/application-ab6bb9f22b553eba6788881b0b36f94c.js"></script>
Nginx配置:
http {
passenger_root /home/ubuntu/.rvm/gems/ruby-2.1.3/gems/passenger-4.0.53;
passenger_ruby /home/ubuntu/.rvm/gems/ruby-2.1.3/wrappers/ruby;
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name subdomain.domain.com;
root /home/user/rails-project/public;
passenger_enabled on;
rails_env production;
}
}
这里是css请求/响应(请注意正确的mime-types):
CSS文件已正确编译,我的所有sass和css规则和存在(组合sass和css)。
答案 0 :(得分:0)
静态文件应该直接从磁盘提供,因此请确保Nginx设置为具有足够的权限来访问文件。它还建议为静态创建自己的位置:
location /assets/ {
root /home/user/rails-project/public;
gzip_static on;
expires max;
add_header Cache-Control public;
access_log /dev/null;
}
Assets Pipeline生成压缩的JS和CSS文件的gzip版本,因此“gzip_static”指令可以帮助您直接提供gzip,而不会产生任何开销。您的Nginx应该使用模块--with-http_gzip_static_module进行编译以支持它。
答案 1 :(得分:0)
事实证明,CSS编译器很可能是此问题的根源。当我有这样的编译器设置:
config.assets.css_compressor = :yui
css文件似乎被缩小并编译而没有问题。但是,一旦我删除了该行(我假设它默认为压缩的sass gem),那么当我尝试rake assets:precompile
时,我收到以下错误:
➜ stylesheets git:(master) ✗ rake assets:precompile
(in /home/ubuntu/spice-conduit)
rake aborted!
Sass::SyntaxError: Invalid CSS after " filter: progid": expected ";", was ": DXImageTransf..."
(in /home/ubuntu/spice-conduit/app/assets/stylesheets/application.css)
(sass):3566
/home/ubuntu/.rvm/gems/ruby-2.1.3/gems/sass-3.2.19/lib/sass/scss/parser.rb:1147:in `expected'
/home/ubuntu/.rvm/gems/ruby-2.1.3/gems/sass-3.2.19/lib/sass/scss/parser.rb:1085:in `expected'
/home/ubuntu/.rvm/gems/ruby-2.1.3/gems/sass-3.2.19/lib/sass/scss/parser.rb:1080:in `tok!'
/home/ubuntu/.rvm/gems/ruby-2.1.3/gems/sass-3.2.19/lib/sass/scss/parser.rb:586:in `block in declaration_or_ruleset'
/home/ubuntu/.rvm/gems/ruby-2.1.3/gems/sass-3.2.19/lib/sass/scss/parser.rb:1123:in `call'
/home/ubuntu/.rvm/gems/ruby-2.1.3/gems/sass-3.2.19/lib/sass/scss/parser.rb:1123:in `rethrow'
/home/ubuntu/.rvm/gems/ruby-2.1.3/gems/sass-3.2.19/lib/sass/scss/parser.rb:592:in `declaration_or_ruleset'
/home/ubuntu/.rvm/gems/ruby-2.1.3/gems/sass-3.2.19/lib/sass/scss/parser.rb:554:in `block_child'
/home/ubuntu/.rvm/gems/ruby-2.1.3/gems/sass-3.2.19/lib/sass/scss/parser.rb:546:in `block_contents'
/home/ubuntu/.rvm/gems/ruby-2.1.3/gems/sass-3.2.19/lib/sass/scss/parser.rb:535:in `block'
/home/ubuntu/.rvm/gems/ruby-2.1.3/gems/sass-3.2.19/lib/sass/scss/parser.rb:529:in `ruleset'
显然,使用yui压缩器时没有显示的补偿错误?编译bootstrap和font-awesome css文件时会发生这些编译错误。
我如何解决:
这个解决方案不是主意,因为现在我必须手动维护bootstrap和font-awesome资产文件。但是,目前至少我的应用程序提供压缩和缩小的资产!