第一部分 WEB層均衡負(fù)載 .net平臺下,我目前部署過的均衡負(fù)載有兩種方式(iis7和Nginx),以下以Nginx為例講解web層的均衡負(fù)載. 簡介:Nginx 超越 Apache 的高性能和穩(wěn)定性,使得國內(nèi)使用 Nginx 作為 Web 服務(wù)器的網(wǎng)站也越來越多,其中包括新浪博客、新浪播客、網(wǎng)易新聞等門戶網(wǎng)站頻道,六間房、56.com等,視頻分享網(wǎng)站,Discuz!官方論壇、水木社區(qū)等知名論壇,豆瓣、YUPOO相冊、海內(nèi)SNS、迅雷在線等新興Web 2.0網(wǎng)站。
據(jù)說Nginx能承受3萬并發(fā)連接數(shù),這一點(diǎn)沒有測試,總之Nginx是以高并發(fā)著名的。 Nginx 做前端的均衡負(fù)載也是相當(dāng)不錯的選擇,而且和具體的語言無關(guān),下面是Nginx 分發(fā)到IIS的方式 簡單流程:用戶訪問網(wǎng)站(服務(wù)器C)->服務(wù)器C(不需要IIS) Nginx分發(fā)請求到->A或B或都更多的服務(wù)器(具體的IIS服務(wù)器), 實(shí)現(xiàn)前端負(fù)載 配置非常簡單,方法如下: 1.下載Nginx windows版本,網(wǎng)上搜一下就行了.下載后解壓放在C服務(wù)器(192.168.0.3)C:或D:目錄下,例如(c:/nginx) 2. 把a(bǔ)sp.net站點(diǎn)復(fù)制到A服務(wù)器(192.168.0.1),B服務(wù)器(192.168.0.2),并建立好相應(yīng)的iis, 端口自已定, 例如(81) 確保A服務(wù)器和B服務(wù)器的頁面是完全一樣的,以及web.config需要配置machineKey一致,不然會報異常的。 <system.web> <machineKey validation="3DES" validationKey="319B474B1D2B7A87C996B280450BB36506A95AEDF9B51211" decryptionKey="280450BB36319B474C996B506A95AEDF9B51211B1D2B7A87" decryption="3DES"/> 3. 配置C服務(wù)器(前端負(fù)載轉(zhuǎn)發(fā)服務(wù)器)nginx的配置文件 nginx.conf 以下標(biāo)紅的就是需要配置的.其中ip_hash很重要(可以保證每個訪客可以固定一個后端,保證session不會出問題) #user nobody; worker_processes 1;
#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 mytest. { ip_hash; server 192.168.0.1:81; server 192.168.0.2:81; } server { listen 80; server_name mytest.; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://mytest.; proxy_redirect default; } #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 html; } # 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; # } #} } 5. 配置完成后, 通過命令行進(jìn)入ngnix目錄,運(yùn)行ngnix.exe,即啟動ngnix。(請確保沒有其它iis或apache占用80端口) 6.關(guān)閉ngnix命令為:ngnix -s stop.
|