nginx + passenger + rails 3.1 = 502坏网关?

时间:2011-06-06 09:38:35

标签: ruby-on-rails ubuntu nginx passenger

我有最新的Nginx与Passenger,SQLite和Rails 3.1一起运行。不知何故,当我有Passenger运行一段时间后,我在访问我的网站时开始收到“502 bad gateway”错误。

以下是我的Nginx错误日志的摘录:

2011/06/27 08:55:33 [error] 20331#0: *11270 upstream prematurely closed connection while reading response header from upstream, client: xxx.xxx.xx.x, server: www.example.com, request: "GET / HTTP/1.1", upstream: "passenger:unix:/passenger_helper_server:", host: "example.com"
2011/06/27 08:55:47 [info] 20331#0: *11273 client closed prematurely connection, so upstream connection is closed too while sending request to upstream, client: xxx.xxx.xx.x, server: www.example.com, request: "GET / HTTP/1.1", upstream: "passenger:unix:/passenger_helper_server:", host: "example.com"

这是我的passenger-status --show=backtraces输出:

Thread 'Client thread 7':
 in 'Passenger::FileDescriptor Client::acceptConnection()' (HelperAgent.cpp:160)
 in 'void Client::threadMain()' (HelperAgent.cpp:603)

Thread 'Client thread 10':
 in 'Passenger::FileDescriptor Client::acceptConnection()' (HelperAgent.cpp:160)
 in 'void Client::threadMain()' (HelperAgent.cpp:603)

Thread 'Client thread 11':
 in 'Passenger::FileDescriptor Client::acceptConnection()' (HelperAgent.cpp:160)
 in 'void Client::threadMain()' (HelperAgent.cpp:603)

Thread 'Client thread 12':
 in 'Passenger::FileDescriptor Client::acceptConnection()' (HelperAgent.cpp:160)
 in 'void Client::threadMain()' (HelperAgent.cpp:603)

Thread 'Client thread 13':
 in 'Passenger::FileDescriptor Client::acceptConnection()' (HelperAgent.cpp:160)
 in 'void Client::threadMain()' (HelperAgent.cpp:603)

Thread 'Client thread 14':
 in 'Passenger::FileDescriptor Client::acceptConnection()' (HelperAgent.cpp:160)
 in 'void Client::threadMain()' (HelperAgent.cpp:603)

Thread 'Client thread 15':
 in 'Passenger::FileDescriptor Client::acceptConnection()' (HelperAgent.cpp:160)
 in 'void Client::threadMain()' (HelperAgent.cpp:603)

Thread 'Client thread 16':
 in 'Passenger::FileDescriptor Client::acceptConnection()' (HelperAgent.cpp:160)
 in 'void Client::threadMain()' (HelperAgent.cpp:603)

Thread 'Client thread 17':
 in 'Passenger::FileDescriptor Client::acceptConnection()' (HelperAgent.cpp:160)
 in 'void Client::threadMain()' (HelperAgent.cpp:603)

Thread 'Client thread 18':
 in 'Passenger::FileDescriptor Client::acceptConnection()' (HelperAgent.cpp:160)
 in 'void Client::threadMain()' (HelperAgent.cpp:603)

Thread 'Client thread 19':
 in 'Passenger::FileDescriptor Client::acceptConnection()' (HelperAgent.cpp:160)
 in 'void Client::threadMain()' (HelperAgent.cpp:603)

Thread 'Client thread 20':
 in 'Passenger::FileDescriptor Client::acceptConnection()' (HelperAgent.cpp:160)
 in 'void Client::threadMain()' (HelperAgent.cpp:603)

Thread 'Client thread 21':
 in 'Passenger::FileDescriptor Client::acceptConnection()' (HelperAgent.cpp:160)
 in 'void Client::threadMain()' (HelperAgent.cpp:603)

Thread 'Client thread 22':
 in 'Passenger::FileDescriptor Client::acceptConnection()' (HelperAgent.cpp:160)
 in 'void Client::threadMain()' (HelperAgent.cpp:603)

