Ruby on rails Rails 3.2.2使用capistrano部署后,在样式表和js资产上获得404

Ruby on rails Rails 3.2.2使用capistrano部署后,在样式表和js资产上获得404,ruby-on-rails,nginx,ruby-on-rails-3.2,unicorn,ubuntu-12.04,Ruby On Rails,Nginx,Ruby On Rails 3.2,Unicorn,Ubuntu 12.04,在用这本书把我的头发拔出来之后,我恐怕我将不得不再次把那些人的知识变得更聪明了 好的,问题出在这里,我遵循以下两条铁路路线: 我的设置: Ubuntu服务器12.04 nginx 独角兽 ruby 1.9.3p125 轨道3.2.2 据我所知,一切都被遵循到了一个T(或茶或可能是一个T恤,我从来没有见过这个短语写下来)。sit现在正在我的服务器上运行,但是没有加载样式表和javascript文件。这似乎是与or相同的问题。虽然第一个问题没有答案,但后一个问题表明web服务器不寻找静态资源可能

在用这本书把我的头发拔出来之后,我恐怕我将不得不再次把那些人的知识变得更聪明了

好的,问题出在这里,我遵循以下两条铁路路线:


我的设置:
Ubuntu服务器12.04
nginx
独角兽
ruby 1.9.3p125
轨道3.2.2

据我所知,一切都被遵循到了一个T(或茶或可能是一个T恤,我从来没有见过这个短语写下来)。sit现在正在我的服务器上运行,但是没有加载样式表和javascript文件。这似乎是与or相同的问题。虽然第一个问题没有答案,但后一个问题表明web服务器不寻找静态资源可能有问题,而且可能不应该这样做

有人知道这是什么原因吗

