新手问题 【已解决】Puma + Nginx + Rails 服务器配置问题

somejump · 2015年05月19日 · 最后由 cassiuschen 回复于 2015年05月20日 · 5375 次阅读

我是小白,第二次配置 rails 服务器,首先创建/修改了 nginx/conf.d/mo.conf,完全复制了 puma 官方提供的源代码(当然更改了路径)

upstream myapp(这里真的就只是“myapp”,没敢改成自己的appname) {
  server unix:///home/mo1.0/tmp/puma.sock;
}

server {
  listen 80;
  server_name myapp.com;

  # ~2 seconds is often enough for most folks to parse HTML/CSS and
  # retrieve needed images/icons/frames, connections are cheap in
  # nginx so increasing this is generally safe...
  keepalive_timeout 5;

  # path for static files
  root /home/mo1.0/public;
  access_log /home/mo1.0/log/nginx.access.log;
  error_log /home/mo1.0/log/nginx.error.log info;

  # this rewrites all the requests to the maintenance.html
  # page if it exists in the doc root. This is for capistrano's
  # disable web task
  if (-f $document_root/maintenance.html) {
    rewrite  ^(.*)$  /maintenance.html last;
    break;
  }

  location / {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;

    # If the file exists as a static file serve it directly without
    # running all the other rewite tests on it
    if (-f $request_filename) {
      break;
    }

    # check for index.html for directory index
    # if its there on the filesystem then rewite
    # the url to add /index.html to the end of it
    # and then break to send it to the next config rules.
    if (-f $request_filename/index.html) {
      rewrite (.*) $1/index.html break;
    }

    # this is the meat of the rack page caching config
    # it adds .html to the end of the url and then checks
    # the filesystem for that file. If it exists, then we
    # rewite the url to have explicit .html on the end
    # and then send it on its way to the next config rule.
    # if there is no file on the fs then it sets all the
    # necessary headers and proxies to our upstream pumas
    if (-f $request_filename.html) {
      rewrite (.*) $1.html break;
    }

    if (!-f $request_filename) {
      proxy_pass http://myapp;
      break;
    }
  }

  # Now this supposedly should work as it gets the filenames with querystrings that Rails provides.
  # BUT there's a chance it could break the ajax calls.
  location ~* \.(ico|css|gif|jpe?g|png|js)(\?[0-9]+)?$ {
     expires max;
     break;
  }

  # Error pages
  # error_page 500 502 503 504 /500.html;
  location = /500.html {
    root /home/mo1.0/public;
  }
}

然后是 config/puma.rb

threads 8,32
workers 3
preload_app!
on_worker_boot do
  ActiveSupport.on_load(:active_record) do
    ActiveRecord::Base.establish_connection
  end
end
bind 'unix:///home/mo1.0/tmp/puma.sock'
stdout_redirect '/home/mo1.0/log/stdout', '/home/mo1.0/log/stderr'
environment 'production'
daemonize
pidfile '/home/mo1.0/tmp/pids/puma.pid'
state_path '/home/mo1.0/tmp/pids/puma.state'

之后把 puma 跑起来后输入 ps aux,发现运行的进程没有 puma 直接访问 ip(lz 还不会配置域名),提示 502 bad gateway,说明 nginx 跑起来了,puma 跪了 服务器是 linode 的,还挺好使的,域名买了了.com 的只不过还没 DNS 解析呢,想先测试一下,结果就这样了

求帮忙 QUQ

解决方法:看 puma 的错误日志 === puma startup: 2015-05-19 17:04:50 +0800 === /usr/local/rvm/gems/ruby-2.0.0-p643/gems/puma-2.9.0/lib/puma/cli.rb:246:in initialize': No such file or directory - /home/mo1.0/tmp/pids/puma.pid (Errno::ENOENT) from /usr/local/rvm/gems/ruby-2.0.0-p643/gems/puma-2.9.0/lib/puma/cli.rb:246:inopen' from /usr/local/rvm/gems/ruby-2.0.0-p643/gems/puma-2.9.0/lib/puma/cli.rb:246:in write_pid' from /usr/local/rvm/gems/ruby-2.0.0-p643/gems/puma-2.9.0/lib/puma/cli.rb:222:inwrite_state' from /usr/local/rvm/gems/ruby-2.0.0-p643/gems/puma-2.9.0/lib/puma/cluster.rb:332:in run' from /usr/local/rvm/gems/ruby-2.0.0-p643/gems/puma-2.9.0/lib/puma/cli.rb:499:inrun' from /usr/local/rvm/gems/ruby-2.0.0-p643/gems/puma-2.9.0/bin/puma:10:in ' from /usr/local/rvm/gems/ruby-2.0.0-p643/bin/puma:23:inload' from /usr/local/rvm/gems/ruby-2.0.0-p643/bin/puma:23:in

