`rails server puma`与`puma`的区别

9

有些指南(例如)建议使用这种方法来启动自己的Web服务器。

bundle exec rails server puma

但我一直都是直接使用puma启动服务器的

bundle exec puma

当通过rails server启动puma(或任何其他服务器)时,是否会发生特殊的事情?

1个回答

15

当你使用rails s <server>命令时,该服务器将从Rails命令启动,并且知道Rails环境。

这使得例如可以使用任何rails server命令提供的功能和标志。

rails s --help
Usage: rails server [mongrel, thin, etc] [options]
    -p, --port=port                  Runs Rails on the specified port.
                                     Default: 3000
    -b, --binding=ip                 Binds Rails to the specified ip.
                                     Default: 0.0.0.0
    -c, --config=file                Use custom rackup configuration file
    -d, --daemon                     Make server run as a Daemon.
    -u, --debugger                   Enable ruby-debugging for the server.
    -e, --environment=name           Specifies the environment to run this server under (test/development/production).
                                     Default: development
    -P, --pid=pid                    Specifies the PID file.
                                     Default: tmp/pids/server.pid

    -h, --help                       Show this help message.

例如,您可以通过传递 --debugger 或将服务器设置为守护进程来将调试器附加到会话。

第二个优点是您可以对 Puma 实例进行版本控制,因为您必须在 Gemfile 中列出该 gem。如果像您现在这样使用 bundle exec 启动,则已经实现了此功能。

相反,当您仅运行 $ puma(或 $ bundle exec puma)时,不会经过 Rails 系统。Puma 将尝试查找 rack 引导文件并使用它(因为 Rails 在应用程序根目录提供了一个 config.ru 脚本,所以它可以正常工作)。

一般来说,如果您不需要向服务器传递特定选项,则没有真正的区别。我喜欢 Puma,并且倾向于在某些项目中使用它,即使在生产环境中我们使用 Unicorn,因此作为独立命令运行 $ puma 很方便,因为我不需要将其添加到 Gemfile 中。

然而,如果我的整个堆栈都使用 Puma,我可能会选择 $ rails s puma。这也是文档中建议的命令


当运行rails s puma时,您拥有的配置文件不会被加载...这是缺点。这里有一个快速别名来使用配置文件(config/puma.rb)启动服务器。只需复制粘贴以下命令:echo 'alias start_puma="bundle exec puma -p 3000 -S ~/puma -C config/puma.rb"' >> ~/.bash_profile && source ~/.bash_profile 然后使用start_puma即可。 - blnc
3
链接页面上推荐的命令现在是 bundle exec puma(2018年12月7日)。 - vinegarbin
那个页面上推荐的内容不太清楚。但是从我所了解的来看,puma 有一个单独的环境概念,用于选择配置文件。而 bin/rails s 会自动获取这个环境(从 RAILS_ENV)。 - x-yuri
...或者可能没有独立的环境概念。也许它只是不把自己视为“rails”专用的web服务器,因此不能依赖于RAILS_ENV变量。毕竟,它尊重RACK_ENV。 - x-yuri

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接