大型架構.net平臺篇(WEB層均衡負載nginx)

第一部分 WEB層均衡負載
.net平臺下,我目前部署過的均衡負載有兩種方式(iis7和Nginx),如下以Nginx爲例講解web層的均衡負載. php

簡介:Nginx 超越 Apache 的高性能和穩定性,使得國內使用 Nginx 做爲 Web 服務器的網站也愈來愈多,其中包括新浪博客、新浪播客、網易新聞等門戶網站頻道,六間房、56.com等,視頻分享網站,Discuz!官方論壇、水木社 區等知名論壇,豆瓣、YUPOO相冊、海內SNS、迅雷在線等新興Web 2.0網站。 html

聽說Nginx能承受3萬併發鏈接數,這一點沒有測試,總之Nginx是以高併發著名的。 前端

Nginx 作前端的均衡負載也是至關不錯的選擇,並且和具體的語言無關,下面是Nginx 分發到IIS的方式 nginx

簡單流程:用戶訪問網站(服務器C)->服務器C(不須要IIS) Nginx分發請求到->A或B或都更多的服務器(具體的IIS服務器), 實現前端負載 web

配置很是簡單,方法以下: apache

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) windows

確保A服務器和B服務器的頁面是徹底同樣的,以及web.config須要配置machineKey一致,否則會報異常的。 後端

<system.web>
    <machineKey validation="3DES" 服務器

validationKey="319B474B1D2B7A87C996B280450BB36506A95AEDF9B51211" session

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.

相關文章
相關標籤/搜索