新手问题 Nginx 貌似连不上 Unicorn

woody1983 · 2012年11月20日 · 最后由 woody1983 回复于 2012年11月20日 · 6530 次阅读

Unicorn Configure


# unicorn_rails -c /data/github/current/config/unicorn.rb -E production -D

rails_env = ENV['RAILS_ENV'] || 'production'

# 16 workers and 1 master
#worker_processes (rails_env == 'production' ? 16 : 4)
worker_processes 6

# Load rails+github.git into the master before forking workers
# for super-fast worker spawn times
preload_app true

# Restart any workers that haven't responded in 30 seconds
timeout 30

# Listen on a Unix data socket
listen '/tmp/unicorn.sock', :backlog => 2048

root = "/www"
working_directory root
# Define Log paths
stderr_path "#{root}/log/unicorn.log"
stdout_path "#{root}/log/unicorn.log"
##
# REE

# http://www.rubyenterpriseedition.com/faq.html#adapt_apps_for_cow
if GC.respond_to?(:copy_on_write_friendly=)
  GC.copy_on_write_friendly = true
end


before_fork do |server, worker|
  ##
  # When sent a USR2, Unicorn will suffix its pidfile with .oldbin and
  # immediately start loading up a new version of itself (loaded with a new
  # version of our app). When this new Unicorn is completely loaded
  # it will begin spawning workers. The first worker spawned will check to
  # see if an .oldbin pidfile exists. If so, this means we've just booted up
  # a new Unicorn and need to tell the old one that it can now die. To do so
  # we send it a QUIT.
  #
  # Using this method we get 0 downtime deploys.

  old_pid = '/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
      # someone else did our job for us

Nginx configure


user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

upstream unicorn {
    server unix:/tmp/unicorn.sock fail_timeout=0;
}

server {
        listen 8081 default deferred;
        server_name  localhost;
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://192.168.103.212:3000;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

}


    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;


    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}


搞的火大~中间还跑去弄 apache 试试

unicorn 启动貌似是正常 pig 和 socket 都正常 但浏览器访问就是打不开

[root@mysql-test-woody www]# bundle exec unicorn_rails -c config/unicorn.rb -E production -D
[root@mysql-test-woody www]# ps aux | grep
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.
[root@mysql-test-woody www]# ps aux | grep 'unicorn'
root      9510 32.1  3.6  45072 37368 ?        Sl   15:06   0:04 unicorn_rails master -c config/unicorn.rb -E production -D
git       9514  0.1  3.4  45116 35776 ?        Sl   15:06   0:00 unicorn_rails worker[0] -c config/unicorn.rb -E production -D
git       9517  0.1  3.4  45116 35776 ?        Sl   15:06   0:00 unicorn_rails worker[1] -c config/unicorn.rb -E production -D
git       9520  0.1  3.4  45116 35776 ?        Sl   15:06   0:00 unicorn_rails worker[2] -c config/unicorn.rb -E production -D
git       9523  0.1  3.4  45116 35776 ?        Sl   15:06   0:00 unicorn_rails worker[3] -c config/unicorn.rb -E production -D
git       9526  0.1  3.4  45116 35776 ?        Sl   15:06   0:00 unicorn_rails worker[4] -c config/unicorn.rb -E production -D
git       9529  0.1  3.4  45116 35776 ?        Sl   15:06   0:00 unicorn_rails worker[5] -c config/unicorn.rb -E production -D
root      9543  0.0  0.0   3892   676 pts/1    R+   15:06   0:00 grep unicorn

nginx 虽然配置了 upstream,但是 server 里没有指定 upstream

现在是 404 页面了~ 修改了基础配置 重新安装了 Nginx 结果现在open() "/etc/nginx//html/50x.html" failed 怎么会指向到这里呢


2012/11/20 16:54:23 [error] 15044#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.103.68, server: localhost, request: "GET / HTTP/1.1", upstream: "http://192.168.103.212:5000/", host: "192.168.103.212:8081"
2012/11/20 16:54:23 [error] 15044#0: *1 open() "/etc/nginx//html/50x.html" failed (2: No such file or directory), client: 192.168.103.68, server: localhost, request: "GET / HTTP/1.1", upstream: "http://192.168.103.212:5000/", host: "192.168.103.212:8081"
2012/11/20 16:54:23 [error] 15044#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.103.68, server: localhost, request: "GET /favicon.ico HTTP/1.1", upstream: "http://192.168.103.212:5000/favicon.ico", host: "192.168.103.212:8081"
2012/11/20 16:54:23 [error] 15044#0: *1 open() "/etc/nginx//html/50x.html" failed (2: No such file or directory), client: 192.168.103.68, server: localhost, request: "GET /favicon.ico HTTP/1.1", upstream: "http://192.168.103.212:5000/favicon.ico", host: "192.168.103.212:8081"

将这一部分注销后~现在又成 502 错误~看 Log 是说无法连接到upstream

[error] 15550#0: *1 connect() failed (111: Connection refused) while connecting to upstream,


#error_page   500 502 503 504  /50x.html;
#location = /50x.html {
#    root   html;
#}
需要 登录 后方可回复, 如果你还没有账号请 注册新账号