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