加载nginx + Unicorn(Rails 3应用程序)时出现错误的网关错误
我有一个运行在云平台上的nginx和独角兽的Rails(3.2)应用程序。 “盒子”在Ubuntu 12.04上运行。加载nginx + Unicorn(Rails 3应用程序)时出现错误的网关错误
当系统负载是在约70%以上,nginx的突然(和表面上随机地)开始投掷502网关错误;当负载较少时,没有什么比这更好的了。我已经尝试过不同数量的内核(4,6,10 - 我可以在云平台上“更换硬件”),情况总是相同的。 (CPU负载与系统负载类似,用户占用率为55%,其余为系统和被盗,具有足够的可用内存,不需要交换)。
502通常分批但并不总是。
(我每运行一个核心麒麟工人,以及一个或两个nginx的工人。见CONFIGS的相关部分的10个内核上运行时以下。)
我真的不知道如何追踪原因这些错误。我怀疑这可能与独角兽工作人员无法服务(及时?)有关,但看起来很奇怪,因为他们似乎没有饱和CPU,我看不出为什么他们会等待IO(但我不知道不知道如何确定这一点)。
请问你能帮我找到原因吗?
麒麟配置(unicorn.rb
):
worker_processes 10
working_directory "/var/www/app/current"
listen "/var/www/app/current/tmp/sockets/unicorn.sock", :backlog => 64
listen 2007, :tcp_nopush => true
timeout 90
pid "/var/www/app/current/tmp/pids/unicorn.pid"
stderr_path "/var/www/app/shared/log/unicorn.stderr.log"
stdout_path "/var/www/app/shared/log/unicorn.stdout.log"
preload_app true
GC.respond_to?(:copy_on_write_friendly=) and
GC.copy_on_write_friendly = true
check_client_connection false
before_fork do |server, worker|
... I believe the stuff here is irrelevant ...
end
after_fork do |server, worker|
... I believe the stuff here is irrelevant ...
end
而且ngnix配置:
/etc/nginx/nginx.conf
:
worker_processes 2;
worker_rlimit_nofile 2048;
user www-data www-admin;
pid /var/run/nginx.pid;
error_log /var/log/nginx/nginx.error.log info;
events {
worker_connections 2048;
accept_mutex on; # "on" if nginx worker_processes > 1
use epoll;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
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;
# optimialization efforts
client_max_body_size 2m;
client_body_buffer_size 128k;
client_header_buffer_size 4k;
large_client_header_buffers 10 4k; # one for each core or one for each unicorn worker?
client_body_temp_path /tmp/nginx/client_body_temp;
include /etc/nginx/conf.d/*.conf;
}
/etc/nginx/conf.d/app.conf
:
sendfile on;
tcp_nopush on;
tcp_nodelay off;
gzip on;
gzip_http_version 1.0;
gzip_proxied any;
gzip_min_length 500;
gzip_disable "MSIE [1-6]\.";
gzip_types text/plain text/css text/javascript application/x-javascript;
upstream app_server {
# fail_timeout=0 means we always retry an upstream even if it failed
# to return a good HTTP response (in case the Unicorn master nukes a
# single worker for timing out).
server unix:/var/www/app/current/tmp/sockets/unicorn.sock fail_timeout=0;
}
server {
listen 80 default deferred;
server_name _;
client_max_body_size 1G;
keepalive_timeout 5;
root /var/www/app/current/public;
location ~ "^/assets/.*" {
...
}
# Prefer to serve static files directly from nginx to avoid unnecessary
# data copies from the application server.
try_files $uri/index.html $uri.html $uri @app;
location @app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 128k;
proxy_buffers 10 256k; # one per core or one per unicorn worker?
proxy_busy_buffers_size 256k;
proxy_temp_file_write_size 256k;
proxy_max_temp_file_size 512k;
proxy_temp_path /mnt/data/tmp/nginx/proxy_temp;
open_file_cache max=1000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
}
}
在google找到nginx错误日志中的表达式之后,它变成了一个已知问题,与nginx无关,与独角兽关系不大,并且植根于OS(linux)设置。
问题的核心在于套接字积压太短。有多种考虑因素应该是多少(无论您是想尽快检测集群成员故障还是让应用程序推动负载限制)。但无论如何,listen
:backlog
都需要调整。
我发现在我的情况下,listen ... :backlog => 2048
就足够了。 (我没有做太多的尝试,尽管如果你愿意,可以做一个很好的黑客工作,通过两个套接字在nginx和独角兽之间进行不同的备份和更长时间的通信;然后在nginx日志中看到更短的队列失败的频率)请注意,这不是科学计算和YMMV的结果。
但是,请注意,许多OS-es(包括大多数Linux发行版,包括Ubuntu 12.04)在套接字积压大小(低至128)上具有低得多的操作系统级默认限制。
您可以更改如下(为根)操作系统限制:
sysctl -w net.core.somaxconn=2048
sysctl -w net.core.netdev_max_backlog=2048
这些添加到/etc/sysctl.conf
以使更改永久。 (/etc/sysctl.conf
可以在不与sysctl -p
重新启动被重新加载。)
有提到,您可能需要增加可以通过以下方法也可以打开的文件(用于永久性ulimit -n
和/etc/security/limits.conf
)的最大数量。我已经做了其他的原因,所以我不知道它是否有所作为。
非常好!谢谢。 – 2013-05-04 06:23:22
谢谢fastcatch!应用此修复程序..还没有验证它是否改善了暂态错误网关错误。 – amolk 2013-12-18 05:49:26