我该如何使用Puma的配置文件?

26

我正在按照这篇指南进行操作,它记录了存储在应用程序配置目录中的puma.rb文件。

这篇指南有点不太可靠,但是我认为puma.rb文件所做的事情如下。它不会运行像以下这样疯狂的命令来在指定的套接字上运行puma:

bundle exec puma -e production -b unix:///var/run/my_app.sock

您可以在puma.rb文件中指定端口、pid、session和其他参数,类似于以下方式:

rails_env = ENV['RAILS_ENV'] || 'production'

threads 4,4

bind  "/home/starkers/Documents/alpha/tmp/socket"
pidfile "/home/starkers/Documents/alpha/tmp/pid"
state_path "/home/starkers/Documents/alpha/tmp/state"

activate_control_app

然后,您可以进入应用程序的根目录并运行一个简单的命令,例如

'puma'

puma.rb中设置的参数将被遵循。不幸的是,这对我似乎不起作用。

至少,我在一个小型测试应用程序的根目录中运行了puma,但是/home/starkers/Documents/alpha/tmp/sockets没有出现.sock文件,这是否意味着它没有工作?

我该如何解决这个问题?我在本地开发机器上,这可能会导致出现错误吗?在运行

puma时,我需要传递某些参数吗?

4个回答

44

我也曾经苦于找不到有关Puma配置文件的文档,但我发现全功能config.ru文件还是很有用的。我在这里为将来参考格式化了它:

# The directory to operate out of.
# The default is the current directory.

directory '/u/apps/lolcat'

# Load “path” as a rackup file.
# The default is “config.ru”.

rackup '/u/apps/lolcat/config.ru'

# Set the environment in which the rack's app will run. The value must be a string.
# The default is “development”.

environment 'production'

# Daemonize the server into the background. Highly suggest that
# this be combined with “pidfile” and “stdout_redirect”.
# The default is “false”.

daemonize
daemonize false

# Store the pid of the server in the file at “path”.

pidfile '/u/apps/lolcat/tmp/pids/puma.pid'

# Use “path” as the file to store the server info state. This is
# used by “pumactl” to query and control the server.

state_path '/u/apps/lolcat/tmp/pids/puma.state'

# Redirect STDOUT and STDERR to files specified. The 3rd parameter
# (“append”) specifies whether the output is appended, the default is
# “false”.

stdout_redirect '/u/apps/lolcat/log/stdout', '/u/apps/lolcat/log/stderr'
stdout_redirect '/u/apps/lolcat/log/stdout', '/u/apps/lolcat/log/stderr', true

# Disable request logging.
# The default is “false”.

quiet

# Configure “min” to be the minimum number of threads to use to answer
# requests and “max” the maximum.
# The default is “0, 16”.

threads 0, 16

# Bind the server to “url”. “tcp://”, “unix://” and “ssl://” are the only
# accepted protocols.
# The default is “tcp://0.0.0.0:9292”.

bind 'tcp://0.0.0.0:9292'
bind 'unix:///var/run/puma.sock'
bind 'unix:///var/run/puma.sock?umask=0777'
bind 'ssl://127.0.0.1:9292?key=path_to_key&cert=path_to_cert'

# Listens on port 7001
# The default is 9292
port 7001

# Instead of “bind 'ssl://127.0.0.1:9292?key=path_to_key&cert=path_to_cert'” you
# can also use the “ssl_bind” option.

 ssl_bind '127.0.0.1', '9292', { key: path_to_key, cert: path_to_cert }

# Code to run before doing a restart. This code should
# close log files, database connections, etc.

# This can be called multiple times to add code each time.

on_restart do
  puts 'On restart...'
end

# Command to use to restart puma. This should be just how to
# load puma itself (ie. 'ruby -Ilib bin/puma'), not the arguments
# to puma, as those are the same as the original process.

restart_command '/u/app/lolcat/bin/restart_puma'

# === Cluster mode ===

# How many worker processes to run.
# The default is “0”.

workers 2

# Code to run when a worker boots to setup the process before booting
# the app.
# This can be called multiple times to add hooks.

on_worker_boot do
  puts 'On worker boot...'
end

# === Puma control rack application ===

# Start the puma control rack application on “url”. This application can
# be communicated with to control the main server. Additionally, you can
# provide an authentication token, so all requests to the control server
# will need to include that token as a query parameter. This allows for
# simple authentication.

# Check out https://github.com/puma/puma/blob/master/lib/puma/app/status.rb
# to see what the app has available.

activate_control_app 'unix:///var/run/pumactl.sock'
activate_control_app 'unix:///var/run/pumactl.sock', { auth_token: '12345' }
activate_control_app 'unix:///var/run/pumactl.sock', { no_token: true }

那些设置将会在一个 Ruby 文件中(例如 config/puma.rb)进行配置,之后像 Starkers 所说的那样,您可以使用以下命令运行它:

puma -C config/puma.rb


14

更新: 自2019年以来,Puma版本的原始答案不再正确:Puma添加了一个回退机制,现在两个位置都会被检查。( https://github.com/puma/puma/pull/1885)

Puma首先查找配置文件config/puma/<environment_name>.rb,然后回退到config/puma.rb

过时的答案:

如果定义了环境 - 这是您的示例中的情况 - 配置文件将从config/puma/[environment].rb而不是config/puma.rb读取。

只需将config/puma.rb移动到config/puma/production.rb即可解决问题。

阅读Puma文档以获取更多详细信息:配置文件


根据链接中的信息,即使指定了环境,“Puma首先在config/puma/<environment_name>.rb查找配置文件,然后退回到config/puma.rb。”。因此,这可能不是一个问题。 - Smitty
1
我的答案已更新。该功能是在我发布帖子两年多之后添加的。文档更新甚至更晚。(https://github.com/puma/puma/pull/1885和https://github.com/puma/puma/pull/2509) - Daniel Rikowski
1
啊,谢谢!我一直在想那是否是这种情况。 - Smitty

6
这将有效:

puma -C config/puma.rb

1

您需要告诉 Puma 在哪里找到您的 rackup 文件,可以通过将以下内容放入配置中来实现:

rackup DefaultRackup

看起来这个问题的修复已经合并到主分支中了:https://github.com/puma/puma/pull/271


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