1. 程式人生 > >PCB 使用Nginx讓IIS7實現負載均衡

PCB 使用Nginx讓IIS7實現負載均衡

在PCB行業眾多系統中PCB工程系統是主要的資料生產者, 它與外部系統資料互動是最多的,經統計介面數超過100個之多;這麼多介面呼叫與管理起來是混亂的,所以今年年初對工程整合方式改造,將原來的點對點的方式整合更改為Web中心整合方式,並業務邏輯與資料層全部轉向Web中心端處理,下面看看工程整合線路改造前後整合線路圖.

   改造前---PCB工程系統整合線路圖

   改造後---PCB工程系統整合線路圖

     改造後優點,

     1.整合管理工程系統介面內部呼叫與外部呼叫
     2.統一工程系統客戶端呼叫引數傳遞
     3.統一業務邏輯與資料層
     4.統一Log 日誌記錄
     5.外部介面變更,可以實現客戶端不用更新達到介面更新

    但存在缺點,業務邏輯處理壓力全部壓在Web中心(其實也算不上缺點,基本都是採用分而治之的思想,1臺伺服器不行稿2臺,2臺不行稿3臺,)

 

但由於統一整合介面管理,邏輯業務處理全部轉向Web中心端處理,這樣一來導致壓力全部集中在一臺Web伺服器上, 目前此臺伺服器一直處於高位執行, 以當前的訂單量還能應付,但我們搞程式的人總要有居安思危,有防患於未然的意識, 不要等到哪天伺服器伺服器扛不住了,再想解決辦法就晚了是吧.這裡就介紹使用Nginx讓IIS7實現負載均衡,使用起來好簡單,網上也有很多例子,這裡用實際操作再記錄一遍。

 

一.下載Nginx

         官方下載  http://nginx.org/en/download.html

         目前1.14.0這個版本最穩定,在64位與32位都運行了沒問題.

        

二.解壓Nginx

          這是綠色軟體,不需安裝,解壓zip後,再配置一下就能用,但不能放在中文目錄下,不然執行不起來

             

 三.配置Nginx

         

 四.啟動Nginx,並驗證是否成功

       

 

五.Nginx配置負載均衡

    

       配置程式碼:

#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 myserver{
        server 172.18.226.184:8030 weight=2 down;
        server 172.18.226.184:8031 weight=1;
        server 172.18.226.184:8032 weight=2;
        ip_hash;
    }

    server {
        listen       8033;
        server_name  myserver;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass    http://myserver; 
        }

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

}
View Code

六.實測網頁

     http://pcbren.cn:8033/

    

      這樣就可以實現對外一個網址,但對內卻是多臺伺服器(這裡用埠號區分)

  

六.nginx批處理管理

       用於nginx管理 有nginx.bat管理更方便(來自http://leleroyn.cnblogs.com,感謝分享) 

         

cls 
@ECHO OFF 
SET NGINX_PATH=D: 
SET NGINX_DIR=D:\nginx-1.14.0\
color 0a 
TITLE Nginx 管理程式 Power By Ants (http://leleroyn.cnblogs.com)
GOTO MENU 
:MENU 
CLS 
ECHO. 
ECHO. * * * *  Nginx 管理程式 Power By Ants (http://leleroyn.cnblogs.com) * * *  
ECHO. * * 
ECHO. * 1 啟動Nginx * 
ECHO. * * 
ECHO. * 2 關閉Nginx * 
ECHO. * * 
ECHO. * 3 重啟Nginx * 
ECHO. * * 
ECHO. * 4 退 出 * 
ECHO. * * 
ECHO. * * * * * * * * * * * * * * * * * * * * * * * * 
ECHO. 
ECHO.請輸入選擇專案的序號: 
set /p ID= 
IF "%id%"=="1" GOTO cmd1 
IF "%id%"=="2" GOTO cmd2 
IF "%id%"=="3" GOTO cmd3 
IF "%id%"=="4" EXIT 
PAUSE 
:cmd1 
ECHO. 
ECHO.啟動Nginx...... 
IF NOT EXIST %NGINX_DIR%nginx.exe ECHO %NGINX_DIR%nginx.exe不存在 
%NGINX_PATH% 
cd %NGINX_DIR% 
IF EXIST %NGINX_DIR%nginx.exe start %NGINX_DIR%nginx.exe 
ECHO.OK 
PAUSE 
GOTO MENU 
:cmd2 
ECHO. 
ECHO.關閉Nginx...... 
taskkill /F /IM nginx.exe > nul 
ECHO.OK 
PAUSE 
GOTO MENU 
:cmd3 
ECHO. 
ECHO.關閉Nginx...... 
taskkill /F /IM nginx.exe > nul 
ECHO.OK 
GOTO cmd1 
GOTO MENU 
View Code

 

 七.更多Nginx配置請連結,這裡整理如下:

 

      Nginx負載均衡簡單配置

 

          4 種負載均衡演算法       

        nginx反向代理proxy_pass配置URI引起的思考