windows下通過nginx實現tomcat叢集負載均衡(入門)
一、目標
- Windows下,下載安裝nginx
- Nginx常用命令
- Nginx負載均衡兩個tomcat
- Nginx配置多個負載均衡服務
二、下載安裝nginx
下載地址http://nginx.org/en/download.html
版本nginx-1.14.1
無需安裝,解壓後直接使用即可。
啟動後,瀏覽器輸入localhost, 驗證nginx是否安裝啟動成功。
三、Nginx常用命令
1、啟動(命令列):start nginx
2、關閉(命令列):nginx –s stop
3、重新載入配置:nginx –s reload
4、測試配置是否正確:nginx –t
5、注意
有時執行了關閉命令,nginx程序任然存在,需要在工作管理員中將nginx程序殺掉。否則會導致nginx的最新配置無法生效。
四、Nginx負載均衡兩個tomcat
修改nginx-1.14.1\conf的nginx.conf
- 在#gzip on;下方新增:
upstream www.hcbtest.com{
server 127.0.0.1:8080 weight=2;
server 127.0.0.1:9090 weight=3;
}
- 修改location /,修改為:
location / {
root html;
index index.html index.htm;
proxy_pass http://www.hcbtest.com;
proxy_redirect default;
}
- 修改server埠:
listen 10080;
server_name localhost;
- Tomcat調整:
保證tomcat的自帶工程root是存在的;把兩個tomcat的index.jsp內容進行修改,修改成不同的頁面顯示效果,用以體現負載均衡效果。
- 瀏覽器輸入localhost:10080,多次重新整理瀏覽器,可見有時呈現的是tomcat1的index.jsp頁面,有時呈現的是tomcat2的index.jsp的頁面。
五、Nginx配置多個負載均衡服務
只需基於上述的配置,繼續新增如下配置即可:
1、
upstream www.hcbtest2.com{
server 127.0.0.1:8080 weight=3;
server 127.0.0.1:9090 weight=2;
}
2、 server {
listen 10081;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root root;
index index.html index.htm index.jsp;
proxy_pass http://www.hcbtest2.com/examples/;
proxy_redirect default;
}
3、保證tomcat自帶的examples工程是存在的;
4、把兩個tomcat的examples工程index.jsp內容進行修改,修改成不同的頁面顯示效果,用以體現負載均衡效果。
5、瀏覽器輸入localhost:10081,多次重新整理瀏覽器即可呈現不同的介面。
六、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 www.hcbtest.com{
server 127.0.0.1:8080 weight=2;#預設為權重,訪問7088和9080 埠的比例為1:1
server 127.0.0.1:9090 weight=3;
}
upstream www.hcbtest2.com{
server 127.0.0.1:8080 weight=3;#預設為權重,訪問7088和9080 埠的比例為1:1
server 127.0.0.1:9090 weight=2;
}
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://www.hcbtest.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;
#}
}
server {
listen 10081;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root root;
index index.html index.htm index.jsp;
proxy_pass http://www.hcbtest2.com/examples/;
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;
# }
#}
}