为什么 ActionDispatch::Static 从生产中的中间件堆栈中删除?

Amr*_*man 4 middleware ruby-on-rails

我试图了解 Rails 如何在/public下提供静态文件,如果我理解正确,则ActionDispatch::Static中间件负责此操作。

但是,我注意到它仅在开发环境中可用:

$ rake middleware

use Rack::Sendfile
use ActionDispatch::Static
use Rack::Lock
.
.
Run Code Online (Sandbox Code Playgroud)

并在生产中:

$ RAILS_ENV=production rake middleware

use Rack::Sendfile
use Rack::Lock
.
.
Run Code Online (Sandbox Code Playgroud)

那么静态文件在生产中是如何提供的呢?我的猜测是,这是由 Web 服务器本身(apache、puma...等)处理的,以提高性能,这是正确的吗?

如果是这样的话,那么为什么要在开发中为这个任务创建一个专用的中间件呢?

谢谢。

zwi*_*pie 6

这是由以下设置控制的config/environments/production.rb

# Disable Rails's static asset server (Apache or nginx will already do this).
config.serve_static_files = false
Run Code Online (Sandbox Code Playgroud)

在开发中未启用此功能的原因是,在开发中您不需要 nginx 或 apache 的额外功能/开销。例如,您不希望浏览器在开发时获取缓存这些文件的指令。

ActionDispatch::Static只会从磁盘加载这些文件并将它们发送到浏览器,没什么花哨的。所有请求都public将由 处理ActionDispatch::Static,所有其他请求均由您的 Rails 应用程序处理。