24*7*365 web 系統建設<一>——nginx+iis實現負載均衡

參考資料
1.Nginx - Windows下Nginx初入門
2.nginx for Windows
3.nginx+iis實現負載均衡
4.abp
請先閱讀參考1 2 3 ,在繼續閱讀以下內容

源代碼:https://git.oschina.net/zhaord/DispersedDemo.git


介紹

如果一個web程序,只部署在一臺機器上,那么如果這個機器出現宕機或者其他不可知事件,那么這個web程序,就over了,直到下次恢復……
那么,如何打造 247365 不間斷提供服務的web系統呢?采用的是iis集群,也就是說,部署多臺web服務,通過nginx來實現iis集群功能,當一臺服務不工作的時候,其他服務可以頂上,甚至,平攤web服務工作。


代碼準備工作

1.abp

通過 參4創建web程序,并根據文檔正確運行 程序。

2. 部署

在本地開啟iis服務,并且創建三個網站,每個網站分別綁定接口為10091/10092/10093,三個網站的物理路徑不一致,結果如下

iis站點.png
物理路徑.png
3. 發布代碼

右擊 vs,選擇發布,配置如下,這里需要掌握vs的發布方法

圖片.png
圖片.png

發布成功后,在解決方案下會多出一個文件,如下所示

圖片.png


復制 demo.pubxml文件并從命名 demo1.pubxml/demo2.pubxml,如下

圖片.png

修改 demo1 對應的物理路徑為文件夾2,修改 demo2對應的物理文件夾為3,修改后的內容如下

圖片.png
圖片.png

發布三個網站

4. 運行

打開瀏覽器,分別打開三個地址
http://127.0.0.1:10091/account/login
http://127.0.0.1:10092/account/login
http://127.0.0.1:10093/account/login

三個網站正常運行


圖片.png

nginx,參考2 參考3

1. nginx配置

修改 nginx的配置文件nginx.conf,配置如下


#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;
    
    # 服務器集群 sky_test
    upstream sky_test{
        server 127.0.0.1:10091 max_fails=3 fail_timeout=4s weight=9;
        server 127.0.0.1:10092 max_fails=3 fail_timeout=4s weight=9;
        server 127.0.0.1:10093 max_fails=3 fail_timeout=4s weight=9;
    }

    server {
        listen       10080;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
            
            # 添加集群
            proxy_pass  http://sky_test;
            # 設置主機頭和客戶端真實地址,以便服務器獲取到客戶端真實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;
        }

        #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;
    #    }
    #}

}

主要是一下兩部分

圖片.png
圖片.png

來實現集群

通過


圖片.png

實現監聽 10080 服務

2.運行 nginx

運行命令如下

圖片.png

發現任務管理器中右 nginx.exe 表示 運行成功

圖片.png
3. 運行

瀏覽器打開 http://127.0.0.1:10080/account/login

圖片.png

通過結果發現,可以正常訪問網站

4.進一步測試

現在,假設 10091的服務器出現了問題,我們手動停止站點

圖片.png

F5 刷新 http://127.0.0.1:10080/account/login
10080正常訪問

圖片.png

F5 刷新 http://127.0.0.1:10091/account/login

圖片.png

10091 不能訪問了


總結

通過以上,我們可以看到,即使其中一個網站掛掉了,但是我們仍然可以正常訪問我們的web程序。
但是,由于我們的web程序是部署三個地方,這就導致了其他問題,比如

  1. session 如何共享?
  2. log日志如何處理?現在的log日志,是分散在三個物理路徑的。
  3. 我們剛才是手動部署了三個站點,如何實現部署一個站點,其他站點內容同事更新部署?
  4. 上傳文件如何處理?因為上傳的文件,可能是分散在三個物理機器上。
    ....
    這些內容,將在后面繼續分享。

QQ:1260825783

我的公眾號

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容