编辑:
我确实在最初的问题中看到,在 Linux 下运行没有问题,但是,在“高负载”场景(即 +50 个并发请求)下,我在 Linux 上也遇到了困难,因此这也可能适用于 OS X。 ..
我更深入地研究了这个问题,找到了我的设置的解决方案 - 在负载测试我的简单 hello world 应用程序时,我不再收到 502 Bad Gateway 错误。我在 Ubuntu 13.10 上测试了一切,并在 /opt/mono 中安装了新编译的 Mono 3.2.3。
当您使用“/verbose=True /printlog=True”启动 mono-fastcgi-4 服务器时,您将注意到以下输出:
Root directory: /some/path/you/defined
Parsed unix:/tmp/nginx-1.sockets as URI unix:/tmp/nginx-1.sockets
Listening on file /tmp/nginx-1.sockets with default permissions
Max connections: 1024
Max requests: 1024
重要的几行是“最大连接数”和“最大请求数”。这些基本上告诉了 mono-fastcgi 服务器将能够处理多少个活动 TCP 连接和请求 - 在本例中为 1024。
我的 NGINX 配置如下:
worker_processes 4;
events {
worker_connections 1024;
}
所以我有4个工人,每个可以有 1024 个连接。因此,NGINX 愉快地接受 4096 个并发连接,然后将其发送到 mono-fastcgi(它只希望处理 1024 个 conns)。因此,mono-fastcgi 正在“自我保护”并停止服务请求。对此有两种解决方案:
- 降低 NGINX 可以接受的请求量
- 增加你的 fastcgi 上游池
通过将 NGINX 配置更改为如下所示,可以轻松解决问题 1:
worker_processes 4; # <-- or 1 here
events {
worker_connections 256; # <--- if 1 above, then 1024 here
}
但是,这很可能意味着您无法充分利用计算机上的资源。
2. 的解决方案有点棘手。首先,mono-fastcgi 必须启动多次。为此,我创建了以下脚本(在应该启动的网站内):
function startFastcgi {
/opt/mono/bin/fastcgi-mono-server4 /loglevels=debug /printlog=true /multiplex=false /applications=/:`pwd` /socket=$1 &
}
startFastcgi 'unix:/tmp/nginx-0.sockets'
startFastcgi 'unix:/tmp/nginx-1.sockets'
startFastcgi 'unix:/tmp/nginx-2.sockets'
startFastcgi 'unix:/tmp/nginx-3.sockets'
chmod 777 /tmp/nginx-*
它启动 4 个 mono-fastcgi 工作线程,每个工作线程可以接受 1024 个连接。那么 NGINX 应该配置如下:
upstream servercom {
server unix:/tmp/nginx-0.sockets;
server unix:/tmp/nginx-1.sockets;
server unix:/tmp/nginx-2.sockets;
server unix:/tmp/nginx-3.sockets;
}
server {
listen 80;
location / {
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_pass servercom;
include fastcgi_params;
}
}
这为 NGINX 配置了一个由 4 个“上游工作人员”组成的池,它将在循环赛时尚 http://nginx.org/en/docs/http/ngx_http_upstream_module.html。现在,当我用以下命令敲击我的服务器时Boom https://github.com/tarekziade/boom并发 200 持续 1 分钟,一切都很好(又名根本没有 502)。
我希望您能以某种方式将其应用到您的代码中并使其正常工作:)
P.S:
你可以下载我用来测试的Hello World ServiceStack代码here https://github.com/nover/service-stack-fastcgi-heroku.
你可以下载我的完整 NGINX.confighere http://ge.tt/5xxs8Ry/v/0%20here.
虽然有一些路径需要调整,但它应该作为一个良好的基础。