• 非常感谢 @jasl 的指导 已搞定,确实是写瞎了,没有预编译 css 等

    思路过程:

    我已经邦在不同的二级域名上 app1.domain.com app2.domain.com kill 掉 unicorn_rails,分别单独分别独立运行访问,两个都是正常的,所以程序没有写瞎。 应该是 unicorn.rb 或者 nginx.conf 哪里写错了

    nginx 那边每次都是正常的,没有错误提醒

    /usr/local/webserver/nginx/sbin/nginx -t
    nginx: the configuration file /usr/local/webserver/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /usr/local/webserver/nginx/conf/nginx.conf test is successful
    

    然后

    kill -HUP `cat /usr/local/webserver/nginx/logs/nginx.pid`
    

    app 的 unicorn 的错误日志是说 当用 unix domain socket 错误提醒是 (两个 app 都运行不起来)

    /usr/local/rvm/gems/ruby-1.9.3-p374/gems/unicorn-4.6.2/lib/unicorn/socket_helper.rb:134:in `bind_listen': socket=/tmp/ruby-china.sock specified but it is not a socket! (ArgumentError)
    

    改用 tcp socket 的时候,能运行一个 app,谁先运行 unicorn 的谁就先能用,第二个运行似乎也正常了没出现提醒,但是

    等等, 我重新对应一下

    app1
    uicorn.rb
    listen 5001
    listen 127.0.0.1:3000
    

    app1.conf 那边

    listen 127.0.0.1:3000
    

    app2

    uniorn.rb
    listen 5002
    listen 127.0.0.1:3002
    
    app2.conf那边
    listen 127.0.0.1:3002
    

    -t conf 正常,kill 重启 nginx app1 正常 app2 sth wrong

    查看 production.log 发现 不好意思,果然是写瞎了! 开发环境和生产环境的切换没注意到, 看 production.log 发现是没有预编译

    bundle exec rake assets:precompile
    /usr/local/rvm/rubies/ruby-1.9.3-p374/bin/ruby /usr/local/rvm/gems/ruby-1.9.3-p374/bin/rake assets:precompile:all RAILS_ENV=production RAILS_GROUPS=assets
    

    ps:没搞懂为咋当初单独的时候可以,可能是我中间过程端口改来改去的,导致端口对应错了

    非常感谢 @jasl 的指导,问题已解决

  • 看完第七季的嗜血法医,回来浏览器一打开,改了几下问题处理好了。 感谢@yangkit ,这么详细、细致的帮忙解决了困恼我几天的问题 留个支付宝咋的,我转点 4 杯喝咖啡的感谢

  • @yangkit 其他动态文件,出现内部 500 错误

    rails@ubuntu:~/hellworld$ rake routes
        homes GET    /homes(.:format)          homes#index
              POST   /homes(.:format)          homes#create
     new_home GET    /homes/new(.:format)      homes#new
    edit_home GET    /homes/:id/edit(.:format) homes#edit
         home GET    /homes/:id(.:format)      homes#show
              PUT    /homes/:id(.:format)      homes#update
              DELETE /homes/:id(.:format)      homes#destroy
    rails@ubuntu:~/hellworld$ curl -I ruby1.com:5000/homes
    HTTP/1.1 500 Internal Server Error
    Date: Fri, 19 Apr 2013 14:48:21 GMT
    Status: 500 Internal Server Error
    Connection: close
    Content-Type: text/html; charset=utf-8
    Content-Length: 643
    X-Request-Id: 4c91649b0d41f3cb005ab015b93b467a
    X-Runtime: 0.088350
    X-Rack-Cache: miss
    
    rails@ubuntu:~/hellworld$ curl -I ruby1.com:5000/homes/new
    HTTP/1.1 500 Internal Server Error
    Date: Fri, 19 Apr 2013 14:48:47 GMT
    Status: 500 Internal Server Error
    Connection: close
    Content-Type: text/html; charset=utf-8
    Content-Length: 643
    X-Request-Id: 9b925ac79e2892dd36dd1eca4ffe395c
    X-Runtime: 0.009192
    X-Rack-Cache: miss
    
  • @yangkit 真实存在

    /home/rails/hellworld/public
    ls -l
    total 24
    -rwxrwxr-x 1 rails rails  728 Apr 18 15:01 404.html
    -rwxrwxr-x 1 rails rails  711 Apr 18 15:01 422.html
    -rwxrwxr-x 1 rails rails  643 Apr 18 15:01 500.html
    -rwxrwxr-x 1 rails rails    0 Apr 18 15:01 favicon.ico
    -rwxrwxr-x 1 rails rails 5906 Apr 18 15:01 index.html
    -rwxrwxr-x 1 rails rails  204 Apr 18 15:01 robots.txt
    
  • 我用 rails s 再测试一下,3000 端口可以 curl -I ruby1.com:3000/robots.txt

    HTTP/1.1 200 OK 
    Last-Modified: Thu, 18 Apr 2013 22:01:23 GMT
    Content-Type: text/plain
    Content-Length: 204
    Server: WEBrick/1.3.1 (Ruby/1.9.3/2013-02-22)
    Date: Fri, 19 Apr 2013 14:36:07 GMT
    Connection: Keep-Alive
    
  • 其他 get 路径

    curl -I ruby1.com:5000/homes
    HTTP/1.1 500 Internal Server Error
    Date: Fri, 19 Apr 2013 14:35:26 GMT
    Status: 500 Internal Server Error
    Connection: close
    Content-Type: text/html; charset=utf-8
    Content-Length: 643
    X-Request-Id: e865931fc4ff24348215b7541164b1ea
    X-Runtime: 0.112063
    X-Rack-Cache: miss
    ```ruby
    
  • #21 楼 @yangkit

    rails@ubuntu:~/hellworld$ curl -I ruby1.com:5000/robots.txt
    HTTP/1.1 404 Not Found
    Date: Fri, 19 Apr 2013 14:31:40 GMT
    Status: 404 Not Found
    Connection: close
    Content-Type: text/html; charset=utf-8
    Content-Length: 728
    X-Request-Id: f2fb1fad29aec7fd859f2956100b7f8d
    X-Runtime: 0.016250
    X-Rack-Cache: miss
    

    还是 404 指向不正确?

    我记得 nginx.conf 里头 server root /home/rails/hellworld/public;

  • #15 楼 @yangkit 第一次看到结果了 不过是 404

    curl -I www.ruby1.com:5000

    curl -I www.ruby1.com:5000
    HTTP/1.1 404 Not Found
    Date: Fri, 19 Apr 2013 14:23:39 GMT
    Status: 404 Not Found
    Connection: close
    Content-Type: text/html; charset=utf-8
    Content-Length: 728
    X-Request-Id: 8f3e2a14c4c34e3f96b6bd47a917443c
    X-Runtime: 0.030946
    X-Rack-Cache: miss
    

    网页打开 80 端口还是 502 Bad Gateway

  • #16 楼 @yangkit 14 楼已重新编辑了

  • #15 楼 @yangkit ’ps aux|grep unicorn_rails`

    rails     1307 18.4  1.5 137284 46348 ?        Sl   07:18   0:02 unicorn_rails master -c config/unicorn.rb -E production -D                                                                                                      
    rails     1311  0.4  1.4 137284 43768 ?        Sl   07:18   0:00 unicorn_rails worker[0] -c config/unicorn.rb -E production -D                                                                                                   
    rails     1313  0.4  1.4 137284 43512 ?        Sl   07:18   0:00 unicorn_rails worker[1] -c config/unicorn.rb -E production -D                                                                                                   
    rails     1316  0.4  1.4 137284 43528 ?        Sl   07:18   0:00 unicorn_rails worker[2] -c config/unicorn.rb -E production -D                                                                                                   
    rails     1319  0.3  1.4 137284 43548 ?        Sl   07:18   0:00 unicorn_rails worker[3] -c config/unicorn.rb -E production -D                                                                                                   
    rails     1322  0.3  1.4 137284 43568 ?        Sl   07:18   0:00 unicorn_rails worker[4] -c config/unicorn.rb -E production -D                                                                                                   
    rails     1326  0.3  1.4 137284 43588 ?        Sl   07:18   0:00 unicorn_rails worker[5] -c config/unicorn.rb -E production -D
    

    kill -USR2 1307 等等,把所有 pid 都 kill 掉,然后重新

     RAILS_ENV=production bundle exec unicorn_rails -c config/unicorn.rb -E production -D
    /home/rails/hellworld
    master failed to start, check stderr log for details
    
    还是有pid。。。
    cat log/unicorn.log 查看错误信息
    /home/rails/.rvm/gems/ruby-1.9.3-p392/gems/unicorn-4.6.2/lib/unicorn/http_server.rb:193:in `pid=': Already running on PID:1341 (or pid=/home/rails/hellworld/tmp/pids/unicorn.pid is stale) (ArgumentError)
    
  • sudo vim /etc/init.d/unicorn.hellwold

    #!/bin/sh
    set -e
    
    # Feel free to change any of the following variables for your app:
    TIMEOUT=${TIMEOUT-60}
    APP_ROOT=/home/rails/hellworld
    APP_USER=username
    PID=$APP_ROOT/tmp/pids/unicorn.pid
    ENV=production
    CMD="bundle exec unicorn_rails -E $ENV -D -c $APP_ROOT/config/unicorn.rb"
    action="$1"
    set -u
    
    old_pid="$PID.oldbin"
    cd $APP_ROOT || exit 1
    
    sig (){
            test -s "$PID" && kill -$1 `cat $PID`
    }
    
    oldsig (){
            test -s $old_pid && kill -$1 `cat $old_pid`
    }
    case $action in
    start)
            sig 0 && echo >&2 "Already running" && exit 0
            su $APP_USER -c "$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"
            su $APP_USER -c "$CMD"
            ;;
    upgrade)
            if sig USR2 && sleep2 && sig 0 && oldsig QUIT
            then
                    n=$TIMEOUT
                    while test -s $old_pid && test $n -ge 0
                    do
                            printf '.' && sleep 1 && n=$(($n - 1))
                    done
                    echo
    
                    if test $n -lt 0 && test -s $old_pid
                    then
                            echo >&2 "$old_pid still exists after $TIMEOUT seconds"
                            exit1
                    fi
                    exit0
            fi
            echo >&2 "Couldn't upgrade, starting '$CMD' instead"
            su $APP_USER -c "$CMD"
            ;;
    reopen-logs)
            sig USR1
            ;;*)
            echo >&2 "Usage: $0 <start|stop|restart|upgrade|force-stop|reopen-logs>"
            exit 1
            ;;
    esac
    
    rails@ubuntu:/etc/init.d$ sudo chmod +x unicorn.hellwold 
    rails@ubuntu:/etc/init.d$ sudo /etc/init.d/unicorn.hellworld start
    sudo: /etc/init.d/unicorn.hellworld: command not found
    

    @yangkit 是这样添加吗

  • bundle install 装了,在之前部署的时候就 gem install unicorn 现在又在 helloworld 根下执行了几次 bundle install

    RAILS_ENV=production bundle exec unicorn_rails -c config/unicorn.rb -E production -D
    

    运行一下,停顿一下, 然后就下一个$

    rails@ubuntu:~/hellworld$ cat log/unicorn.log 
    I, [2013-04-19T06:54:24.883859 #1226]  INFO -- : Refreshing Gem list
    I, [2013-04-19T06:54:28.800850 #1226]  INFO -- : listening on addr=0.0.0.0:5000 fd=11
    I, [2013-04-19T06:54:28.801181 #1226]  INFO -- : listening on addr=/tmp/unicorn.ruby-china.sock fd=12
    I, [2013-04-19T06:54:28.803877 #1229]  INFO -- : worker=0 spawned pid=1229
    I, [2013-04-19T06:54:28.812031 #1232]  INFO -- : worker=1 spawned pid=1232
    I, [2013-04-19T06:54:28.818480 #1237]  INFO -- : worker=3 spawned pid=1237
    I, [2013-04-19T06:54:28.818915 #1234]  INFO -- : worker=2 spawned pid=1234
    I, [2013-04-19T06:54:28.840173 #1241]  INFO -- : worker=4 spawned pid=1241
    I, [2013-04-19T06:54:28.841920 #1226]  INFO -- : master process ready
    I, [2013-04-19T06:54:28.863606 #1244]  INFO -- : worker=5 spawned pid=1244
    I, [2013-04-19T06:54:28.999588 #1229]  INFO -- : worker=0 ready
    I, [2013-04-19T06:54:29.029094 #1237]  INFO -- : worker=3 ready
    I, [2013-04-19T06:54:29.038157 #1241]  INFO -- : worker=4 ready
    I, [2013-04-19T06:54:29.046324 #1232]  INFO -- : worker=1 ready
    I, [2013-04-19T06:54:29.054201 #1234]  INFO -- : worker=2 ready
    I, [2013-04-19T06:54:29.061516 #1244]  INFO -- : worker=5 ready
    /home/rails/.rvm/gems/ruby-1.9.3-p392/gems/unicorn-4.6.2/lib/unicorn/http_server.rb:193:in `pid=': Already running on PID:1226 (or pid=/home/rails/hellworld/tmp/pids/unicorn.pid is stale) (ArgumentError)
            from /home/rails/.rvm/gems/ruby-1.9.3-p392/gems/unicorn-4.6.2/lib/unicorn/http_server.rb:137:in `start'
            from /home/rails/.rvm/gems/ruby-1.9.3-p392/gems/unicorn-4.6.2/bin/unicorn_rails:209:in `<top (required)>'
            from /home/rails/.rvm/gems/ruby-1.9.3-p392/bin/unicorn_rails:19:in `load'
            from /home/rails/.rvm/gems/ruby-1.9.3-p392/bin/unicorn_rails:19:in `<main>'
            from /home/rails/.rvm/gems/ruby-1.9.3-p392/bin/ruby_noexec_wrapper:14:in `eval'
            from /home/rails/.rvm/gems/ruby-1.9.3-p392/bin/ruby_noexec_wrapper:14:in `<main>'
    /home/rails/.rvm/gems/ruby-1.9.3-p392/gems/unicorn-4.6.2/lib/unicorn/http_server.rb:193:in `pid=': Already running on PID:1226 (or pid=/home/rails/hellworld/tmp/pids/unicorn.pid is stale) (ArgumentError)
            from /home/rails/.rvm/gems/ruby-1.9.3-p392/gems/unicorn-4.6.2/lib/unicorn/http_server.rb:137:in `start'
            from /home/rails/.rvm/gems/ruby-1.9.3-p392/gems/unicorn-4.6.2/bin/unicorn_rails:209:in `<top (required)>'
            from /home/rails/.rvm/gems/ruby-1.9.3-p392/bin/unicorn_rails:19:in `load'
            from /home/rails/.rvm/gems/ruby-1.9.3-p392/bin/unicorn_rails:19:in `<main>'
            from /home/rails/.rvm/gems/ruby-1.9.3-p392/bin/ruby_noexec_wrapper:14:in `eval'
            from /home/rails/.rvm/gems/ruby-1.9.3-p392/bin/ruby_noexec_wrapper:14:in `<main>'
    

    反复 sudo reboot,然后执行 RAILS_ENV=production bundle exec unicorn_rails -c config/unicorn.rb -E production -D 网页还是 502

  • #9 楼 @blacktulip sudo service unicorn status unicorn: unrecognized service "unicorn: unrecognized service" google 没找到有答案 stackoverflow 也没找到,555

  • sudo service unicorn status unicorn: unrecognized service 确实没启动

    但是 unicorn -v 有结果,说明有安装 cat log/unicorn.log 没输出结果

    vim Gemfile gem 'unicorn' 有,有去注解

    vim /home/rails/hellworld/config/unicorn.rb 看顶楼

    nginx.conf

    upstream unicorn_server { server unix:/tmp/unicorn.ruby-china.sock fail_timeout=0; }

    proxy_pass http://unicorn_server;

  • curl -I ruby1.com:5000 curl: (7) couldn't connect to host

    curl baidu.com

    curl 127.0.0.1:5000 curl: (7) couldn't connect to host

    curl 127.0.0.1

    502 Bad Gateway

    502 Bad Gateway


    nginx/1.1.19

    ================================== ps aux|grep unicorn_rails rails 1096 0.0 0.0 9384 872 pts/1 S+ 06:41 0:00 grep unicorn_rails

    ================================== cat unicorn.log 是空的 T_T

  • nginx 那边设置监听是 80 端口,所以 www.ruby1.com:5000 tail -f /var/log/nginx/error.log 这里没有任何新的输出信息

  • #1 楼 @yangkit 不正常 是不是 unicorn 没启动起来

    unicorn -v unicorn v4.6.2