gpt4 book ai didi

ruby-on-rails - Rails 3.2.2 在使用 capistrano 部署后在样式表和 js Assets 上获得 404

转载 作者:行者123 更新时间:2023-12-02 05:28:45 25 4
gpt4 key购买 nike

用这个把我的头发拉出来后,恐怕我将不得不把那些人的知识变得更聪明......再次!

好的,问题来了,我正在关注以下两个 railscast:

deploying to a vps
asset pipeline in production

我的设置:
Ubuntu 服务器 12.04
nginx
unicorn
ruby 1.9.3p125
rails 3.2.2

据我所知,所有内容都跟在 T 之后(或 Tea 或 Tee,我以前从未见过写下这个短语)。该站点现在正在我的服务器上运行,但未加载样式表和 javascript 文件。这似乎与 Rails 3.1 404 Error when loading .css files that are present in public/assets 是同一个问题或 Rails 404 error for Stylesheet or JavaScript files .虽然第一个没有答案,但后者表明这可能是网络服务器没有寻找静态资源的问题,也许不应该。

有人知道是什么原因造成的吗?

一些事实:

  • 首先,我遇到了一些关于预编译的令人讨厌的错误,并注意到我的个人 css/js 文件已被缩小到它们的应用程序副本中,因此我更改了它们在 ruby​​ 帮助程序中的声明以反射(reflect)这一点。

  • 当我更改为 config.serve_static_assets = true 时,应用程序在本地运行在 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我得到一个 rails 生成 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

最后,我不太清楚的领域,只是涵盖了所有内容,这里是 railscast 中用于配置 capistrano、nginx 和 unicorn 的文件:

我取消了 load 'deploy/assets' 的注释在顶文件中

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

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

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。

对于信息可能过载的家伙,我们深表歉意,但如果它能帮助您大致了解我的问题,那么它有助于获得诊断,非常感谢任何帮助。

提前致谢

最佳答案

如果 nginx 不提供静态资源,那可能是因为 root 指令中的拼写错误。三次检查它是否正确引用了您的 Rails 公用文件夹。

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

如果您收到来自 Rails 应用程序的响应,请不要假设您的应用程序已正确连接。 root 指令必须指向正确的文件夹才能解析静态 Assets 。

server {
...

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

...
}

关于ruby-on-rails - Rails 3.2.2 在使用 capistrano 部署后在样式表和 js Assets 上获得 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11404502/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com