Capistrano运行没有错误,但我的页面仍然没有显示

时间:2015-12-16 14:40:11

标签: ruby-on-rails nginx capistrano unicorn

我已经设置了我的rails应用程序并正在部署它,我遇到了一个问题,我不知道如何解决。我运行cap production deploy并且运行没有任何错误。

我的capfile:

...
require 'capistrano/deploy'
require 'capistrano/rails'

production.rb

role :app, %w{deploy@ip}
role :web, %w{deploy@ip}
role :db,  %w{deploy@ip}

部署:

set :application, 'mainapp'
set :repo_url, 'git@github.com:almnes/mainapp.git'
set :deploy_to, '/opt/www/mainapp'
set :user, 'deploy'
set :linked_dirs, %w{log tmp/pids tmp/cache tmp/sockets}

namespace :deploy do

  %w[start stop restart].each do |command|
    desc 'Manage Unicorn'
    task command do
      on roles(:app), in: :sequence, wait: 1 do
        execute "/etc/init.d/unicorn_#{fetch(:application)} #{command}"
      end      
    end
  end

  after :publishing, :restart

end

我的unicorn.log正在制作中:

I, [2015-12-16T09:15:39.575346 #1128]  INFO -- : master complete
I, [2015-12-16T09:15:50.979431 #1131]  INFO -- : listening on addr=/tmp/unicorn.mainapp.sock fd=10
I, [2015-12-16T09:15:50.983388 #1131]  INFO -- : worker=0 spawning...
I, [2015-12-16T09:15:50.984460 #1131]  INFO -- : master process ready
I, [2015-12-16T09:15:50.989710 #1134]  INFO -- : worker=0 spawned pid=1134
I, [2015-12-16T09:15:50.989947 #1134]  INFO -- : Refreshing Gem list
I, [2015-12-16T09:15:55.351175 #1134]  INFO -- : worker=0 ready
I, [2015-12-16T09:18:54.364835 #1131]  INFO -- : reloading config_file=/opt/www/mainapp/current/config/unicorn.rb
I, [2015-12-16T09:18:54.373599 #1131]  INFO -- : done reloading config_file=/opt/www/mainapp/current/config/unicorn.rb
I, [2015-12-16T09:18:54.527631 #1131]  INFO -- : reaped #<Process::Status: pid 1134 exit 0> worker=0
I, [2015-12-16T09:18:54.527945 #1131]  INFO -- : worker=0 spawning...
I, [2015-12-16T09:18:54.530843 #3986]  INFO -- : worker=0 spawned pid=3986
I, [2015-12-16T09:18:54.531103 #3986]  INFO -- : Refreshing Gem list
I, [2015-12-16T09:18:57.170960 #3986]  INFO -- : worker=0 ready
I, [2015-12-16T09:23:19.949319 #1131]  INFO -- : reaped #<Process::Status: pid 3986 exit 0> worker=0
I, [2015-12-16T09:23:19.949764 #1131]  INFO -- : master complete
I, [2015-12-16T09:23:30.987309 #1132]  INFO -- : listening on addr=/tmp/unicorn.mainapp.sock fd=10
I, [2015-12-16T09:23:30.987889 #1132]  INFO -- : worker=0 spawning...
I, [2015-12-16T09:23:30.988912 #1132]  INFO -- : master process ready
I, [2015-12-16T09:23:31.000132 #1135]  INFO -- : worker=0 spawned pid=1135
I, [2015-12-16T09:23:31.000368 #1135]  INFO -- : Refreshing Gem list
I, [2015-12-16T09:23:35.040186 #1135]  INFO -- : worker=0 ready

生产日志只包含迁移(成功)。

nginx的:

upstream unicorn_mainapp {
  server unix:/tmp/unicorn.mainapp.sock fail_timeout=0;
}

server {
  listen 80 default deferred;
  server_name: ip_address;
  root /opt/www/mainapp/current/public;

  location ~ ^/assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  try_files $uri/index.html $uri @unicorn;
  location @unicorn {
    proxy_set_header Host $http_host;  
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_redirect off;
    proxy_pass http://unicorn_mainapp;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 10;
}

和我的独角兽生产方配置:

#!/bin/sh
### BEGIN INIT INFO
# Provides:          unicorn
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Manage unicorn server
# Description:       Start, stop, restart unicorn server for a specific application.
### END INIT INFO
set -e

# Feel free to change any of the following variables for your app:
TIMEOUT=${TIMEOUT-60}
APP_ROOT=/opt/www/mainapp/current
PID=$APP_ROOT/tmp/pids/unicorn.pid
CMD="cd $APP_ROOT; bundle exec unicorn -D -c $APP_ROOT/config/unicorn.rb -E production"
AS_USER=deploy
set -u

OLD_PIN="$PID.oldbin"

sig () {
  test -s "$PID" && kill -$1 `cat $PID`
}

oldsig () {
  test -s $OLD_PIN && kill -$1 `cat $OLD_PIN`
}

run () {
  if [ "$(id -un)" = "$AS_USER" ]; then
    eval $1
  else
    su -c "$1" - $AS_USER
  fi
}

case "$1" in
start)
  sig 0 && echo >&2 "Already running" && exit 0
  run "$CMD"
  ;;
stop)
  sig QUIT && exit 0
  echo >&2 "Not running"
  ;;
force-stop)
  sig TERM && exit 0
  echo >&2 "Not running"
  ;;
restart|reload)
  sig HUP && echo reloaded OK && exit 0
  echo >&2 "Couldn't reload, starting '$CMD' instead"
  run "$CMD"
  ;;
upgrade)
  if sig USR2 && sleep 2 && sig 0 && oldsig QUIT
  then
    n=$TIMEOUT
    while test -s $OLD_PIN && test $n -ge 0
    do
      printf '.' && sleep 1 && n=$(( $n - 1 ))
    done
    echo

    if test $n -lt 0 && test -s $OLD_PIN
    then
      echo >&2 "$OLD_PIN still exists after $TIMEOUT seconds"
      exit 1
    fi
    exit 0
  fi
  echo >&2 "Couldn't upgrade, starting '$CMD' instead"
  run "$CMD"
  ;;
reopen-logs)
  sig USR1
  ;;
*)
  echo >&2 "Usage: $0 "
  exit 1
  ;;
esac

最后我的应用程序unicorn.rb文件:

root = "/opt/www/mainapp/current"
working_directory root
pid "#{root}/tmp/pids/unicorn.pid"
stderr_path "#{root}/log/unicorn.log"
stdout_path "#{root}/log/unicorn.log"

listen "/tmp/unicorn.mainapp.sock"
worker_processes 1
timeout 30

如果它可能有用,我试图在IP上运行它,而不是域。我不知道出了什么问题,但我觉得这与nginx有关。

1 个答案:

答案 0 :(得分:0)

在nginx.conf中设置Unicorn

Unicorn上有4个定义,在你的例子中,unicorn_mainapp需要匹配或者套接字不对齐,你会得到nginx套接字错误。我已将它们添加到下面的示例init文件中。

  upstream unicorn_mainapp {
      server unix:/tmp/unicorn.mainapp.sock fail_timeout=0;
    }

server {
  listen 80 default deferred;
  server_name: example.com
  root /opt/www/mainapp/current/public;

  location ~ ^/assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  try_files $uri/index.html $uri @unicorn_mainapp;
  location @unicorn_mainapp {
    proxy_set_header Host $http_host;  
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_redirect off;
    proxy_pass http://unicorn_mainapp;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 10;
}

套接字文件权限可能是另一个棘手的问题,但是如上所述设置独角兽,让我知道你看到了什么。