Ruby-on-rails Rails 公用文件夹项目在生产中不可用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22570563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails public folder items not available in production
提问by Tyler
I have a few items that are accessible just fine in development mode within the /publicdirectory of my app: favicon.ico, robots.txt.
我有一些项目可以在/public我的应用程序目录中的开发模式下正常访问:favicon.ico, robots.txt.
I can view these in development at e.g. localhost:3000/favicon.ico. However, when I push up to production, the assets are not visible at those paths. As a result, I don't have a favicon icon and google can't find my robots.txt file, among other things.
我可以在例如localhost:3000/favicon.ico. 但是,当我推进生产时,这些路径上的资产是不可见的。结果,我没有收藏夹图标,谷歌也找不到我的 robots.txt 文件等等。
How can I fix this so that the /publicdirectory is accessible through relevant urls?
如何解决此问题,以便/public可以通过相关网址访问该目录?
回答by Kirti Thorat
Add following line in your app/config/environments/production.rb
在您的 app/config/environments/production.rb
config.serve_static_assets = true
Here's what Rails Guides has to say
以下是 Rails Guides 所说的
config.serve_static_assets configures Rails itself to serve static assets. 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.
config.serve_static_assets 配置 Rails 本身以提供静态资产。默认为 true,但在生产环境中关闭,因为用于运行应用程序的服务器软件(例如 Nginx 或 Apache)应改为提供静态资产。与默认设置不同,在运行时将此设置为 true(绝对不推荐!)或使用 WEBrick 在生产模式下测试您的应用程序。否则,您将无法使用页面缓存,并且对公共目录下定期存在的文件的请求无论如何都会命中您的 Rails 应用程序。
回答by Stanley Shauro
Rails 5.1
导轨 5.1
Add following line in your app/config/environments/production.rb
在您的 app/config/environments/production.rb
config.public_file_server.enabled = true
Updated
更新
config/environments/production.rbcontains the line:
config/environments/production.rb包含以下行:
config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
It means you can pass RAILS_SERVE_STATIC_FILESin a command line when you starts your rails server, e.g.:
这意味着您可以RAILS_SERVE_STATIC_FILES在启动 rails 服务器时传入命令行,例如:
RAILS_ENV=production RAILS_SERVE_STATIC_FILES=yes rails s -b 0.0.0.0 -p 3000

