首頁  >  文章  >  運維  >  nginx+tomcat怎麼實現Windows系統下的負載平衡

nginx+tomcat怎麼實現Windows系統下的負載平衡

WBOY
WBOY轉載
2023-05-16 13:28:061043瀏覽

nginx+tomcat怎麼實現Windows系統下的負載平衡

首先,安裝兩個tomcat,可以是同一個複製成兩個,也可以下載兩個不同版本的tomcat,我就是下載了兩個不同的版本。

(這是8.0版本的,隨便找兩個不是特別老的版本的就行)。

然後啟動兩個tomcat,在啟動前,先更改其中一個的端口號,使得兩個tomcat啟動時不會端口衝突,一個是本身的8080端口,一個是改成了9080端口。配好以後,打開cmd命令窗口,我的tomcat一個放在d:\software\apache-tomcat-8.5.24目錄下,按照如下命令即可啟動,啟動成功會彈出另一個窗口,顯示如下:

nginx+tomcat怎麼實現Windows系統下的負載平衡nginx+tomcat怎麼實現Windows系統下的負載平衡

開啟瀏覽器,輸入http://localhost:9080/,出現下列介面即tomcat啟動成功。另一個採用同樣的步驟即可。

nginx+tomcat怎麼實現Windows系統下的負載平衡nginx+tomcat怎麼實現Windows系統下的負載平衡

圖1:tomcat8     圖2:tomcat7

再之後,安裝一個nginx,我裝的是穩定版的nginx,下載位址:http://nginx.org/download/nginx-1.12.2.zip,解壓縮即可使用

nginx+tomcat怎麼實現Windows系統下的負載平衡

##在啟動前,必須要對nginx進行一下設定才可實現負載平衡的功能,打開conf資料夾,下面有一個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;

#以下四行是新新增的,兩個ip是兩個tomcat的存取位址, weight表示分給該伺服器的請求比重,兩個都是1,則按照1:1來分配,

upstream netitcast.com{
server 127.0.0.1:8080 weight=1;
server 127.0.0.1:9080 weight=2;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;

#一下兩行是進行修改的,http://netitcast.com和上面添加的要保持一致

location / {
proxy_pass http://netitcast.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 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;
# }
#}
}

還是打開cmd窗口,進入到以上目錄,執行命令:start nginx 即啟動成功,然後輸入網址:http://localhost/index.jsp,不斷的進行訪問,就會發現,上方顯示的圖1和圖2在交互作用的顯示。因為以上的配置weight是以1:2的比重來分配的,所以9080埠的比重就大一些,訪問到圖一(9080埠)的幾率就比較大,訪問到圖二(8080埠)的幾率比較小,這個機率一個是三分之二,一個是三分之一。

以上是nginx+tomcat怎麼實現Windows系統下的負載平衡的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除