Ruby-on-rails 如何使用 puma 的配置文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19946153/
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
How do I use puma's configuration file?
提问by Starkers
I was following this guideit documents the puma.rbfile that is stored inside the app's config directory.
我正在遵循本指南,它记录了puma.rb存储在应用程序配置目录中的文件。
The guide is a bit flakey, but here's what I assume the puma.rbfile does. Instead of running crazy commands such as this to get puma running on a specified socket:
该指南有点古怪,但这是我假设puma.rb文件所做的。而不是像这样运行疯狂的命令来让 puma 在指定的套接字上运行:
bundle exec puma -e production -b unix:///var/run/my_app.sock
You can just specify the port, pid, session and other parameters in the puma.rbfile like this:
您可以puma.rb像这样在文件中指定端口、pid、会话和其他参数:
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
And then you could cd into the app's root and run a simple command like
然后你可以 cd 进入应用程序的根目录并运行一个简单的命令,比如
'puma'
'美洲狮'
and the parameters set in puma.rbwould be followed. Unfortunately that doesn't seem to work for me.
并且puma.rb将遵循中设置的参数。不幸的是,这似乎对我不起作用。
At least, I ran pumainside the root of a tiny test app, and no .sockfile appeared in
/home/starkers/Documents/alpha/tmp/socketsso does that mean it isn't working?
至少,我在puma一个小型测试应用程序的根目录中运行,并且没有.sock出现任何文件,
/home/starkers/Documents/alpha/tmp/sockets这是否意味着它不起作用?
How do I get this working? I am on a local development machine, so could that cause this error somehow? Is there a parameter I need to pass in when running
我如何让这个工作?我在本地开发机器上,所以会以某种方式导致这个错误吗?运行时有没有需要传入的参数
puma?
puma?
回答by Richard Nienaber
I was also stuck trying to find documentation on the config file for puma but I did find the all-in-one config.rufile useful. I've formatted it here for future reference:
我也一直在寻找有关 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 }
Those settings would then go in a ruby file (e.g. config/puma.rb) and then as Starkers says, you can run it with
这些设置然后会进入一个 ruby 文件(例如 config/puma.rb),然后正如 Starkers 所说,你可以使用
puma -C config/puma.rb
puma -C 配置/puma.rb
回答by Daniel Rikowski
If there is an environment defined - which is the case in your example - the configuration file is read from config/puma/[environment].rband not config/puma.rb.
如果定义了环境 - 在您的示例中就是这种情况 - 配置文件将被读取,config/puma/[environment].rb而不是读取config/puma.rb。
Just move your config/puma.rbto config/puma/production.rband it should work.
只需移动你config/puma.rb的config/puma/production.rb,它应该可以工作。
Read the Puma documentation for more details: Configuration file
阅读 Puma 文档以获取更多详细信息:配置文件
回答by Starkers
This will work:
这将起作用:
puma -C config/puma.rb
回答by Schneems
You need to tell puma where to find your rackupfile you can do it by putting this in your config:
你需要告诉 puma 在哪里可以找到你的rackup文件,你可以通过把它放在你的配置中来做到这一点:
rackup DefaultRackup
It looks like a fix for this is merged into master: https://github.com/puma/puma/pull/271
看起来对此的修复已合并到 master 中:https: //github.com/puma/puma/pull/271

