Nginx

如何在我的網路伺服器上實現每秒 500K 的請求?

  • December 20, 2013

我最近送給自己一個新的專用伺服器,我正試圖從中獲得最大的性能,以供娛樂和學習。

我正在嘗試實現此伺服器每秒可以處理的最大可能請求,並以 500K 請求/秒為目標,如此處所述 - http://lowlatencyweb.wordpress.com/2012/03/20/500000-requestssec-modern-http-servers -很快/

伺服器詳情

英特爾® 至強® E3-1270 4 核 (8 HT) x 3.4 GHz

記憶體 24 GB DDR3 ECC

硬碟空間 2,000 GB (2 x 2,000 SATA) RAID 軟體 RAID 1

區域網路 100​​mbps

作業系統 Centos 6.3 64 位

Nginx

對於靜態 txt 文件,我只能達到 35K 請求/秒。我在同一台機器上執行基準測試。我知道 NIC 限制和網路成本

ab -n100000 -c200 http://localhost/test.txt

更新- 165K 請求/秒

我嘗試了另一個名為的基準測試工具wrk,它給了我 165K 請求/秒。非常酷!

更新 2 - 250K 請求/秒

nginx.conf

#######################################################################
#
# This is the main Nginx configuration file.
#
# More information about the configuration options is available on
#   * the English wiki - http://wiki.nginx.org/Main
#   * the Russian documentation - http://sysoev.ru/nginx/
#
#######################################################################

#----------------------------------------------------------------------
# Main Module - directives that cover basic functionality
#
#   http://wiki.nginx.org/NginxHttpMainModule
#
#----------------------------------------------------------------------

user              nginx;
worker_processes  8;
worker_rlimit_nofile 262144;

error_log  /var/log/nginx/error.log;
#error_log  /var/log/nginx/error.log  notice;
#error_log  /var/log/nginx/error.log  info;

pid        /var/run/nginx.pid;


#----------------------------------------------------------------------
# Events Module
#
#   http://wiki.nginx.org/NginxHttpEventsModule
#
#----------------------------------------------------------------------

events {
   worker_connections  16384;
   multi_accept on;
   use epoll;
}


#----------------------------------------------------------------------
# HTTP Core Module
#
#   http://wiki.nginx.org/NginxHttpCoreModule
#
#----------------------------------------------------------------------

http {
   include       /etc/nginx/mime.types;
   index    index.php index.html index.htm;

   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;

   sendfile        on;
   tcp_nopush     on;
   tcp_nodelay on;
   server_tokens off;
   client_max_body_size 24m;
   client_body_buffer_size 128k;
   #keepalive_timeout  0;
   keepalive_timeout  65;
   open_file_cache max=1000;
   open_file_cache_min_uses 10;
   open_file_cache_errors   on;

   gzip on;
       gzip_static on;
   gzip_comp_level 3;
   gzip_disable "MSIE [1-6]\.";
       gzip_http_version 1.1;
       gzip_vary on;
       gzip_proxied any;
       gzip_types text/plain text/css text/xml text/javascript text/x-component text/cache-manifest application/json application/javascript application/x-javascript application/xml application/rss+xml application/xml+rss application/xhtml+xml application/atom+xml application/wlwmanifest+xml application/x-font-ttf image/svg+xml image/x-icon font/opentype app/vnd.ms-fontobject;
       gzip_min_length  1000;

fastcgi_cache_path   /tmp  levels=1:2
                      keys_zone=NAME:10m
                      inactive=5m;

 fastcgi_cache_key "$scheme$request_method$host$request_uri";


server {
   listen       80;
   server_name  _;
       root /var/www/html;

   #charset koi8-r;

   #access_log  logs/host.access.log  main;

   location / {
       try_files $uri $uri/ /index.php?$args;
   }

   error_page  404              /404.html;
   location = /404.html {
       root   /var/www/error;
   }

   # redirect server error pages to the static page /50x.html
   #
   error_page   500 502 503 504  /50x.html;
   location = /50x.html {
       root   /var/www/error;
   }

   # proxy the PHP scripts to Apache listening on 127.0.0.1:80
   #
   #location ~ \.php$ {
   #    proxy_pass   http://127.0.0.1;
   #}

   # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
   #
   location ~ \.php$ {
   fastcgi_pass   127.0.0.1:9000;
   fastcgi_index  index.php;
   fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
   include        fastcgi_params;
   # checks to see if the visitor is logged in, a commenter,
   # or some other user who should bypass cache
   set $nocache "";
   if ($http_cookie ~ (comment_author_.*|wordpress_logged_in.*|wp-postpass_.*)) {
    set $nocache "Y";
  }
   # bypass cache if logged in.
   # Be sure that this is above all other fastcgi_cache directives
   fastcgi_no_cache $nocache;
   fastcgi_cache_bypass $nocache;

 fastcgi_cache   NAME;
 fastcgi_cache_valid   200 302  10m;
 fastcgi_cache_valid   301      1h;
 fastcgi_cache_valid   any      1m;
 fastcgi_cache_min_uses  10;
 fastcgi_cache_use_stale error  timeout invalid_header http_500;
   fastcgi_buffers 256 16k;
   }

location = /favicon.ico {
       log_not_found off;
       access_log off;
}

location = /robots.txt {
       allow all;
       log_not_found off;
       access_log off;
}

# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
location ~ /\. {
       deny all;
       access_log off;
       log_not_found off;
}

# Deny access to any files with a .php extension in the uploads directory
location ~* ^/wp-content/uploads/.*.php$ {
       deny all;
       access_log off;
       log_not_found off;
}

location ~* \.(jpg|jpeg|gif|png|flv|mp3|mpg|mpeg|js|css|ico)$ {
       expires                 max;
       log_not_found   off;
}
}

}

首先,您應該編寫一個新的基準測試工具。您實際上ab不是在進行基準測試nginx

Arpit,如果您想像絕對最小的 Web 響應,即使它是一個靜態文本文件,也是一個乙太網數據包(約 1,500 字節),那麼其中 500,000 個數據包的大小約為 750,000,000 字節,或大約 7.5 吉比特。因此,除非您的伺服器非常容易解除安裝 10Gb 網卡(它沒有,您擁有的網卡慢了一百倍)並設置了驅動程序和核心以允許您幾乎完全淹沒其中一個連結,加上負載平衡器、防火牆、路由器和向前連接的延遲以這種速度,那麼你將永遠無法達到那種性能——即使是單個數據包響應,這不太可能。所以最終 35k 聽起來離你的極限不遠。

引用自:https://serverfault.com/questions/408546