怎樣建設(shè)網(wǎng)站是什么意思全網(wǎng)推廣費用
tomcat重要目錄
bin | 存放啟動和關(guān)閉Tomcat腳本 |
conf | 存放Tomcat不同的配置文件 |
doc | 存放Tomcat文檔 |
lib | 存放Tomcat運行需要的庫文件 |
logs | 存放Tomcat執(zhí)行時的log文件 |
src | 存放Tomcat的源代碼 |
webapps | Tomcat的主要Web發(fā)布目錄 |
work | 存放jsp編譯后產(chǎn)生的class文件 |
nginx負載均衡原理
nginx實現(xiàn)負載均衡是通過反向代理實現(xiàn)
反向代理原理
?nginx配置反向代理的主要參數(shù)
- upstream 服務(wù)池名{}? #配置后端服務(wù)池,以提供響應(yīng)數(shù)據(jù)
- proxy_pass http://服務(wù)池名? #配置將訪問請求轉(zhuǎn)發(fā)給后端服務(wù)池的服務(wù)器處理
Nginx+Tomcat負載均衡、動靜分離?
nginx七層服務(wù)器部署
Nginx 服務(wù)器:192.168.47.10:80
Tomcat服務(wù)器1:192.168.80.20:80
Tomcat服務(wù)器2:192.168.47.50:8080 ?192.168.47.50:8081
1.部署Nginx 負載均衡器
systemctl stop firewalld
setenforce 0yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++ makeuseradd -M -s /sbin/nologin nginxcd /opt/nginx
tar zxvf nginx-1.22.0.tar.gzcd nginx-1.22.0/
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-file-aio \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-http_flv_module \
--with-http_ssl_module \
--with-stream
###啟用 stream模塊,提供4層調(diào)度make -j4 && make installln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecrReload=/bin/kill -s HUP $MAINPID
ExecrStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.targetchmod 754 /lib/systemd/system/nginx.service
systemctl daemon-reload
systemctl start nginx.service
systemctl enable nginx.service
?2.部署2臺Tomcat 應(yīng)用服務(wù)器
systemctl stop firewalld
setenforce 0tar zxvf jdk-8u91-linux-x64.tar.gz -C /usr/local/vim /etc/profile
export JAVA_HOME=/usr/local/jdk1.8.0_91
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:${JRE_HOME}/bin:$PATHsource /etc/profiletar zxvf apache-tomcat-8.5.16.tar.gzmv /opt/apache-tomcat-8.5.16/ /usr/local/tomcat/usr/local/tomcat/bin/shutdown.sh
/usr/local/tomcat/bin/startup.shnetstat -ntap | grep 8080
?通過之前的多實例部署,直接使用tomcat服務(wù)器2
?3.動靜分離配置
(1)Tomcat1 server 配置
mkdir /usr/local/tomcat/webapps/test
vim /usr/local/tomcat/webapps/test/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test1 page</title> #指定為 test1 頁面
</head>
<body>
<% out.println("動態(tài)頁面 1,http://www.test1.com");%>
</body>
</html>vim /usr/local/tomcat/conf/server.xml
#由于主機名 name 配置都為 localhost,需要刪除前面的 HOST 配置
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"><Context docBase="/usr/local/tomcat/webapps/test" path="" reloadable="true"></Context>
</Host>/usr/local/tomcat/bin/shutdown.sh
/usr/local/tomcat/bin/startup.sh
?(2)Tomcat2 server 配置
mkdir /usr/local/tomcat/tomcat1/webapps/test /usr/local/tomcat/tomcat2/webapps/testvim /usr/local/tomcat/tomcat1/webapps/test/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test2 page</title> #指定為 test2 頁面
</head>
<body>
<% out.println("動態(tài)頁面 2");%>
</body>
</html>vim /usr/local/tomcat/tomcat1/conf/server.xml
#刪除前面的 HOST 配置
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"><Context docBase="/usr/local/tomcat/tomcat1/webapps/test" path="" reloadable="true" />
</Host>/usr/local/tomcat/tomcat1/bin/shutdown.sh
/usr/local/tomcat/tomcat1/bin/startup.sh vim /usr/local/tomcat/tomcat2/webapps/test/index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test3 page</title> #指定為 test3 頁面
</head>
<body>
<% out.println("動態(tài)頁面 3");%>
</body>
</html>vim /usr/local/tomcat/tomcat2/conf/server.xml
#刪除前面的 HOST 配置
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"><Context docBase="/usr/local/tomcat/tomcat2/webapps/test" path="" reloadable="true" />
</Host>/usr/local/tomcat/tomcat2/bin/shutdown.sh
/usr/local/tomcat/tomcat2/bin/startup.sh
?
?(3)Nginx server 配置
#準(zhǔn)備靜態(tài)頁面
echo '<html><body><h1>這是靜態(tài)頁面</h1></body></html>
#gzip on;
#配置負載均衡的服務(wù)器列表,weight參數(shù)表示權(quán)重,權(quán)重越高,被分配到的概率越大upstream tomcat_server {server 192.168.47.20:8080 weight=1;server 192.168.47.50:8080 weight=1;server 192.168.47.50:8081 weight=1;}server {listen 80;server_name localhost;charset koi8-r;#access_log logs/host.access.log main;location / {root html;index index.html index.htm;}location ~ .*\.jsp$ { #配置Nginx處理動態(tài)頁面請求,將 .jsp文件請求轉(zhuǎn)發(fā)到Tomcat 服務(wù)器處理proxy_pass http://tomcat_server;
#設(shè)置后端的Web服務(wù)器可以獲取遠程客戶端的真實IP
##設(shè)定后端的Web服務(wù)器接收到的請求訪問的主機名(域名或IP、端口),默認HOST的值為proxy_pass指令設(shè)置的主機名。如果反向代理服務(wù)器不重寫該請求頭的話,那么后端真實服務(wù)器在處理時會認為所有的請求都來自反向代理服務(wù)器,如果后端有防攻擊策略的話,那么機器就被封掉了。proxy_set_header HOST $host;proxy_set_header X-Real-IP $remote_addr;
##把$remote_addr賦值給X-Real-IP,來獲取源IPproxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
##在nginx 作為代理服務(wù)器時,設(shè)置的IP列表,會把經(jīng)過的機器ip,代理機器ip都記錄下來}
?
?
?
?
nginx四層反向代理 在七層基礎(chǔ)上添加
?配置四層反向代理nginx負載均衡服務(wù)器 192.168.47.30
systemctl stop firewalld
setenforce 0yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++ makeuseradd -M -s /sbin/nologin nginxcd /opt/nginx
tar zxvf nginx-1.22.0.tar.gzcd nginx-1.22.0/
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-file-aio \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-http_flv_module \
--with-http_ssl_module \
--with-stream
###啟用 stream模塊,提供4層調(diào)度make -j4 && make installln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecrReload=/bin/kill -s HUP $MAINPID
ExecrStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.targetchmod 754 /lib/systemd/system/nginx.service
systemctl daemon-reload
systemctl start nginx.service
systemctl enable nginx.service
配置七層反向代理nginx負載均衡服務(wù)器 192.168.47.10 192.168.47.100,以上同樣步驟
?部署tomcat應(yīng)用服務(wù)器 tomcat1 tomcat2 tomcat3,用上面七層反向代理的步驟?
??動靜分離配置
(1)nginx靜態(tài)設(shè)置
?
(2)tomcat動態(tài)設(shè)置
?
?
?四層反向代理nginx server設(shè)置
Nginx 四層代理配置:
./configure --with-stream# vim /usr/local/nginx/conf/nginx.conf和http同等級:所以一般只在http上面一段設(shè)置,stream { #啟用 stream模塊,提供4層調(diào)度upstream appserver { #配置負載均衡的七層nginx服務(wù)器列表地址池server 192.168.47.10:80 weight=1;server 192.168.47.100:80 weight=1;}server {listen 8080;proxy_pass appserver; #訪問本主機8080端口實際是轉(zhuǎn)發(fā)到地址池訪問}
}http {include mime.types;default_type application/octet-stream;
七層反向代理nginx server 設(shè)置
?
?
?
?
?
?Nginx負載均衡策略
介紹完Nginx負載均衡的相關(guān)指令后,我們已經(jīng)能實現(xiàn)將用戶的請求分發(fā)到不同的服務(wù)器上,那么除了采用默認的分配方式外,我們還能采用什么樣的負載算法?
Nginx的upstream支持如下六種方式的分配算法,分別是:
算法名稱 | 說明 |
---|---|
輪詢?? | rr? ?默認方式 |
weight | ?wrr 權(quán)重方式 |
ip_hash | 依據(jù)ip分配方式(根據(jù)客戶端IP做hash緩存的算法) |
least_conn | 依據(jù)最少/小連接方式 |
url_hash | 依據(jù)URL分配方式(根據(jù)客戶端訪問的url路徑做hash緩存的算法) |
fair | 依據(jù)響應(yīng)時間方式 |
補充:random? 隨機分配
? ? ? ? ? hash? ?$remote_addr? ?consistent? ? 一致性hash算法, 客戶端ip哈希算法,是ip_hash算法的加強版
? ? ? ? ? nginx全局變量
nginx反向代理實現(xiàn)會話保持
1)ip_hash url_hash 客戶端IP一致性哈希算法 hash $remote_addr consistent 基于客戶端IP/訪問的URL做哈希緩存實現(xiàn)會話保持2)sticky_cookie_insert 需要安裝第三方的sticky模塊,基于cookie來判斷實現(xiàn)會話保持3)配置后端應(yīng)用服務(wù)器共享 session 或使用后端服務(wù)器自身通過相關(guān)機制保持 session 同步實現(xiàn)會話保持