' from /usr/local/rvm/gems/ruby-2.0.0-p643/bin/ruby_executable_hooks:15:ineval' from /usr/local/rvm/gems/ruby-2.0.0-p643/bin/ruby_executable_hooks:15:in `' 发现文件夹路径设置错误,修正后即可(puma 可以自动生成文件,但不会生成文件夹) 此外,如果去掉后台化代码后 puma 运行时是不能操控终端的,如果发现可以继续操作中断说明 puma 跪了

啊?我用 linode 跑 puma 也有问题,然后果断换回阿里云,我两边 Ruby 的配置是一样的(linode 不是我最开始配置的,我接手的,别的变动不清楚),但是 puma -d 就是无法运行。

daemonize 去掉,再启动,看错误信息

root@hostname:/home/mo1.0# puma -e production -C config/puma.rb
[29743] Puma starting in cluster mode...
[29743] * Version 2.9.0 (ruby 2.0.0-p643), codename: Team High Five
[29743] * Min threads: 8, max threads: 32
[29743] * Environment: production
[29743] * Process workers: 3
[29743] * Preloading application
[29743] * Listening on unix:///home/mo1.0/tmp/puma.sock
[29743] Use Ctrl-C to stop
root@hostname:/home/mo1.0# 

我把后台化的代码区掉了,结果如上,好像是没问题?? ps aux 也没有显示 puma 的进程 #2 楼 @huacnlee

唉,算了,我还是用 passenger 去吧,puma 好像一大堆人都说有 bug

问题是 去掉 -d 就正常了,加上-d 也没错误信息。想不通。。这是上个月遇到的问题,同样配置转到阿里云就正常了。

还有日志没看 '/home/mo1.0/log/stdout', '/home/mo1.0/log/stderr'

你居然跑到这里来了… 你看看/home/mo1.0/tmp/pids/puma.pid是否存在……根据我对你的了解我怀疑你是文件夹没有创建………

=== puma startup: 2015-05-19 17:04:50 +0800 === /usr/local/rvm/gems/ruby-2.0.0-p643/gems/puma-2.9.0/lib/puma/cli.rb:246:in initialize': No such file or directory - /home/mo1.0/tmp/pids/puma.pid (Errno::ENOENT) from /usr/local/rvm/gems/ruby-2.0.0-p643/gems/puma-2.9.0/lib/puma/cli.rb:246:inopen' from /usr/local/rvm/gems/ruby-2.0.0-p643/gems/puma-2.9.0/lib/puma/cli.rb:246:in write_pid' from /usr/local/rvm/gems/ruby-2.0.0-p643/gems/puma-2.9.0/lib/puma/cli.rb:222:inwrite_state' from /usr/local/rvm/gems/ruby-2.0.0-p643/gems/puma-2.9.0/lib/puma/cluster.rb:332:in run' from /usr/local/rvm/gems/ruby-2.0.0-p643/gems/puma-2.9.0/lib/puma/cli.rb:499:inrun' from /usr/local/rvm/gems/ruby-2.0.0-p643/gems/puma-2.9.0/bin/puma:10:in <top (required)>' from /usr/local/rvm/gems/ruby-2.0.0-p643/bin/puma:23:inload' from /usr/local/rvm/gems/ruby-2.0.0-p643/bin/puma:23:in <main>' from /usr/local/rvm/gems/ruby-2.0.0-p643/bin/ruby_executable_hooks:15:ineval' from /usr/local/rvm/gems/ruby-2.0.0-p643/bin/ruby_executable_hooks:15:in `

' 太感谢了!你们真是厉害!(其实是我太弱) 我看了位于/tmp 下的 stderr,发现确实 puma 可以创建文件但是不能创建文件夹,所以我的/pid 路径有误,修正后果然可以了 #7 楼 @cassiuschen #6 楼 @rei #2 楼 @huacnlee 实在谢谢了

#8 楼 @somejump 和我猜的一模一样……………

需要 登录 后方可回复, 如果你还没有账号请 注册新账号