我使用 Nginx + Unicorn 部署了一个 Rails 应用程序。昨晚,当流量达到峰值时,Nginx 收到大量 404 错误。当我深入研究日志时,nginx 端的那些 404 请求甚至没有到达 Rails,因为我在 Rails 日志中找不到这些请求。这里的问题是什么?Rails 应用程序超载了吗?
服务器是双核4GB内存的虚拟主机,Ubuntu 11.10 + nginx 1.0.5,高峰期CPU基本爆满,目前我用的是4个unicorn worker进程。
谢谢!
nginx.conf
user deployer staff;
worker_processes 4;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
multi_accept on;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay off;
keepalive_timeout 65;
types_hash_max_size 2048;
underscores_in_headers on;
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;
error_log /var/log/nginx/error.log warn;
gzip on;
gzip_disable "msie6";
gzip_proxied any;
gzip_min_length 500;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
上游上游应用程序{服务器unix:/tmp/app_production.socket fail_timeout = 0;}
server {
client_max_body_size 10m;
listen 80;
server_name xxx.xxx.xxx.xxx;
#charset koi8-r;
root /home/deployer/apps/app_production/current/public;
try_files $uri /system/maintenance.html @app;
#access_log /var/log/nginx/log/host.access.log main;
location @app {
proxy_pass http://app;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# static resource
location ~ ^/(assets|images|javascripts|stylesheeets|system)/ {
gzip_static on;
expires max;
add_header Cache-Control public;
add_header Last-Modified "";
add_header ETag "";
open_file_cache max=100 inactive=500s;
open_file_cache_valid 600s;
open_file_cache_errors on;
break;
}
}
nginx 日志中大多数 404 错误如下所示
2013/02/21 06:15:40 [error] 1953#0: *7081 connect() to unix:/tmp/app_production.socket failed (11: Resource temporarily unavailable) while connecting to upstream, client: xxx.xxx.xxx.xxx, server: xxx.xxx.xxx.xxx, request: "GET /api/modules HTTP/1.1", upstream: "http://unix:/tmp/app_production.socket:/api/modules", host: "myapp.com"
答案1
您可能确实达到了打开套接字的最大数量,请尝试增加 nginx 中的 worker_rlimit_nofile 和 OS 中的 fs.file-max。