Thread 'Client thread 23':
 in 'Passenger::FileDescriptor Client::acceptConnection()' (HelperAgent.cpp:160)
 in 'void Client::threadMain()' (HelperAgent.cpp:603)

Thread 'Client thread 24':
 in 'Passenger::FileDescriptor Client::acceptConnection()' (HelperAgent.cpp:160)
 in 'void Client::threadMain()' (HelperAgent.cpp:603)

Thread 'MessageServer thread':
 in 'void Passenger::MessageServer::mainLoop()' (MessageServer.h:537)

Thread 'MessageServer client thread 35':
 in 'virtual bool Passenger::BacktracesServer::processMessage(Passenger::MessageServer::CommonClientContext&, boost::shared_ptr<Passenger::MessageServer::ClientContext>&, const std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&)' (BacktracesServer.h:47)
 in 'void Passenger::MessageServer::clientHandlingMainLoop(Passenger::FileDescriptor&)' (MessageServer.h:470)

这是我的passenger-memory-stats显示的内容:

---------- Nginx processes ----------
PID    PPID   VMSize   Private  Name
-------------------------------------
16291  1      35.4 MB  0.1 MB   nginx: master process /home/apps/.nginx/sbin/nginx
16292  16291  36.0 MB  0.8 MB   nginx: worker process
16293  16291  35.8 MB  0.5 MB   nginx: worker process
16294  16291  35.8 MB  0.5 MB   nginx: worker process
16295  16291  35.8 MB  0.5 MB   nginx: worker process
### Processes: 5
### Total private dirty RSS: 2.46 MB


----- Passenger processes ------
PID    VMSize    Private   Name
--------------------------------
16251  87.0 MB   0.3 MB    PassengerWatchdog
16254  100.4 MB  1.3 MB    PassengerHelperAgent
16256  41.6 MB   5.7 MB    Passenger spawn server
16259  134.8 MB  0.8 MB    PassengerLoggingAgent
18390  770.4 MB  17.1 MB   Passenger ApplicationSpawner: /home/apps/manager/current
18415  853.3 MB  147.7 MB  Rack: /home/apps/manager/current
18424  790.5 MB  57.2 MB   Rack: /home/apps/manager/current
18431  774.7 MB  18.7 MB   Rack: /home/apps/manager/current
### Processes: 8
### Total private dirty RSS: 248.85 MB

似乎我的Passenger和Nginx之间的沟通存在问题?

此外,查看Rails日志,很明显该请求根本不会到达Rails,因为没有用于获取502错误的访问的日志条目。所以我最初想到任何Rack中间件出错都应该是不可能的。

5 个答案:

答案 0 :(得分:2)

The "V" in VM is for Virtual。另见其他SO问题的答案,例如: Virtual Memory Usage from Java under Linux, too much memory used

前147 MB​​并没有暗示任何异常。从Passenger的角度来看,你的502错误意味着工人流程出现了其他问题。你应该检查你的Rails&amp; Nginx日志文件的线索,也许是passenger-status --show=backtraces

答案 1 :(得分:1)

尝试设置passenger_spawn_method conservative - 显然乘客默认分叉设置和Rails 3.1存在问题

答案 2 :(得分:1)

我刚刚遇到nginx报道的这种致命的“502 Bad Gateway错误”,网络堆栈是Ubuntu 12.04 + Rails 3.2.9 + Passenger 3.0.18 + nginx 1.2.4,它花了我2个小时才找到根本原因:

我的rails应用程序不需要数据库支持,所以我只删除了Gemfile中的gem 'sqlite3',它在开发模式下工作正常,但会在生产模式下导致502 Bad Gateway

因此在Gemfile中添加回gem 'sqlite3'之后,这样的502 Bad Gateway错误就会消失....

答案 3 :(得分:0)

我遇到了同样的问题,在我的例子中,它有助于增加Nginx配置文件中的passenger_max_pool_size设置。

也许您还可以查看以下帖子,这也有助于我找到这个解决方案:

答案 4 :(得分:0)

在Rails 4中对我来说是一样的,但我在/confirg/secrets.yml中添加了“SECRETKEYBASE”

production:
secretkeybase: # add yours here