nginx +乘客 + Rails 3.1 = 502 网关不好?
我有最新的 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 中间件出现问题的想法都是不可能的。
I have the latest Nginx running with Passenger, SQLite and Rails 3.1. Somehow, when I have Passenger running for a while, I start getting "502 bad gateway" errors when visiting my website.
Here is a snippet from my Nginx error log:
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"
Here is my passenger-status --show=backtraces
output:
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)
This is what my passenger-memory-stats
shows:
---------- 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
It seems there is an issue with my the communication between Passenger and Nginx?
Also, looking at the Rails logs, it is clear that the request never reaches Rails at all, as there are no log entries for visits that get the 502 error. So my initial thought of something being wrong with any Rack middleware should not be possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试设置
passenger_spawn_method保守
- 显然Passenger默认分叉设置和Rails 3.1存在问题Try setting
passenger_spawn_method conservative
-- apparently there are issues with Passenger default forking settings and Rails 3.1我刚刚遇到nginx报告的致命的“502 Bad Gateway错误”,web堆栈是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 错误就消失了......I just meet such deadly "502 Bad Gateway error" reported by nginx, web stack is Ubuntu 12.04 + Rails 3.2.9 + Passenger 3.0.18 + nginx 1.2.4, it spent me 2 hours to found the root cause:
My rails application no need database support, so I just remove the
gem 'sqlite3'
in the Gemfile, it works fine in development mode, but will lead502 Bad Gateway
in production mode.So after add back
gem 'sqlite3'
in Gemfile, such 502 Bad Gateway error disappear....我遇到了同样的问题,就我而言,它有助于增加 Nginx 配置文件中的
passenger_max_pool_size
设置。也许您还可以查看以下帖子,它们也帮助我找到了此解决方案:
I had the same problem and in my case it helped to increase the
passenger_max_pool_size
setting in the Nginx configuration file.Maybe you can also take a look on the following postings which also helped me finding this solution:
Rails 4 中的情况对我来说是一样的,但是我在 /confirg/secrets.yml 中添加了一个“SECRETKEYBASE”
It was the same for me in Rails 4, but I have added a "SECRETKEYBASE" in /confirg/secrets.yml
VM 中的“V”代表虚拟。另请参阅其他 SO 问题的答案,例如 Linux 下 Java 的虚拟内存使用情况,使用了太多内存。
顶部的 147 MB 并没有暗示任何异常情况。从 Passenger 的角度来看,您的 502 错误意味着工作进程存在其他问题。你应该检查你的 Rails & Nginx 日志文件中寻找线索,也许还有passenger-status --show=backtraces。
The "V" in VM is for Virtual. See also answers on other SO questions, e.g. Virtual Memory Usage from Java under Linux, too much memory used.
That top 147 MB does not hint of anything unusual whatsoever. Your 502 errors mean something else is wrong with the worker processes from Passenger's point of view. You should check your Rails & Nginx log files for clues, and perhaps
passenger-status --show=backtraces
.