部署 请各位帮忙看看怎么个问题,一直无法解决

chunlea · 2012年08月30日 · 最后由 chunlea 回复于 2012年09月01日 · 3610 次阅读

我终于可以使用capistrano进行部署了,可是却出现了难以预见的错误,Google 了很久也没有解决。 系统环境: Linode 512, RVM Mulit-User, Ruby 1.9.3, Rails 3.2.8, PostgreSQL 9.1其他基本都是最新,系统是Ubuntu12.06 64bit LTS 问题如下: 看似好像解决了,现在重点是问题二问题一 部署成功之后,老是提示无法加载 /asset/ 目录下的内容,但是已经预编译了啊,而且 /asset/ 目录下也有相关的文件,纠结中。 最后开启了 rails 的production.rb中的config.serve_static_assets = true 解决了问题,但是怀恨在心啊! 我的 nginx 文件配置:

# /etc/nginx/sites-enabled/slyu 
upstream slyu_backend {
    server unix:/tmp/unicorn.slyu.sock fail_timeout=0;
}

server {
        listen 80;
        server_name slyu.chunlea.org;

        root /home/slyu/apps/slyu/current/public;

        location ~ ^/(assets)/  {
      root /home/slyu/apps/slyu/current/public;
      gzip_static on; # to serve pre-gzipped version
      expires max;
      add_header Cache-Control public;
    }

    location / {
          proxy_redirect     off;
          proxy_set_header   Host $host;
          proxy_set_header   X-Forwarded-Host $host;
          proxy_set_header   X-Forwarded-Server $host;
          proxy_set_header   X-Real-IP        $remote_addr;
          proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
          proxy_buffering    on;
          proxy_pass         http://slyu_backend;
        }
}

我的Unicorn.rbdeploy.rb是参照 Ruby-China 的修改的。应该不会有问题,就是替换了路径。 希望各位指点一二。 问题二: 系统可以访问,但是老是出现 500 错误。错误提示:

Started GET "/admin/users?page=2" for 112.234.126.91 at 2012-08-30 01:23:14 +0000
Processing by Admin::UsersController#index as HTML
  Parameters: {"page"=>"2"}
  Rendered admin/users/index.html.erb within layouts/application (42.2ms)
Completed 500 Internal Server Error in 242ms

ActionView::Template::Error (PG::Error: ERROR:  prepared statement "a1" already exists
:             SELECT COUNT(*)
            FROM pg_class c
            LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
            WHERE c.relkind in ('v','r')
            AND c.relname = $1
            AND n.nspname = ANY (current_schemas(false))
):
    9:     </tr>
    10:   </thead>
    11:   <tbody>
    12:       <% @users.each do |user| %>
    13:       <tr id="#user_<%= user.id %>">
    14:         <td><%= link_to user.userid, admin_user_path(user) %></td>
    15:         <td><%= user.name %></td>
  app/views/admin/users/index.html.erb:12:in `_app_views_admin_users_index_html_erb__1504903244061183802_16634340'
  app/controllers/admin/users_controller.rb:6:in `index'

我的users_controller.rb

def index
  @page_title=t('page_title.admin.users')
  @users = User.paginate(:page => params[:page], :per_page => 10)

  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @users }
  end

相关的 view:

<%- model_class = User -%>
<div class="page-header">
  <h1><%=t '.title', :default => model_class.model_name.human %></h1>
</div>
<table class="table table-striped">
  <thead>
    <tr>
      <th><%= model_class.human_attribute_name(:userid) %></th>
      <th><%= model_class.human_attribute_name(:name) %></th>
      <th><%= model_class.human_attribute_name(:email) %></th>
      <th><%= model_class.human_attribute_name(:created_at) %></th>
      <th><%=t '.actions', :default => t("helpers.actions") %></th>
    </tr>
  </thead>
  <tbody>
      <% @users.each do |user| %>
      <tr id="#user_<%= user.id %>">
        <td><%= link_to user.userid, admin_user_path(user) %></td>
        <td><%= user.name %></td>
        <td><%= user.email %></td>
        <td><%= user.created_at %></td>
        <td>
          <%= link_to t('.edit', :default => t("helpers.links.edit")),
                      edit_admin_user_path(user), :class => 'btn btn-mini' %>
          <%= link_to t('.destroy', :default => t("helpers.links.destroy")),
                      admin_user_path(user),
                      :method => :delete,
                      :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) },
                      :class => 'btn btn-mini btn-danger'
                      %>
        </td>
      </tr>
    <% end %>
    </tbody>
</table>
<div class="apple_pagination">
    <div clas="page_info">
          <%= page_entries_info @posts %>
        </div>
  <%= will_paginate @users %>
</div>

<%= link_to t('.new', :default => t("helpers.links.new")),
            user_registration_path,
            :method => :post,
            :class => 'btn btn-primary' %>

到底是什么问题呢??

源代码在https://github.com/slyu/slyu

老是提示无法加载 /asset/ 目录下的内容

怎么提示的,在那个地方提示的?

#1 楼 @huacnlee 浏览器直接 500,看的日志。我有一个大的背景,后来其他的图片显示了,就是死活不现实那张图片。图片我放在了 /app/asset/images下,用asset_path 'background.jpg'引用。 还有一次是直接不加载任何样式表,就裸奔 CSS 了。 不过,非常不解的是问题二

去掉 root 试试 location ~ ^/(assets)/ { #root /path/to/public; gzip_static on; # to serve pre-gzipped version expires max; add_header Cache-Control public; }

#3 楼 @jimrokliu 谢谢,我重新部署看看

#3 楼 @jimrokliu 问题一好像解决了。现在重点是问题二。无语啊,怎么就让我碰上了呢?

问题二已经成功解决,谢谢。

WU Jun 5 months ago
A simple note:
pg gem uses prepare statement in production env by default, which is different from MySQL.
This can be an issue if you are using unicorn. You may got PGError: ERROR: prepared statement "a3" already exists after several requests.
To solve this, make sure each unicorn process uses its own database connection by adding this to unicorn config file:
===================================
after_fork do |server, worker|
  ActiveRecord::Base.establish_connection
end
===================================
Hope this helps :)
需要 登录 后方可回复, 如果你还没有账号请 注册新账号