第一部分 WEB层均衡负载
.net平台下,我目前部署过的均衡负载有两种方式(iis7和Nginx),以下以Nginx为例讲解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.