NGINX轻松管理10万长连接
生活随笔
收集整理的這篇文章主要介紹了
NGINX轻松管理10万长连接
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一 前言 當管理大量連接時,特別是只有少量活躍連接,NGINX有比較好的CPU和RAM利用率,如今是多終端保持在線的時代,更能讓NGINX發揮這個優點。本文做一個簡單測試,NGINX在一個普通PC虛擬機上維護100k的HTTP長連接,然后查看NGINX和系統的資源利用率。 二 測試環境 1.服務端 硬件:雙核2.3GHz,2GB內存 軟件:CentOS 6.5, kernel 2.6.32, ?gcc?4.4.7, nginx 1.4.7 IP:10.211.55.8 內核參數調整:
$ /sbin/sysctl -w net.netfilter.nf_conntrack_max=102400 # 提升系統整體連接數 $ /sbin/sysctl net.netfilter.nf_conntrack_max #驗證是否生效 NGINX從源碼編譯時帶--with-http_stub_status_module,只列出與默認設置不同的部分:
worker_rlimit_nofile 102400; events { worker_connections? 102400; } http { # 設一個比較大得超時,客戶端能以平緩的方式發送HEAD請求來維持KeepAlive keepalive_timeout? 3600; #監控連接數,本機訪問 location /nginx_status { stub_status on; access_log?? off; allow 127.0.0.1; deny all; } } 2. 客戶端1 硬件:雙核2.3GHz,2GB內存 軟件:CentOS 6.5, kernel 2.6.32, gcc?4.4.7, Python 3.3.5 IP:10.211.55.9 內核參數調整: $ /sbin/sysctl -w net.ipv4.ip_local_port_range="1024 61024” #實際只使用50000個端口 $ /sbin/sysctl net.ipv4.ip_local_port_range #驗證是否生效 $ vi /etc/security/limits.conf #提升當前用戶的最大打開文件數nofile(hard >= soft > 50000) $ ulimit -n #驗證是否生效,可能需重啟shell Python 3.3.5從源碼編譯,如下配置: $ pyvenv ~/pyvenv #創建虛擬環境,便于測試 $ . ~/pyvenv/bin/activate #激活虛擬環境 (pyvenv) $ python get-pip.py #從pip官網下載get-pip.py (pyvenv) $ pip install asyncio #安裝異步IO模塊 因為Apache ab只能批量請求,不能維持連接,所以自己寫了一個HTTP長連接測試工具asyncli.py,詳細實現見http://blog.chinaunix.net/uid-190176-id-4223282.html。 基本用法: (pyvenv) $ python?asyncli.py?--help usage:?asyncli.py?[-h] [-c CONNECTIONS] [-k KEEPALIVE] url asyncli positional arguments: url?????????????????? page address optional arguments: -h, --help??????????? show this help message and exit -c CONNECTIONS, --connections CONNECTIONS number of connections simultaneously -k KEEPALIVE, --keepalive KEEPALIVE HTTP keepalive timeout 工作機制: 每隔10毫秒連續創建10個連接(每秒約1000個連接),直到總連接數達到CONNECTIONS,每個連接都會睡眠[1, KEEPALIVE / 2]的一個隨機數(單位為秒),然后向服務端url發送一個HEAD請求來維持HTTP KeepAlive,然后重復上一個睡眠步驟。。。 3. 客戶端2 與客戶端1完全一致,除了IP為10.211.55.10 三?運行與輸出 1. 服務端系統空閑 # vmstat procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu----- r? b?? swpd?? free?? buff? cache?? si?? so??? bi??? bo?? in?? cs us sy id wa st 0? 0????? 0 1723336? 11624? 76124??? 0??? 0??? 62???? 1?? 26?? 28? 0? 0 100? 0? 0 2. 服務端啟動NGINX,無外部WEB請求 # nginx # vmstat procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu----- r? b?? swpd?? free?? buff? cache?? si?? so??? bi??? bo?? in?? cs us sy id wa st 0? 0????? 0 1681552? 11868? 76840??? 0??? 0??? 50???? 1?? 24?? 25? 0? 0 100? 0? 0? 3. 客戶端1和2先后啟動,每個客戶端發起50000個長連接,并維持直到服務端關閉或超時。 (pyvenv) $ python?asyncli.py?-c 50000 -k 3600?http://10.211.55.8/?& 4. 約2小時后。。。查看服務端 # curl?http://127.0.0.1/nginx_status Active connections: 100001 server accepts handled requests 165539 165539 1095055 Reading: 0 Writing: 1 Waiting: 100000 # ps -p 1899 -o pid,%cpu,%mem,rss,comm PID %CPU %MEM?? RSS COMMAND 1899? 2.0? 4.9 94600 nginx # vmstat 3 procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu----- r? b?? swpd?? free?? buff? cache?? si?? so??? bi??? bo?? in?? cs us sy id wa st 0? 0????? 0 654248? 62920 158924??? 0??? 0???? 6???? 6? 361? 108? 0? 1 98? 0? 0???? 0? 0????? 0 654232? 62920 158952??? 0??? 0???? 0??? 85? 804? 218? 0? 1 98? 0? 0???? 0? 0????? 0 654108? 62928 158976??? 0??? 0???? 0???? 9? 813? 214? 0? 1 98? 0? 0???? 0? 0????? 0 654108? 62928 159004??? 0??? 0???? 0???? 0? 803? 220? 0? 1 99? 0? 0???? ^C # free total?????? used?????? free???? shared??? buffers???? cached Mem:?????? 1918576??? 1264576???? 654000????????? 0????? 62952???? 159112 -/+ buffers/cache:??? 1042512???? 876064 Swap:????? 4128760????????? 0??? 4128760 四 總結 1. NGINX平均每個連接的內存占用很小,通過ps的rss看出,每個連接物理內存占用約1k。多數內存都被內核TCP緩存占用。 2. NGINX維持大量連接(少量活躍連接,本文中平均每秒活躍連接為總連接數的千分之一)占用很少CPU,上文僅為2%。 3. 最好的優化就是不優化。整個測試除了提升文件數和連接數的這些硬限制外,沒有任何參數調優,但仔細計算下就發現平均每個連接內存占用不到10k,遠小于默認的緩存大小(net.ipv4.tcp_rmem = 4096???? 87380???? 4194304)和 (net.ipv4.tcp_wmem = 4096???? 16384???? 4194304) 4. NGINX維持此類連接的主要瓶頸就是可用內存大小,我的2GB內存虛擬機其實可以支持15萬長連接,只不過我物理機器沒有內存再繼續clone虛擬機客戶端了:-( 5. 雖然會遇到更多內核參數的限制,但大內存服務器支持100萬連接是完全沒問題的。?
$ /sbin/sysctl -w net.netfilter.nf_conntrack_max=102400 # 提升系統整體連接數 $ /sbin/sysctl net.netfilter.nf_conntrack_max #驗證是否生效 NGINX從源碼編譯時帶--with-http_stub_status_module,只列出與默認設置不同的部分:
worker_rlimit_nofile 102400; events { worker_connections? 102400; } http { # 設一個比較大得超時,客戶端能以平緩的方式發送HEAD請求來維持KeepAlive keepalive_timeout? 3600; #監控連接數,本機訪問 location /nginx_status { stub_status on; access_log?? off; allow 127.0.0.1; deny all; } } 2. 客戶端1 硬件:雙核2.3GHz,2GB內存 軟件:CentOS 6.5, kernel 2.6.32, gcc?4.4.7, Python 3.3.5 IP:10.211.55.9 內核參數調整: $ /sbin/sysctl -w net.ipv4.ip_local_port_range="1024 61024” #實際只使用50000個端口 $ /sbin/sysctl net.ipv4.ip_local_port_range #驗證是否生效 $ vi /etc/security/limits.conf #提升當前用戶的最大打開文件數nofile(hard >= soft > 50000) $ ulimit -n #驗證是否生效,可能需重啟shell Python 3.3.5從源碼編譯,如下配置: $ pyvenv ~/pyvenv #創建虛擬環境,便于測試 $ . ~/pyvenv/bin/activate #激活虛擬環境 (pyvenv) $ python get-pip.py #從pip官網下載get-pip.py (pyvenv) $ pip install asyncio #安裝異步IO模塊 因為Apache ab只能批量請求,不能維持連接,所以自己寫了一個HTTP長連接測試工具asyncli.py,詳細實現見http://blog.chinaunix.net/uid-190176-id-4223282.html。 基本用法: (pyvenv) $ python?asyncli.py?--help usage:?asyncli.py?[-h] [-c CONNECTIONS] [-k KEEPALIVE] url asyncli positional arguments: url?????????????????? page address optional arguments: -h, --help??????????? show this help message and exit -c CONNECTIONS, --connections CONNECTIONS number of connections simultaneously -k KEEPALIVE, --keepalive KEEPALIVE HTTP keepalive timeout 工作機制: 每隔10毫秒連續創建10個連接(每秒約1000個連接),直到總連接數達到CONNECTIONS,每個連接都會睡眠[1, KEEPALIVE / 2]的一個隨機數(單位為秒),然后向服務端url發送一個HEAD請求來維持HTTP KeepAlive,然后重復上一個睡眠步驟。。。 3. 客戶端2 與客戶端1完全一致,除了IP為10.211.55.10 三?運行與輸出 1. 服務端系統空閑 # vmstat procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu----- r? b?? swpd?? free?? buff? cache?? si?? so??? bi??? bo?? in?? cs us sy id wa st 0? 0????? 0 1723336? 11624? 76124??? 0??? 0??? 62???? 1?? 26?? 28? 0? 0 100? 0? 0 2. 服務端啟動NGINX,無外部WEB請求 # nginx # vmstat procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu----- r? b?? swpd?? free?? buff? cache?? si?? so??? bi??? bo?? in?? cs us sy id wa st 0? 0????? 0 1681552? 11868? 76840??? 0??? 0??? 50???? 1?? 24?? 25? 0? 0 100? 0? 0? 3. 客戶端1和2先后啟動,每個客戶端發起50000個長連接,并維持直到服務端關閉或超時。 (pyvenv) $ python?asyncli.py?-c 50000 -k 3600?http://10.211.55.8/?& 4. 約2小時后。。。查看服務端 # curl?http://127.0.0.1/nginx_status Active connections: 100001 server accepts handled requests 165539 165539 1095055 Reading: 0 Writing: 1 Waiting: 100000 # ps -p 1899 -o pid,%cpu,%mem,rss,comm PID %CPU %MEM?? RSS COMMAND 1899? 2.0? 4.9 94600 nginx # vmstat 3 procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu----- r? b?? swpd?? free?? buff? cache?? si?? so??? bi??? bo?? in?? cs us sy id wa st 0? 0????? 0 654248? 62920 158924??? 0??? 0???? 6???? 6? 361? 108? 0? 1 98? 0? 0???? 0? 0????? 0 654232? 62920 158952??? 0??? 0???? 0??? 85? 804? 218? 0? 1 98? 0? 0???? 0? 0????? 0 654108? 62928 158976??? 0??? 0???? 0???? 9? 813? 214? 0? 1 98? 0? 0???? 0? 0????? 0 654108? 62928 159004??? 0??? 0???? 0???? 0? 803? 220? 0? 1 99? 0? 0???? ^C # free total?????? used?????? free???? shared??? buffers???? cached Mem:?????? 1918576??? 1264576???? 654000????????? 0????? 62952???? 159112 -/+ buffers/cache:??? 1042512???? 876064 Swap:????? 4128760????????? 0??? 4128760 四 總結 1. NGINX平均每個連接的內存占用很小,通過ps的rss看出,每個連接物理內存占用約1k。多數內存都被內核TCP緩存占用。 2. NGINX維持大量連接(少量活躍連接,本文中平均每秒活躍連接為總連接數的千分之一)占用很少CPU,上文僅為2%。 3. 最好的優化就是不優化。整個測試除了提升文件數和連接數的這些硬限制外,沒有任何參數調優,但仔細計算下就發現平均每個連接內存占用不到10k,遠小于默認的緩存大小(net.ipv4.tcp_rmem = 4096???? 87380???? 4194304)和 (net.ipv4.tcp_wmem = 4096???? 16384???? 4194304) 4. NGINX維持此類連接的主要瓶頸就是可用內存大小,我的2GB內存虛擬機其實可以支持15萬長連接,只不過我物理機器沒有內存再繼續clone虛擬機客戶端了:-( 5. 雖然會遇到更多內核參數的限制,但大內存服務器支持100萬連接是完全沒問題的。?
總結
以上是生活随笔為你收集整理的NGINX轻松管理10万长连接的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CodeForces 486C Pali
- 下一篇: heartbeat 日志分析