一些事实:

  • 首先,我在预编译时遇到了一些令人讨厌的错误,注意到我的单个css/js文件已经缩小到应用程序的对应部分,因此我在ruby helpers中更改了它们的声明以反映这一点

  • 当我在
    config/environments/production.rb

  • 这是一个生产转储。请登录我的服务器

    Started GET "/main" for 92.237.28.149 at 2012-07-09 23:08:02 +0100
       Processing by MainController#index as HTML
         Rendered shared/_login_form.html.erb (0.4ms)
         Rendered shared/_footer.html.erb (0.4ms)
      Rendered shared/_details_form.html.erb (7.7ms)
      Rendered main/index.html.erb within layouts/main_logged (17.1ms)
    Completed 200 OK in 21ms (Views: 20.5ms | ActiveRecord: 0.0ms)
    Compiled jquery.js  (2ms)  (pid 8253)
    Compiled jquery_ujs.js  (0ms)  (pid 8253)
    Compiled admin_user.js  (227ms)  (pid 8253)
    Compiled construction.js  (227ms)  (pid 8253)
    Compiled google-map.js  (0ms)  (pid 8253)
    Compiled jquery-1.2.6.min.js  (1ms)  (pid 8253)
    Compiled jquery-ui-personalized-1.5.2.packed.js  (0ms)  (pid 8253)
    Compiled login.js  (227ms)  (pid 8253)
    Compiled main.js  (222ms)  (pid 8253)
    Compiled menu.js  (223ms)  (pid 8253)
    Compiled order_steps.js  (225ms)  (pid 8253)
    Compiled register_user.js  (223ms)  (pid 8253)
    Compiled sprinkle.js  (0ms)  (pid 8253)   
    Compiled sticky_basket/jquery.easing.1.3.js  (0ms)  (pid 8253)
    Compiled sticky_basket/stickysidebar.jquery.js  (0ms)  (pid 8253)
    Compiled sticky_basket/stickysidebar.jquery.min.js  (0ms)  (pid 8253)
    Compiled user.js  (292ms)  (pid 8253)
    Compiled application.js  (2155ms)  (pid 8253)
    Compiled admin_user.css  (12ms)  (pid 8253)
    Compiled construction.css  (0ms)  (pid 8253)
    Compiled construction.css  (1ms)  (pid 8253)
    Compiled lightbox.css  (0ms)  (pid 8253)
    Compiled login.css  (1ms)  (pid 8253)
    Compiled main.css  (0ms)  (pid 8253)
    Compiled login.css  (1ms)  (pid 8253)
    Compiled main.css  (0ms)  (pid 8253)
    Compiled main.css  (2ms)  (pid 8253)
    Compiled menu.css  (1ms)  (pid 8253)
    Compiled menu_style.css  (0ms)  (pid 8253)
    Compiled normalise.css  (0ms)  (pid 8253)
    Compiled order_steps.css  (0ms)  (pid 8253)
    Compiled order_steps.css  (1ms)  (pid 8253)
    Compiled register_user copy.css  (0ms)  (pid 8253)
    Compiled register_user.css  (0ms)  (pid 8253)
    Compiled register_user.css  (1ms)  (pid 8253)
    Compiled tab_style.css  (0ms)  (pid 8253)
    Compiled user.css  (1ms)  (pid 8253)
    Compiled application.css  (236ms)  (pid 8253)
    Compiled jquery.js  (2ms)  (pid 8253)
    Compiled jquery_ujs.js  (0ms)  (pid 8253)
    Compiled admin_user.js  (273ms)  (pid 8253)
    Compiled construction.js  (225ms)  (pid 8253)
    Compiled google-map.js  (0ms)  (pid 8253)
    Compiled jquery-1.2.6.min.js  (1ms)  (pid 8253)
    Compiled jquery-ui-personalized-1.5.2.packed.js  (0ms)  (pid 8253)
    Compiled login.js  (269ms)  (pid 8253)
    Compiled main.js  (225ms)  (pid 8253)
    Compiled menu.js  (224ms)  (pid 8253)
    Compiled order_steps.js  (226ms)  (pid 8253)
    Compiled register_user.js  (224ms)  (pid 8253)
    Compiled sprinkle.js  (0ms)  (pid 8253)   
    Compiled sticky_basket/jquery.easing.1.3.js  (0ms)  (pid 8253)
    Compiled sticky_basket/stickysidebar.jquery.js  (0ms)  (pid 8253)
    Compiled sticky_basket/stickysidebar.jquery.min.js  (0ms)  (pid 8253)
    Compiled user.js  (224ms)  (pid 8253)
    Compiled application.js  (2096ms)  (pid 8253)
    Compiled admin_user.css  (12ms)  (pid 8253)
    Compiled construction.css  (0ms)  (pid 8253)
    Compiled construction.css  (1ms)  (pid 8253)
    Compiled lightbox.css  (0ms)  (pid 8253)
    Compiled login.css  (1ms)  (pid 8253)
    Compiled main.css  (0ms)  (pid 8253)
    Compiled login.css  (1ms)  (pid 8253)
    Compiled main.css  (0ms)  (pid 8253)
    Compiled main.css  (1ms)  (pid 8253)
    Compiled menu.css  (2ms)  (pid 8253)
    Compiled menu_style.css  (0ms)  (pid 8253)
    Compiled normalise.css  (0ms)  (pid 8253)
    Compiled order_steps.css  (0ms)  (pid 8253)
    Compiled order_steps.css  (1ms)  (pid 8253)
    Compiled register_user copy.css  (0ms)  (pid 8253)
    Compiled register_user.css  (0ms)  (pid 8253)
    Compiled register_user.css  (1ms)  (pid 8253)
    Compiled tab_style.css  (0ms)  (pid 8253)
    Compiled user.css  (1ms)  (pid 8253)
    Compiled application.css  (179ms)  (pid 8253)
    
    
    Started GET "/main" for 92.237.28.149 at 2012-07-09 23:15:00 +0100
    Processing by MainController#index as HTML   
      Rendered shared/_login_form.html.erb (1.2ms)
      Rendered shared/_footer.html.erb (1.1ms)
      Rendered shared/_details_form.html.erb (15.5ms)
      Rendered main/index.html.erb within layouts/main_logged (80.9ms)
    Completed 200 OK in 142ms (Views: 140.4ms | ActiveRecord: 0.0ms)
    
    
    Started GET "/main" for 92.237.28.149 at 2012-07-09 23:33:54 +0100
    Processing by MainController#index as HTML
      Rendered shared/_login_form.html.erb (1.2ms)
      Rendered shared/_footer.html.erb (1.1ms)
      Rendered shared/_details_form.html.erb (15.6ms)
      Rendered main/index.html.erb within layouts/main_logged (80.7ms)
    Completed 200 OK in 142ms (Views: 140.0ms | ActiveRecord: 0.0ms)
    
    application-7c90c01effcf9d8593e28e4c4b716ac2.css
    application-7c90c01effcf9d8593e28e4c4b716ac2.css.gz
    application.css
    application.css.gz
    application-dd445a707b7e96c914afd14bf1f02bbc.css
    application-dd445a707b7e96c914afd14bf1f02bbc.css.gz
    application-def3878140cda305cb0ca5ef745fd758.js
    application-def3878140cda305cb0ca5ef745fd758.js.gz
    application.js
    application.js.gz
    
