2

用这个拔掉我的头发后,恐怕我将不得不再次将那些知识变得更聪明......再次!

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

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

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

广告据我所知,所有内容都遵循了 T(或者 Tea 或者可能是 Tee,我以前从未见过这句话被写下来)。该坐席现在在我的服务器上运行,但未加载样式表和 javascript 文件。加载公共/资产中存在的 .css 文件时,它似乎与Rails 3.1 404 错误或样式表或 JavaScript 文件的 Rails 404 错误相同。虽然第一个没有答案,但后者表明这可能是网络服务器不寻找静态资源的问题,也许不应该。

有谁知道可能是什么原因造成的?

一些事实:

  • 首先,我遇到了一些关于预编译的严重错误,并注意到我的个人 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)
    

如您所见,没有更多关于预编译的投诉

这是生成的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>

如果我点击它们,这些链接会生成一个 nginx 404,但是如果我将 url 更改为 http://<ipaddress>/public/assets/application-dd445a707b7e96c914afd14bf1f02bbc.css我会生成一个 railsThe 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

最后,我不太清楚的领域,只是它全部覆盖,这里是 railscast 中用于配置 capistrano、nginx 和 unicorn 的文件:

我已取消注释load 'deploy/assets'in capfile

部署.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;
}

独角兽.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

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

unicorn_init.sh 也是 chmod +x。

抱歉,信息可能会过载,但如果它有助于为您提供我的问题的快照,它有助于获得诊断,非常感谢任何帮助。

提前致谢

4

2 回答 2

15

If nginx isn't serving static assets it's probably due to a typo in the root directive. Triple check that it's correctly referencing your rails public folder.

In my case, I forgot that I moved my app to a different location. This didn't break the app entirely as nginx was still able to proxy_pass requests via my unicorn socket which hadn't moved.

Don't assume your app is wired up properly if you get responses from your rails app. The root directive must point to the correct folder to resolve static assets.

server {
  ...

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

  ...
}
于 2012-09-26T15:06:06.887 回答
2

不太确定如何执行此操作(我不确定回答您自己问题的礼仪)但我可能已经解决了问题,基本上,我认为虽然 nginx 服务器正在重新启动,但独角兽服务器没有(或者如果所以效果没有生效)。我发现这个帖子重启独角兽问题(capistrano)的解决方案似乎已经成功了。现在我只需要弄清楚如何将它连接到我的代码中!

于 2012-07-10T13:04:06.417 回答