nginx+tomcat實現負載均衡
文章目錄
一. 負載均衡的配置例項
1. 環境準備
主機名稱 | IP | 角色 |
---|---|---|
nginx | 192.168.110.10 | 反向代理、負載均衡伺服器 |
tomcat1 | 192.168.110.11 | tomcat叢集伺服器 |
tomcat2 | 192.168.110.12 | tomcat叢集伺服器 |
2. 安裝nginx服務
//建立系統使用者nginx
[root@localhost ~]# useradd -r -M -s /sbin/nologin nginx
//安裝依賴環境
[root@localhost ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++
安裝過程略....
[root@localhost ~]# yum -y groups mark install 'Development Tools'
Loaded plugins: product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
There is no installed groups file.
Maybe run: yum groups mark convert (see man yum)
Marked install: Development Tools
//建立日誌存放目錄
[root@localhost ~]# mkdir -p /var/log/nginx
[root@localhost ~]# chown -R nginx.nginx /var/log/nginx
//下載nginx
[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget http://nginx.org/download/nginx-1.12.0.tar.gz
--2018-08-20 11:19:09-- http://nginx.org/download/nginx-1.12.0.tar.gz
Resolving nginx.org (nginx.org)... 95.211.80.227, 206.251.255.63, 2606:7100:1:69::3f, ...
Connecting to nginx.org (nginx.org)|95.211.80.227|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 980831 (958K) [application/octet-stream]
Saving to: ‘nginx-1.12.0.tar.gz’
100%[======================================================>] 980,831 15.9KB/s in 43s
2018-08-20 11:19:52 (22.3 KB/s) - ‘nginx-1.12.0.tar.gz’ saved [980831/980831]
//編譯安裝
[root@localhost src]# ls
debug kernels nginx-1.12.0.tar.gz
[root@localhost src]# tar xf nginx-1.12.0.tar.gz
[root@localhost src]# cd nginx-1.12.0
[root@localhost nginx-1.12.0]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log
[root@localhost nginx-1.12.0]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
安裝過程略....
//配置環境變數
[root@localhost ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@localhost ~]# . /etc/profile.d/nginx.sh
//啟動nginx
[root@localhost ~]# nginx
[root@nginx ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
3. 安裝tomcat服務
tomcat1的配置:
3.1.1 java環境安裝
//關閉防火牆和selinux
[root@tomcat1 ~]# systemctl stop firewalld
[root@tomcat1 ~]# systemctl disable firewalld
[root@tomcat1 ~]# setenforce 0
//安裝jdk環境
[root@tomcat1 ~]# yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel
//檢視安裝的版本
[root@tomcat2 ~]# java -version
openjdk version "1.8.0_262"
OpenJDK Runtime Environment (build 1.8.0_262-b10)
OpenJDK 64-Bit Server VM (build 25.262-b10, mixed mode)
3.1.2 tomcat部署
//下載tomcat
[root@tomcat1 ~]# cd /usr/src/
[root@tomcat1 src]# wget http://mirror.bit.edu.cn/apache/tomcat/tomcat-9/v9.0.38/bin/apache-tomcat-9.0.38.tar.gz
//解壓部署
[root@tomcat1 src]# ls
apache-tomcat-9.0.38.tar.gz debug kernels
[root@tomcat1 src]# tar xf apache-tomcat-9.0.38.tar.gz -C /usr/local/
[root@tomcat1 src]# cd /usr/local/
[root@tomcat1 local]# ln -s apache-tomcat-9.0.38/ tomcat
[root@tomcat1 local]# ll
總用量 0
drwxr-xr-x. 9 root root 220 8月 30 09:45 apache-tomcat-9.0.38
drwxr-xr-x. 2 root root 6 3月 10 2016 bin
drwxr-xr-x. 2 root root 6 3月 10 2016 etc
drwxr-xr-x. 2 root root 6 3月 10 2016 games
drwxr-xr-x. 2 root root 6 3月 10 2016 include
drwxr-xr-x. 2 root root 6 3月 10 2016 lib
drwxr-xr-x. 2 root root 6 3月 10 2016 lib64
drwxr-xr-x. 2 root root 6 3月 10 2016 libexec
drwxr-xr-x. 2 root root 6 3月 10 2016 sbin
drwxr-xr-x. 5 root root 49 3月 25 02:56 share
drwxr-xr-x. 2 root root 6 3月 10 2016 src
lrwxrwxrwx. 1 root root 21 8月 30 09:45 tomcat -> apache-tomcat-9.0.38/
//寫一個hello world的java頁面
[root@tomcat1 ~]# vim index.html
[root@tomcat1 ~]# cat index.jsp
index.jsp
[root@tomcat1 test]# cat index.jsp
<html>
<head>
<title>test page</title>
</head>
<body>
<%
out.println("I am tomcat!");
%>
</body>
</html>
[root@tomcat1 ~]# mkdir /usr/local/tomcat/webapps/test
[root@tomcat1 ~]# cp index.jsp /usr/local/tomcat/webapps/test/
[root@tomcat1 ~]# ll /usr/local/tomcat/webapps/test/
總用量 4
-rw-r--r--. 1 root root 137 8月 30 09:50 index.jsp
//啟動tomcat
[root@tomcat1 ~]# /usr/local/tomcat/bin/catalina.sh start
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@tomcat1 ~]# ps -ef | grep tomcat
root 25206 1 0 09:24 pts/2 00:00:08 /usr/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -classpath /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap start
root 25269 9735 0 10:00 pts/2 00:00:00 grep --color=auto tomcat
[root@tomcat1 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 1 ::ffff:127.0.0.1:8015 :::*
LISTEN 0 100 :::8080 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
tomcat2的配置:
3.2.1 java環境安裝
//關閉防火牆和selinux
[root@tomcat2 ~]# systemctl stop firewalld
[root@tomcat2 ~]# systemctl disable firewalld
[root@tomcat2 ~]# setenforce 0
//安裝jdk環境
[root@tomcat2 ~]# yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel
//檢視安裝的版本
[root@tomcat2 ~]# java -version
openjdk version "1.8.0_262"
OpenJDK Runtime Environment (build 1.8.0_262-b10)
OpenJDK 64-Bit Server VM (build 25.262-b10, mixed mode)
3.2.2 tomcat部署
//下載tomcat
[root@tomcat2 ~]# cd /usr/src/
[root@tomcat2 src]# wget http://mirror.bit.edu.cn/apache/tomcat/tomcat-9/v9.0.38/bin/apache-tomcat-9.0.38.tar.gz
//解壓部署
[root@tomcat2 src]# ls
apache-tomcat-9.0.38.tar.gz debug kernels
[root@tomcat2 src]# tar xf apache-tomcat-9.0.38.tar.gz -C /usr/local/
[root@tomcat2 src]# cd /usr/local/
[root@tomcat2 local]# ln -s apache-tomcat-9.0.38/ tomcat
[root@tomcat2 local]# ll
總用量 0
drwxr-xr-x. 9 root root 220 8月 30 09:45 apache-tomcat-9.0.38
drwxr-xr-x. 2 root root 6 3月 10 2016 bin
drwxr-xr-x. 2 root root 6 3月 10 2016 etc
drwxr-xr-x. 2 root root 6 3月 10 2016 games
drwxr-xr-x. 2 root root 6 3月 10 2016 include
drwxr-xr-x. 2 root root 6 3月 10 2016 lib
drwxr-xr-x. 2 root root 6 3月 10 2016 lib64
drwxr-xr-x. 2 root root 6 3月 10 2016 libexec
drwxr-xr-x. 2 root root 6 3月 10 2016 sbin
drwxr-xr-x. 5 root root 49 3月 25 02:56 share
drwxr-xr-x. 2 root root 6 3月 10 2016 src
lrwxrwxrwx. 1 root root 21 8月 30 09:45 tomcat -> apache-tomcat-9.0.38/
//寫一個hello world的java頁面
[root@tomcat2 ~]# vim index.html
[root@tomcat2 ~]# cat index.jsp
<html>
<head>
<title>test page</title>
</head>
<body>
<%
out.println("Hello World");
%>
</body>
</html>
[root@tomcat2 ~]# mkdir /usr/local/tomcat/webapps/test
[root@tomcat2 ~]# cp index.jsp /usr/local/tomcat/webapps/test/
[root@tomcat2 ~]# ll /usr/local/tomcat/webapps/test/
總用量 4
-rw-r--r--. 1 root root 137 8月 30 09:50 index.jsp
//啟動tomcat
[root@tomcat2 ~]# /usr/local/tomcat/bin/catalina.sh start
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@tomcat2 ~]# ps -ef | grep tomcat
root 8433 1 0 16:34 pts/0 00:00:27 /usr/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -classpath /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap start
root 24240 24160 0 19:04 pts/1 00:00:00 grep --color=auto tomcat
[root@tomcat2 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 100 :::8080 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 1 ::ffff:127.0.0.1:8005 :::*
4. 修改tomcat1配置檔案
[root@tomcat1 ~]# vim /usr/local/tomcat/conf/server.xml
//修改下面兩條port
<Server port="8015" shutdown="SHUTDOWN">
<Connector port="8081" protocol="HTTP/1.1"
//重啟tomcat服務
[root@tomcat1 ~]# /usr/local/tomcat/bin/startup.sh
[root@tomcat1 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 1 ::ffff:127.0.0.1:8015 :::*
LISTEN 0 100 :::8081 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
5. 修改nginx配置檔案
//在/usr/local/nginx/conf/nginx.conf檔案中新增如下
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
#gzip on;
upstream tomcat-web {
server 192.168.110.11:8081 weight=1;
server 192.168.110.12:8080 weight=1;
}
server {
listen 80;
server_name 192.168.110.10;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://tomcat-web;
root html;
index index.html index.htm index.jsp;
}
//重啟nginx服務
[root@nginx ~]# nginx -s stop
[root@nginx ~]# nginx
[root@nginx ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
6. 測試結果
//在瀏覽器上訪問http://192.168.110.10/test/index.jsp
相關文章
- Nginx+Tomcat部署負載均衡NginxTomcat負載
- Nginx+Tomcat實現負載均衡、動靜分離叢集部署NginxTomcat負載
- Ribbon實現負載均衡負載
- GRPC 負載均衡實現RPC負載
- nginx實現負載均衡Nginx負載
- Linux環境搭建Nginx+Tomcat負載均衡叢集LinuxNginxTomcat負載
- HaProxy 實現 MySQL 負載均衡MySql負載
- 使用Redis儲存Nginx+Tomcat負載均衡叢集的SessionRedisNginxTomcat負載Session
- Nginx+Tomcat負載均衡,動靜分離群集部署解析NginxTomcat負載
- dubbo(三):負載均衡實現解析負載
- 使用YARP來實現負載均衡負載
- Python實現簡單負載均衡Python負載
- SpringCloud微服務中使用RestTemplate+Ribbon實現負載均衡(實現方法+實現原理+替換負載均衡策略)SpringGCCloud微服務REST負載
- 帶你瞭解Nginx+Tomcat負載均衡,動靜分離群集NginxTomcat負載
- Nginx實現簡單的負載均衡Nginx負載
- Haproxy搭建 Web 群集實現負載均衡Web負載
- 在Linux中,如何實現負載均衡?Linux負載
- Nginx如何實現四層負載均衡?Nginx負載
- Keepalived實現Nginx負載均衡高可用Nginx負載
- Docker Compose+nginx實現負載均衡DockerNginx負載
- orleans叢集及負載均衡實現負載
- jmeter壓力測試實現負載均衡JMeter負載
- 伺服器負載均衡原理及實現伺服器負載
- nginx實現兩臺服務負載均衡Nginx負載
- nginx讓多個tomcat實現負載均衡NginxTomcat負載
- RHEL 7配置HAProxy實現Web負載均衡Web負載
- SpringCloud Fegin結合Ribbon實現負載均衡SpringGCCloud負載
- LVS+Keepalived 實現高可用負載均衡負載
- Nginx如何實現負載均衡釋出策略?Nginx負載
- haporxy+keepalived實現負載均衡+高可用負載
- LVS負載均衡群集概念、NAT模式LVS負載均衡實戰部署負載模式
- gRPC負載均衡(自定義負載均衡策略)RPC負載
- gRPC負載均衡(客戶端負載均衡)RPC負載客戶端
- 超實用:實現負載均衡技術的方式負載
- 負載均衡負載
- 淺談負載均衡演算法與實現負載演算法
- Spring Cloud:使用 Feign 實現負載均衡詳解SpringCloud負載
- keepalived+haproxy實現mysql負載均衡高可用MySql負載