正如您所看到的,对于预编译没有更多的抱怨

下面是生成的html

  <title>HTML Snippet</title>
  <link href="/assets/application-dd445a707b7e96c914afd14bf1f02bbc.css" media="screen" rel="stylesheet" type="text/css" />
  <script src="/assets/application-def3878140cda305cb0ca5ef745fd758.js" type="text/javascript"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js" type="text/javascript"></script>
  <script src="http://cloud.github.com/downloads/malsup/cycle/jquery.cycle.all.latest.js" type="text/javascript"></script>
最后,我不太清楚的地方,也不太清楚,这里是railscast中用于配置capistrano、nginx和unicorn的文件:

我已经取消了capfile中加载'deploy/assets'的注释

deploy.rb

require "bundler/capistrano"

server "92.237.28.149", :web, :app, :db, primary: true

set :application, "little_oven_deploy"
set :user, "daz"
set :deploy_to, "/home/#{user}/apps/#{application}"
set :deploy_via, :remote_cache
set :use_sudo, false

set :scm, "git"
set :repository, "git@bitbucket.org:DanBaldwin/little_oven_deploy.git"
set :branch, "master"

default_run_options[:pty] = true
ssh_options[:forward_agent] = true

after "deploy", "deploy:cleanup" # keep only the last 5 releases

namespace :deploy do
  %w[start stop restart].each do |command|
    desc "#{command} unicorn server"
    task command, roles: :app, except: {no_release: true} do
      run "/etc/init.d/unicorn_#{application} #{command}"
    end
  end

  task :setup_config, roles: :app do
    sudo "ln -nfs #{current_path}/config/nginx.conf /etc/nginx/sites-enabled/#{application}"
    sudo "ln -nfs #{current_path}/config/unicorn_init.sh /etc/init.d/unicorn_#{application}"
    run "mkdir -p #{shared_path}/config"
    put File.read("config/database.example.yml"), "#{shared_path}/config/database.yml"
    puts "Now edit the config files in #{shared_path}."
  end
  after "deploy:setup", "deploy:setup_config"

  task :symlink_config, roles: :app do
    run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
  end
  after "deploy:finalize_update", "deploy:symlink_config"

  desc "Make sure local git is in sync with remote."
  task :check_revision, roles: :web do
    unless `git rev-parse HEAD` == `git rev-parse origin/master`
      puts "WARNING: HEAD is not the same as origin/master"
      puts "Run `git push` to sync changes."
      exit
    end
  end
  before "deploy", "deploy:check_revision"
end
nginx.conf

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

server {
  listen 80 default deferred;
  # server_name example.com;
  root /home/deployer/apps/blog/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 X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://unicorn;
  }

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

root = "/home/daz/apps/little_oven_deploy/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.little_oven_deploy.sock"
worker_processes 2
timeout 30
麒麟_init.sh

