我是小白,第二次配置 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 解析呢,想先测试一下,结果就这样了
解决方法:看 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 跪了