网页一直是 502 Bad Gateway
Ubuntu 12.04 ruby 1.9.3p392 (2013-02-22 revision 39386) [x86_64-linux] Rails 3.2.13 以上参考 wiki 部署 http://ruby-china.org/wiki/install_ruby_guide 之前一直用 centos 部署 php,最近刚刚开始尝试使用 ubuntu
账号 rails /home/rails/hellworld rails new helloworld,建立项目,scaffold home, rake db:migrate rails s,利用 WEBrick,ip:3000 访问正常
==========上面很简单,到 unicorn 和 nginx 就难死了,接下来一大堆坑了========== Nginx apt-get install nginx
Unicorn gem install unicorn
搞了两天没搞定,就直接 copy ruby-china 的 unicron.rb,稍微改动一点点
vim /home/rails/hellworld/config/unicorn.rb
module Rails
class <<self
def root
File.expand_path("../..", __FILE__)
end
end
end
puts Rails.root
rails_env = ENV["RAILS_ENV"] || "production"
preload_app true
working_directory Rails.root
pid "#{Rails.root}/tmp/pids/unicorn.pid"
stderr_path "#{Rails.root}/log/unicorn.log"
stdout_path "#{Rails.root}/log/unicorn.log"
listen 5000, :tcp_nopush => false
listen "/tmp/unicorn.ruby-china.sock"
worker_processes 6
timeout 120
if GC.respond_to?(:copy_on_write_friendly=)
GC.copy_on_write_friendly = true
end
before_exec do |server|
ENV["BUNDLE_GEMFILE"] = "#{Rails.root}/Gemfile"
end
before_fork do |server, worker|
old_pid = "#{Rails.root}/tmp/pids/unicorn.pid.oldbin"
if File.exists?(old_pid) && server.pid != old_pid
begin
Process.kill("QUIT", File.read(old_pid).to_i)
rescue Errno::ENOENT, Errno::ESRCH
puts "Send 'QUIT' signal to unicorn error!"
end
end
end
创建一下
sudo touch /tmp/unicorn.ruby-china.sock
sudo chown rails /tmp/unicorn.ruby-china.sock
’‘’
编辑nginx.conf
sudo vim /etc/nginx/nginx.conf
```BashLexer
user rails;
worker_processes 1;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
accept_mutex off;
}
http {
include mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/nginx.access.log combined;
sendfile on;
tcp_nopush on;
tcp_nodelay off;
gzip on;
gzip_http_version 1.0;
gzip_proxied any;
gzip_min_length 500;
gzip_disable "MSIE [1-6]\.";
gzip_types text/plain text/css
text/comma-separated-values
text/javascript application/x-javascript
application/atom+xml;
upstream unicorn_server {
server unix:/tmp/unicorn.ruby-china.sock fail_timeout=0;
}
server {
listen 80;
server_name www.ruby1.com ruby1.com;
client_max_body_size 4G;
root /home/rails/hellworld/public;
#access_log logs/host.access.log main;
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://unicorn_server;
}
}
}
域名 rub1.com 只是测试的,vim /etc/hosts 都改回指向 127.0.0.1
浏览器测试 网页一直是 502 Bad Gateway
tail -f /var/log/nginx/error.log 跟踪 nginx error.log
rver: www.ruby1.com, request: "GET /favicon.ico HTTP/1.1", upstream: "http://unix:/tmp/unicorn.ruby-china.sock:/favicon.ico", host: "www.ruby1.com"
2013/04/19 06:10:36 [error] 1152#0: *42 connect() to unix:/tmp/unicorn.ruby-china.sock failed (111: Connection refused) while connecting to upstream, client: 10.0.1.88, server: www.ruby1.com, request: "GET /favicon.ico HTTP/1.1", upstream: "http://unix:/tmp/unicorn.ruby-china.sock:/favicon.ico", host: "www.ruby1.com"
111: Connection refused
搞了好久都没法搞定 求高手指点