nginx 負載均衡

  (1)準備三臺虛擬機,IP地址分別爲172.31.108.114,172.31.108.115,172.31.108.116。114服務器安裝ngnix服務器,115,116安裝tomcat服務器。html

  (2)nginx配置文件主要配置信息以下:nginx

#user  nobody;

# 容許生成的進程數,默認爲1
worker_processes  1;

#全局錯誤日誌
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#ngnix運行id
#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  65;
    #gzip  on;
    #負載均衡配置
    upstream mystr{  
        server 172.31.108.115:8090;  
        server 172.31.108.200:8090;  
    } 
    server {
        listen       80; #監聽端口
        server_name  localhost; #監聽地址
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
    #請求的url地址過濾
        location / {
            root   html;
            index  index.html index.htm;
        }
        #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;
        }
    #請求的url地址匹配正則表達式
    location ~ \.jsp$ {
       proxy_pass http://mystr;
    }
    }

}

  (3)主要修改ngnix配置信息以下便可實現負載均衡。web

 upstream mystr{  
        server 172.31.108.115:8090;  
        server 172.31.108.116:8090;  
    } 

location ~ \.jsp$ {
       proxy_pass http://mystr;
    }

  (4)分爲修改一下115,116 tomcat服務器下默認首頁index.jap頁面,目錄 /webapp/ROOT/index.jsp,訪問兩次http://172.31.108.114/index.jsp進行測試,顯示以下:正則表達式

  

  

相關文章
相關標籤/搜索