FreeBSD 8.1下用nginx配置簡單web負載均衡
原創【51CTO獨家特稿】Nginx (“engine x”) 相信大家已經很熟悉了,是俄羅斯人Igor Sysoev(塞索耶夫)編寫的一款高性能的 HTTP 和反向代理服務器。Nginx 已經在俄羅斯最大的門戶網站── Rambler Media(www.rambler.ru)上運行了3年時間,同時俄羅斯超過20%的虛擬主機平臺采用Nginx作為反向代理服務器。
51CTO推薦專題:企業內網開發環境部署與管理全攻略(FreeBSD+PHP)
在國內,已經有 新浪博客、新浪播客、網易新聞、六間房、56.com、Discuz!、水木社區、豆瓣、YUPOO、海內、迅雷在線 等多家網站使用 Nginx 作為Web服務器或反向代理服務器。
我觀察了國內清一色的CDN網站,基本都是用了nginx作為它們的web服務器。nginx的優點如下:
1、高并發連接:官方測試能夠支撐5萬并發連接,在實際生產環境中跑到2~3萬并發連接數。
2、內存消耗少:在3萬并發連接下,開啟的10個Nginx進程才消耗150M內存(15M*10=150M)。
3、配置文件非常簡單:風格跟程序一樣通俗易懂。
4、成本低廉:Nginx為開源軟件,可以免費使用。而購買F5 BIG-IP、NetScaler等硬件負載均衡交換機則需要十多萬至幾十萬人民幣。
5、支持Rewrite重寫規則:能夠根據域名、URL的不同,將HTTP請求分到不同的后端服務器群組。
6、內置的健康檢查功能:如果 Nginx Proxy后端的某臺Web服務器宕機了,不會影響前端訪問。
7、節省帶寬:支持GZIP壓縮,可以添加瀏覽器本地緩存的Header頭。
8、穩定性高:用于反向代理,宕機的概率微乎其微。
9、對網絡的依賴性非常之低,理論上ping得通就能正常使用。
本文將介紹如何利用nginx配置簡單的web負載均衡。文中所用的平臺是64bit的FreeBSD 8.1。
部分內容參考了張宴的相關文章。
作者簡介:余洪春(博客),網名撫琴煮酒,英文名Andrew.Yu,武漢某外企高級Linux/Unix系統管理員、項目實施工程師,紅帽RHCE講師,擅長負載均衡高可用和中小型證券類和商務網站架構,目前關注網站架構研究及網絡安全。
安裝
cd /usr/ports/www/nginx make install clean
配置nginx.conf文件
#user nobody; worker_processes 4; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include 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 logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; upstream webserver { server 192.168.21.45 weight=1; server 192.168.4.45 weight=1; } server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://webserver; 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 4 32k; 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/local/www/nginx-dist; } # 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$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443; # server_name localhost; # ssl on; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_timeout 5m; # ssl_protocols SSLv2 SSLv3 TLSv1; # ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
重點語法講解
worker_processes 4; #這個與服務器的核數等同即可 upstream #這個里面的weight值越大,服務器所要承擔的壓力也越大;如果采用ip_hash模塊時,就不能分配weight了,不然Nginx啟動時會報錯 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #以上配置是讓后端的web服務器可以通過X-Forwarded-For獲取客戶端的真實IP #允許客戶端請求的最大的單個文件字節數 client_max_body_size 10m; #緩沖區代理緩沖用戶端請求的最大字節數 可以理解為先保存到本地再傳給用戶 client_body_buffer_size 128k; #跟后端服務器連接的超時時間_發起握手等候響應超時時間 proxy_connect_timeout 90; #連接成功后_等候后端服務器響應時間_其實已經進入后端的排隊之中等候處理 proxy_read_timeout 90; #后端服務器數據回傳時間_就是在規定時間之內后端服務器必須傳完所有的數據 proxy_send_timeout 90; #代理請求緩存區_這個緩存區間會保存用戶的頭信息以供Nginx進行規則處理,一般只要能保存下頭信息即可 proxy_buffer_size 8k; #同上 告訴Nginx保存單個用的幾個Buffer 最大用多大空間 proxy_buffers 4 32k; #如果系統很忙的時候可以申請更大的proxy_buffers 官方推薦*2 proxy_busy_buffers_size 64k; #proxy緩存臨時文件的大小 proxy_temp_file_write_size 64k;
Nginx如何解決session的問題
Nginx負載均衡器采用ip_hash來代替默認的rr方式,即可以將某客戶端IP的請求通過哈希算法定位到同一臺后端web服務器上,這樣避免了session丟失,解決了session問題。
但ip_hash指令無法保證后端服務器的負載均衡,可能有些后端服務器接收的請求多,有些后端服務器接收的請求少;這樣失去了負載均衡的意義。
我們的解決方案是將用戶的登錄session信息寫進后端的Mysql數據庫,這個在后面的PHPCMS系統中也實現了,效果也不錯;后來我提出了折衷方案,如果Nginx并發連接數(即Nginx負載均衡器的NginxStatus的active connections)>2000,即采用將session寫進MySQL數據庫的方法;如果并發數小的話,ip_hash效果也是相當好的。
【51CTO.com獨家特稿,轉載請注明原文作者和出處。】
【編輯推薦】