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

Daz*_*win 2 ruby-on-rails nginx unicorn ruby-on-rails-3.2 ubuntu-12.04

用这个把我的头发拉出来之后,我恐怕不得不再把那些更明智的知识转过来了.....

好的,这就是问题,我正在关注以下两个railscast:

在生产中部署到vps
资产管道

我的设置:
Ubuntu Server 12.04
nginx
unicorn
ruby 1.9.3p125
rails 3.2.2

据我所知,一切都被T(或茶或者也许是Tee,我从未见过这句话写过).现在,sit正在我的服务器上运行,但未加载样式表和javascript文件.它似乎与Rails 3.1 404相同的问题在加载公共/资产中存在的.css文件样式表或JavaScript文件的Rails 404错误时出错.虽然第一个没有答案,但后者表明,Web服务器可能不会寻找静态资源,也许不应该这样做.

有人知道是什么原因引起的吗?

一些事实:

  • 首先,我得到了一些关于预编译的令人讨厌的错误,并注意到我的个人css/js文件已经缩小为他们的应用程序对应物,因此我在ruby助手中更改了他们的声明以反映这一点.

  • 当我改为config.serve_static_assets = truein 时,应用程序在本地工作config/environments/production.rb

  • 这是我的服务器上的生产.log的转储

    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)
    
    Run Code Online (Sandbox Code Playgroud)

正如您所看到的,不再有关于预编译的投诉

这是生成的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>
Run Code Online (Sandbox Code Playgroud)

这些链接生成一个nginx 404,如果我点击它们,但如果我更改网址, http://<ipaddress>/public/assets/application-dd445a707b7e96c914afd14bf1f02bbc.css我得到一个轨道生成The page you were looking for doesn't exist.

这是我服务器上/ public/assets的重要(我认为)内容

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
Run Code Online (Sandbox Code Playgroud)

最后,我不是那么清楚的区域,只是如此全部覆盖,这里是用于配置capistrano,nginx和unicorn的railscast中使用的文件:

load 'deploy/assets'在capfile中取消注释了

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
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

unicorn.rb

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
Run Code Online (Sandbox Code Playgroud)

unicorn_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
Run Code Online (Sandbox Code Playgroud)

unicorn_init.sh也是chmod + x.

对于信息人员的潜在超载感到抱歉,但如果它有助于为您提供我的问题的快照,则有助于获得诊断并且非常感谢任何帮助.

提前致谢

Dan*_*nor 15

如果nginx没有提供静态资产,那可能是由于root指令中的拼写错误.三重检查它是否正确引用rails公用文件夹.

在我的情况下,我忘记了我将我的应用程序移动到其他位置.这并没有完全破坏应用程序,因为nginx仍然可以通过我没有移动的独角兽套接字proxy_pass请求.

如果您从rails应用程序获得响应,请不要认为您的应用程序已正确连接.root指令必须指向正确的文件夹来解析静态资产.

server {
  ...

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

  ...
}
Run Code Online (Sandbox Code Playgroud)