【问题标题】:How do I use puma's configuration file?如何使用 puma 的配置文件?
【发布时间】:2013-11-25 14:29:48
【问题描述】:

我正在关注this guide,它记录了存储在应用配置目录中的puma.rb 文件。

该指南有点古怪,但这是我假设puma.rb 文件所做的。而不是运行像这样的疯狂命令来让 puma 在指定的套接字上运行:

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

你可以像这样在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

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

'彪马'

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

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

我如何让它工作?我在本地开发机器上,那会以某种方式导致此错误吗?有没有运行时需要传入的参数

puma?

【问题讨论】:

    标签: ruby-on-rails puma


    【解决方案1】:

    这将起作用:

    puma -C config/puma.rb
    

    【讨论】:

      【解决方案2】:

      我也一直在尝试查找有关 puma 配置文件的文档,但我确实发现 the all-in-one 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

      【讨论】:

        【解决方案3】:

        您需要告诉 puma 在哪里可以找到您的 rackup 文件,您可以将其放入您的配置中:

        rackup DefaultRackup
        

        似乎对此的修复已合并到 master:https://github.com/puma/puma/pull/271

        【讨论】:

          【解决方案4】:

          更新:对于 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 文档了解更多详情:Configuration file

          【讨论】:

          • 根据链接中找到的信息,即使指定了环境,“Puma 首先在 config/puma/.rb 查找配置,然后回退到 config/puma.rb ."。所以这可能不是问题。
          • 更新了我的答案。该功能是在我发帖 2 年多后添加的。文档更新甚至更晚。 (github.com/puma/puma/pull/1885github.com/puma/puma/pull/2509
          • 啊,谢谢!我想知道是不是这样。
          猜你喜欢
          • 2014-10-03
          • 2017-12-25
          • 2015-12-27
          • 2014-01-24
          • 2019-03-30
          • 2015-09-04
          • 2017-10-03
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多