[英]How to configure Nginx with Passenger to process 500 requests/sec
我正在托管ruby應用程序,但它每秒處理400個以上的請求時出現錯誤(某種錯誤500)。 請求數量較少(低於400個請求)的測試(loader.io)完成,結果良好。 我認為在每秒處理500個請求或更多請求時,我可以得到更好的結果。
該應用程序正在使用t2.2xlarge ec2實例(具有32Gb內存,8個虛擬內核)。 我想它可能會提供更好的性能。 該機器在帶有Passenger的Ubuntu 14.04,Rails 4.0.12,Nginx上運行。
我試圖對Nginx配置進行一些更改,但是沒有任何大的進步。 我當前的配置:
passenger_max_pool_size 60;
#passenger_pool_idle_time 20;
server {
listen 80;
return 301 https://mydomain.eu$request_uri;
}
server {
listen 443;
server_name ~^(\w+)\.mydomain.eu$;
return 301 https://mydomain.eu$request_uri;
}
server {
listen 443 ssl spdy default;
server_name mydomain.eu;
passenger_enabled on;
#passenger_max_pool_size 12;
passenger_max_request_queue_size 2000;
gzip on;
root /home/ubuntu/application/cversion/public;
ssl on;
ssl_certificate /home/ubuntu/fvhsdvhfd35/ssl-bundle1.crt;
ssl_certificate_key /home/ubuntu/fvhsdvhfd35/prvt.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 SSLv3;
ssl_ciphers "EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA384 EECDH+ECDSA+SHA256 EECDH+aRSA+SHA384 EECDH+aRSA+SHA256 EECDH+aRSA+RC4 EECDH EDH+aRSA RC4 !aNULL !eNULL !LOW !3DES !MD5 !EXP !PSK !SRP !DSS";
ssl_prefer_server_ciphers on;
location = /favicon.png {
expires max;
add_header Cache-Control public;
}
location = /ZeroClipboard.swf {
expires max;
add_header Cache-Control public;
}
location ~ ^/(assets)/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
# disable gzip on all omniauth paths to prevent BREACH
location ~ ^/auth/ {
gzip off;
passenger_enabled on;
}
您是否知道如何每秒獲取400個以上的請求?
這是Nginx與Passenger的日志,同時處理500個請求( passenger_max_pool_size為30;和passenger_max_request_queue_size 1200; )
2017/07/06 01:58:32 [error] 11749#11749: *56391 connect() to unix:/tmp/passenger.e1PiPXp/agents.s/core failed (11: Resource temporarily unavailable) while connecting to upstream, client: 54.89.44.6, server: mydomain.us, request: "GET / HTTP/1.1", upstream: "passenger:unix:/tmp/passenger.e1PiPXp/agents.s/core:", host: "mydomain.us"
2017/07/06 01:58:32 [error] 11740#11740: *64104 connect() to unix:/tmp/passenger.e1PiPXp/agents.s/core failed (11: Resource temporarily unavailable) while connecting to upstream, client: 52.87.219.148, server: mydomain.us, request: "GET / HTTP/1.1", upstream: "passenger:unix:/tmp/passenger.e1PiPXp/agents.s/core:", host: "mydomain.us"
2017/07/06 01:58:32 [error] 11749#11749: *64251 connect() to unix:/tmp/passenger.e1PiPXp/agents.s/core failed (11: Resource temporarily unavailable) while connecting to upstream, client: 52.87.219.148, server: mydomain.us, request: "GET / HTTP/1.1", upstream: "passenger:unix:/tmp/passenger.e1PiPXp/agents.s/core:", host: "mydomain.us"
2017/07/06 01:58:32 [error] 11749#11749: *63289 connect() to unix:/tmp/passenger.e1PiPXp/agents.s/core failed (11: Resource temporarily unavailable) while connecting to upstream, client: 54.89.44.6, server: mydomain.us, request: "GET / HTTP/1.1", upstream: "passenger:unix:/tmp/passenger.e1PiPXp/agents.s/core:", host: "mydomain.us"
2017/07/06 01:58:32 [error] 11748#11748: *67786 connect() to unix:/tmp/passenger.e1PiPXp/agents.s/core failed (11: Resource temporarily unavailable) while connecting to upstream, client: 52.86.198.91, server: mydomain.us, request: "GET / HTTP/1.1", upstream: "passenger:unix:/tmp/passenger.e1PiPXp/agents.s/core:", host: "mydomain.us"
2017/07/06 01:58:32 [error] 11748#11748: *35057 connect() to unix:/tmp/passenger.e1PiPXp/agents.s/core failed (11: Resource temporarily unavailable) while connecting to upstream, client: 52.86.198.91, server: mydomain.us, request: "GET / HTTP/1.1", upstream: "passenger:unix:/tmp/passenger.e1PiPXp/agents.s/core:", host: "mydomain.us"
2017/07/06 01:58:32 [error] 11748#11748: *35166 connect() to unix:/tmp/passenger.e1PiPXp/agents.s/core failed (11: Resource temporarily unavailable) while connecting to upstream, client: 52.86.198.91, server: mydomain.us, request: "GET / HTTP/1.1", upstream: "passenger:unix:/tmp/passenger.e1PiPXp/agents.s/core:", host: "mydomain.us"
2017/07/06 01:58:32 [error] 11744#11744: *43208 connect() to unix:/tmp/passenger.e1PiPXp/agents.s/core failed (11: Resource temporarily unavailable) while connecting to upstream, client: 52.86.198.91, server: mydomain.us, request: "GET / HTTP/1.1", upstream: "passenger:unix:/tmp/passenger.e1PiPXp/agents.s/core:", host: "mydomain.us"
2017/07/06 01:58:32 [error] 11744#11744: *69130 connect() to unix:/tmp/passenger.e1PiPXp/agents.s/core failed (11: Resource temporarily unavailable) while connecting to upstream, client: 54.162.105.71, server: mydomain.us, request: "GET / HTTP/1.1", upstream: "passenger:unix:/tmp/passenger.e1PiPXp/agents.s/core:", host: "mydomain.us"
[ 2017-07-06 01:58:34.3865 11703/7fc703fff700 Ser/AcceptLoadBalancer.h:150 ]: Resuming accepting new clients
我有解決辦法。 Nginx Configuration的這些更改使我每秒獲得1000個請求。
首先,我提出:
"65536" in /proc/sys/net/core/somaxconn
"65536" in /proc/sys/net/ipv4/tcp_max_syn_backlog
/etc/nginx/conf.d/m.conf:
passenger_max_pool_size 90;
passenger_socket_backlog 16384;
#in server block
#was listen 443 ssl spdy default;
listen 443 ssl spdy default backlog=16384;
passenger_max_request_queue_size 2300;
ssl_session_cache shared:SSL:10m;
/etc/nginx/nginx.conf:
worker_rlimit_nofile 131072;
#in events block:
use epoll;
worker_connections 8192;
測試1分鍾內,每秒1000個請求的平均響應時間約為6秒。 有什么想法可以改善這種數量的請求的平均響應時間嗎?
根據此博客,我更改了Nginx配置以啟用Nginx Microcashing,但是我沒有更好的性能。 500請求/秒給了我5.1秒的平均響應時間。 大約900請求/秒-5.5秒。 但是,沒有緩存,我有500個請求2.5秒和900個請求5.6秒。
/etc/nginx/nginx.conf:
...
http {
...
proxy_cache_path /tmp/cache keys_zone=one:10m levels=1:2 inactive=600s max_size=100m;
...
}
/etc/nginx/conf.d/m.conf:
}
passenger_max_pool_size 90;
#passenger_pool_idle_time 20;
passenger_socket_backlog 16384;
server {
listen 80;
return 301 https://mydomain.eu$request_uri;
}
server {
listen 443;
server_name ~^(\w+)\.mydomain.eu$;
return 301 https://mydomain.eu$request_uri;
}
server {
listen 443 ssl spdy default backlog=16384;
server_name mydomain.eu;
ssl on;
ssl_certificate /home/ubuntu/fvhsdvhfd35/ssl-bundle1.crt;
ssl_certificate_key /home/ubuntu/fvhsdvhfd35/prvt.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 SSLv3;
ssl_ciphers "EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA384 EECDH+ECDSA+SHA256 EECDH+aRSA+SHA384 EECDH+aRSA+SHA256 EECDH+aRSA+RC4 EECDH EDH+aRSA RC4 !aNULL !eNULL !LOW !3DES !MD5 !EXP !PSK !SRP !DSS";
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
location / {
proxy_http_version 1.1; # Always upgrade to HTTP/1.1
proxy_set_header Connection ""; # Enable keepalives
proxy_set_header Accept-Encoding ""; # Optimize encoding
proxy_pass http://127.0.0.1:81/;
proxy_cache one;
proxy_cache_lock on;
proxy_cache_valid 200 1s;
proxy_cache_use_stale updating;
}
}
server {
listen 81;
server_name mydomain.eu;
passenger_enabled on;
passenger_max_request_queue_size 2300;
gzip on;
root /home/ubuntu/application/cversion/public;
location = /favicon.png {
expires max;
add_header Cache-Control public;
}
location = /ZeroClipboard.swf {
expires max;
add_header Cache-Control public;
}
location ~ ^/(assets)/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
# disable gzip on all omniauth paths to prevent BREACH
location ~ ^/auth/ {
gzip off;
passenger_enabled on;
}
}
為了進行這些優化,請確保您引用了nginx博客鏈接,並確實考慮了每個請求的響應時間(使用Rails技術盡可能地減少響應時間)。 還應考慮數據庫優化,即同時使用正確的索引和最大數據庫連接數。 由於這是一個多層次的問題,因此必須在每個層次上進行配置以獲得最佳性能。 祝好運 :)
可以根據官方的性能調整建議來審查Nginx配置: https : //www.nginx.com/blog/tuning-nginx/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.