配置nginx實現windows/iis應用負載平衡

來源:互聯網
上載者:User

標籤:權重   type   bsp   keep   off   error   核心   com   stc   

nginx是俄羅斯人開發的一款跨平台的高效能HTTP和反向 Proxy伺服器,可以利用它實現web應用伺服器的負載平衡。

反向 Proxy是指將使用者請求通過Proxy 伺服器轉寄給後端內部網路的應用伺服器,典型的應用比如配置nginx、lighttpd等反向 Proxy軟體實現負載平衡。與反向 Proxy相對應的叫正向 Proxy,典型的應用比如vpn。使用者直接存取google網站訪問不了,而Proxy 伺服器可以訪問google網站。這樣使用者就通過訪問Proxy 伺服器,從而間接的達到訪問google網站的目的。

負載平衡是指將使用者發起的大量web請求通過一定的演算法分攤到多個應用伺服器,從來達到降低伺服器負載壓力的目的。

nginx官方網站:http://nginx.org/,可以在網站上面進行下載。本篇部落格結合windows/iis+asp.net+nginx實現負載平衡,所以我們下載對應的windows版的nginx。下載之後,我們解壓看到nginx如下目錄:

我們開啟conf目錄下的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 cluster_server {        server  10.77.137.120:9000 weight=1;        server  10.77.137.11:8081 weight=2;    }        server {        listen       8083;        server_name  guwei4037-pc;        #charset utf-8;        #access_log  logs/host.access.log  main;        location / {            root   html;            index  index.aspx index.html index.htm;                        #指向叢集名稱為cluster_server            proxy_pass    http://cluster_server;                        #設定主機頭和用戶端真真實位址,以便伺服器擷取用戶端真實IP            proxy_set_header   Host              $host;            proxy_set_header   X-Real-IP         $remote_addr;            proxy_set_header   X-Forwarded-For   $proxy_add_x_forwarded_for;                        #禁用緩衝            proxy_buffering off;        }                location ~ \.(jpg|png|jpeg|bmp|gif|swf|css)$        {            expires 30d;            root /nginx-1.9.3/html;#root: #靜態檔案存在地址,這裡設定在/nginx-1.9.3/html下            break;        }        #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 ssl;    #    server_name  localhost;    #    ssl_certificate      cert.pem;    #    ssl_certificate_key  cert.key;    #    ssl_session_cache    shared:SSL:1m;    #    ssl_session_timeout  5m;    #    ssl_ciphers  HIGH:!aNULL:!MD5;    #    ssl_prefer_server_ciphers  on;    #    location / {    #        root   html;    #        index  index.html index.htm;    #    }    #}}

如上設定檔有幾點需要說明:

1、worker_processes 指定為4,表示有4個背景工作處理序,這個數值一般與伺服器的CPU核心數一致。通過start nginx啟動nginx服務後,可以通過工作管理員觀察進程可以看到nginx的進程數。

其中有1個是守護進程,4個是背景工作處理序。如果指定worker_processes為1,則會有兩個nginx.exe進程(1個守護進程,1個背景工作處理序)。

2、worker_connections指定的1024為預設的最大的串連數。可以指定更大的串連數,但要根據nginx伺服器的配置來定。

3、keepalive_timeout指定的65s為保持串連的逾時時間。

4、通過server-location-proxy_pass,與設定的cluster_server叢集綁定。

5、通過upstream節點配置了cluster_server。裡面的兩個server分別指向兩個不同的伺服器,通過weight(權重)設定訪問兩台伺服器的機率。

6、在server-location下的index,添加index.aspx,用於支援asp.net頁面的訪問。

 

然後我們開發一個aspx頁面,用於示範。

protected void Page_Load(object sender, EventArgs e){    this.Label0.Text = "請求開始時間:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");    this.Label1.Text = "伺服器名稱:" + base.Server.MachineName;    this.Label2.Text = "伺服器IP地址:" + base.Request.ServerVariables["LOCAL_ADDR"];    this.Label3.Text = "HTTP訪問連接埠:" + base.Request.ServerVariables["SERVER_PORT"];    this.Label4.Text = string.Concat(new object[] { ".NET解釋引擎版本:.NET CLR", Environment.Version.Major, ".", Environment.Version.Minor, ".", Environment.Version.Build, ".", Environment.Version.Revision });    this.Label5.Text = "伺服器作業系統版本:" + Environment.OSVersion.ToString();    this.Label6.Text = "伺服器IIS版本:" + base.Request.ServerVariables["SERVER_SOFTWARE"];    this.Label7.Text = "伺服器網域名稱:" + base.Request.ServerVariables["SERVER_NAME"];    this.Label8.Text = "虛擬目錄的絕對路徑:" + base.Request.ServerVariables["APPL_RHYSICAL_PATH"];    this.Label9.Text = "執行檔案的絕對路徑:" + base.Request.ServerVariables["PATH_TRANSLATED"];    this.Label10.Text = "虛擬目錄Session總數:" + this.Session.Contents.Count.ToString();    this.Label11.Text = "虛擬目錄Application總數:" + base.Application.Contents.Count.ToString();    this.Label12.Text = "網域名稱主機:" + base.Request.ServerVariables["HTTP_HOST"];    this.Label13.Text = "伺服器地區語言:" + base.Request.ServerVariables["HTTP_ACCEPT_LANGUAGE"];    this.Label14.Text = "使用者資訊:" + base.Request.ServerVariables["HTTP_USER_AGENT"];    this.Label14.Text = "CPU個數:" + Environment.GetEnvironmentVariable("NUMBER_OF_PROCESSORS");    this.Label15.Text = "CPU類型:" + Environment.GetEnvironmentVariable("PROCESSOR_IDENTIFIER");    this.Label16.Text = "請求來源地址:" + base.Request.Headers["X-Real-IP"];}

然後將它發布到多個伺服器的iis中,比如如上配置的10.77.137.120:9000和10.77.137.11:8081兩個地址。我們訪問我們監聽的8083連接埠,地址為:http://10.77.137.120:8083/,可以看到如下的頁面。

多重新整理幾次,可以看到新的地址資訊:

如上便實現了負載平衡。

 

配置nginx實現windows/iis應用負載平衡

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.