Rails 4生产环境中的资产找不到404错误
我对Java环境中的Rails还是陌生的.在轨3到4的资产管道中,我几乎没有什么困惑.
I am new to rails from java enviroment. I have few confusions in asset pipeline with rails 3 to 4.
当前,我处于4.2.5的状态 我在本地环境上创建了一个示例应用程序. 我从app/assets/javascripts内部创建了一个新的js,并从视图中引用了它,一切似乎在我的本地环境下都可以正常工作.
Currently I am in rails 4.2.5 I created a sample app on my local environment. I created a new js from inside app/assets/javascripts and referencing it from the view , everything seems to working fine on my local environment.
在此之后,我想与生产环境一起测试它的工作方式.这是我执行的步骤.
After this I thought to test with production env how it works. Here are the steps I did.
- RAILS_ENV =生产耙资产:清理资产:预编译[所有创建的文件都是公共/资产的
- 以生产模式启动服务器rails server -e production
- 现在,当我浏览页面时,出现此错误,
"NetworkError: 404 Not Found - http://localhost:3000/assets/application-c5c431cb7c0a202f831a634922aaf1d536712002ae74334fb03ba4698b32b84c.js"
在萤火虫中.
- RAILS_ENV=production rake assets:clean assets:precompile [All the files created public/assets]
- Started the server in production mode rails server -e production
- Now when i browsed the page I am getting this error,
"NetworkError: 404 Not Found - http://localhost:3000/assets/application-c5c431cb7c0a202f831a634922aaf1d536712002ae74334fb03ba4698b32b84c.js"
in firebug.
当搜索几个线程时,他们建议添加config.assets.compile = true
.但是我不认为这就是解决方案,因为它会使应用程序变慢.
When searched few threads , they suggest to add config.assets.compile = true
. But I do not think so this is what the solution is, as it slows down the app.
请帮助.
如果没有通过资产处理资产,默认情况下,Rails4将返回404. 外部代理(例如Nginx). - https://github.com/heroku/rails_serve_static_assets
By default Rails4 will return a 404 if an asset is not handled via an external proxy such as Nginx. - https://github.com/heroku/rails_serve_static_assets
我建议您看看 rails_12factor宝石,其中包括
I suggest you have a look at the rails_12factor gem, which includes the rails_serve_static_assets gem which will allow your Rails app to serve static assets (like your .js file).
您可能不需要宝石(尽管我没有尝试过):
You may not need a gem for this (though I've not tried it):
config.serve_static_files
将Rails本身配置为静态服务 文件.默认为true,但在生产环境中已打开 作为用于运行Windows Server 2003的服务器软件(例如NGINX或Apache)关闭 应用程序应该提供静态资产.不同于默认 设置运行时将其设置为true(绝对不建议!)或 使用WEBrick在生产模式下测试您的应用程序.否则你不会 能够使用页面缓存和对定期存在的文件的请求 仍然会在公共目录下访问您的Rails应用程序. - http://guides.rubyonrails.org/configuring.html
config.serve_static_files
configures Rails itself to serve static files. Defaults to true, but in the production environment is turned off as the server software (e.g. NGINX or Apache) used to run the application should serve static assets instead. Unlike the default setting set this to true when running (absolutely not recommended!) or testing your app in production mode using WEBrick. Otherwise you won't be able use page caching and requests for files that exist regularly under the public directory will anyway hit your Rails app. - http://guides.rubyonrails.org/configuring.html
在Rails 5中,此配置方法已重命名为config.public_file_server.enabled
,rails_serve_static_assets gem根据版本处理正确的命名.
In Rails 5, this config method has been renamed to config.public_file_server.enabled
, the rails_serve_static_assets gem handles the correct naming depending on version.