#!/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=/home/daz/apps/little_oven_deploy/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=daz
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 <start|stop|restart|upgrade|force-stop|reopen-logs>"
  exit 1
  ;;
esac
#/垃圾箱/垃圾箱
###开始初始化信息
#提供:独角兽
#必需的开始:$remote_fs$syslog
#所需停止:$remote_fs$syslog
#默认开始:2 3 4 5
#默认停止:0 1 6
#简短描述:管理unicorn服务器
#描述:启动、停止、重新启动特定应用程序的unicorn服务器。
###结束初始化信息
set-e
#请随意更改应用程序的以下任何变量:
超时=${TIMEOUT-60}
APP\u ROOT=/home/daz/apps/little\u-roven\u-deploy/current
PID=$APP_ROOT/tmp/pids/unicorn.PID
CMD=“cd$APP\u ROOT;bundle exec unicorn-D-c$APP\u ROOT/config/unicorn.rb-E production”
AS_USER=daz
set-u
OLD_PIN=“$PID.oldbin”
sig(){
测试-s“$PID”和压井-1`cat$PID`
}
oldsig(){
测试-s$OLD_PIN和kill-$1`cat$OLD_PIN`
}
运行(){
如果[“$(id-un)”=“$作为用户”];则
估价1美元
其他的
su-c“$1”-$AS_用户
fi
}
案件“$1”
开始)
信号0&&echo>&2“已在运行”&退出0
运行“$CMD”
;;
(停止)
sig退出&退出0
echo>&2“未运行”
;;
(强制停止)
sig术语&退出0(&F)
echo>&2“未运行”
;;
重新启动(重新加载)
sig HUP和echo已重新加载确定和退出0(&E)
echo>&2“无法重新加载,改为启动“$CMD”
运行“$CMD”
;;
(升级)
如果sig USR2&&sleep 2&&sig 0&&oldsig退出
然后
n=$TIMEOUT
而测试-s$OLD_PIN和测试$n-ge 0
做
printf'.&&sleep 1&&n=$($n-1))
完成
回声
如果测试$n-lt 0和测试-s$OLD\u PIN
然后
echo>&2“$OLD_PIN在$TIMEOUT秒后仍然存在”
出口1
fi
出口0
fi
echo>&2“无法升级,改为启动“$CMD”
运行“$CMD”
;;
重新打开日志)
sig USR1
;;
*)
echo>&2“用法:$0”
出口1
;;
以撒
unicorn_init.sh也是chmod+x

很抱歉,可能会出现信息过载的情况,但如果这有助于您了解我的问题,有助于获得诊断,我们非常感谢您提供的任何帮助


提前感谢

我不太清楚该怎么做(我不确定回答您自己问题的礼仪),但我可能已经解决了问题,基本上,我认为尽管nginx服务器正在重新启动,但unicorn服务器没有(或者如果是这样,效果没有生效)。我发现这篇文章的解决方案似乎奏效了。现在我只需要找出如何将它连接到我的代码中

如果nginx不提供静态资产,可能是由于
root
指令中的输入错误。三次检查它是否正确引用rails公用文件夹

就我而言,我忘了我把我的应用程序移到了另一个位置。这并没有完全破坏应用程序,因为nginx仍然能够通过我的未移动的unicorn套接字
代理传递
请求

如果你从rails应用程序得到响应,不要认为你的应用程序连接正确。root指令必须指向正确的文件夹才能解析静态资源

server {
  ...

  # make absolutely sure this is pointing to your app/public folder
  root /home/deployer/apps/blog/current/public;

  ...
}

回答你自己的问题完全可以!如果这确实是正确的答案,也请接受它。对其他来这里的人(比如我)的准确回答是最重要的目标。这是我错过的三重检查,双重检查没有做到:)谢谢。我在这个问题上犹豫了一个小时。谢谢我读了这篇文章,一直在寻找其他答案,因为我的答案看起来不错。然后我意识到我的配置缺少当前的
。。。。啊。非常感谢。