企業(yè)手機(jī)網(wǎng)站建設(shè)策劃書(shū)百度搜索次數(shù)統(tǒng)計(jì)
一、Nginx反向代理(七層代理)
實(shí)驗(yàn)要求
使用Nginx實(shí)現(xiàn)Web反向代理功能,實(shí)現(xiàn)如下功能: 后端Web服務(wù)器兩臺(tái),可以使用httpd實(shí)現(xiàn)Nginx采用輪詢(xún)的方式調(diào)用后端Web服務(wù)器兩臺(tái)Web服務(wù)器的權(quán)重要求設(shè)置為不同的值最大失敗次數(shù)為2,失敗超時(shí)時(shí)間為30秒
實(shí)驗(yàn)環(huán)境
以下機(jī)器全部已經(jīng)存在,無(wú)須再次配置主機(jī)名 IP地址 角色
server1(已存在) eth0:192.168.99.254/24 客戶(hù)端
proxy(已存在) eth1:192.168.99.5/24 代理服務(wù)器
web1(已存在) eth1:192.168.99.100/24 web服務(wù)器
web2(已存在) eth1:192.168.99.200/24 web服務(wù)器
image-202410082130582811)部署后端Web服務(wù)器
1)部署后端Web1服務(wù)器
后端Web服務(wù)器可以簡(jiǎn)單使用yum方式安裝httpd實(shí)現(xiàn)Web服務(wù),為了可以看出后端服務(wù)器的不同,可以將兩臺(tái)后端服務(wù)器的首頁(yè)文檔內(nèi)容設(shè)置為不同的內(nèi)容
[root@web1 ~]# yum -y install httpd
[root@web1 ~]# echo "web1" > /var/www/html/index.html
[root@web1 ~]# systemctl enable --now httpd2)部署后端Web2服務(wù)器
[root@web2 ~]# yum -y install httpd
[root@web2 ~]# echo "web2" > /var/www/html/index.html
[root@web2 ~]# systemctl enable --now httpd3)使用proxy主機(jī)測(cè)試
[root@proxy ~]# curl 192.168.99.100
web1
[root@proxy ~]# curl 192.168.99.200
web2
2)配置Nginx代理服務(wù)器
添加服務(wù)器池,實(shí)現(xiàn)反向代理功能之前proxy主機(jī)安裝的nginx已經(jīng)改過(guò)很多配置,避免實(shí)驗(yàn)沖突,先還原proxy主機(jī)的nginx,重新安裝nginx
[root@proxy ~]# /usr/local/nginx/sbin/nginx -s stop #如果之前沒(méi)有啟動(dòng),可以不用執(zhí)行停止的命令
[root@proxy ~]# rm -rf /usr/local/nginx/
[root@proxy ~]# cd /root/lnmp_soft/
[root@proxy lnmp_soft]# rm -rf nginx-1.22.1
[root@proxy lnmp_soft]# tar -xf nginx-1.22.1.tar.gz
[root@proxy lnmp_soft]# cd nginx-1.22.1/
[root@proxy nginx-1.22.1]# yum -y install gcc make pcre-devel openssl-devel
[root@proxy nginx-1.22.1]# ./configure
[root@proxy nginx-1.22.1]# make && make install 1)修改nginx的配置文件
[root@proxy nginx-1.22.1]# vim /usr/local/nginx/conf/nginx.conf
...
http {
...
#使用upstream定義后端服務(wù)器集群,集群名稱(chēng)任意(如webserver)
#使用server定義集群中的具體服務(wù)器和端口upstream webserver {server 192.168.99.100:80;server 192.168.99.200:80;}server {listen 80;server_name localhost;#charset koi8-r;#access_log logs/host.access.log main;location / {root html;index index.html index.htm;proxy_pass http://webserver; #通過(guò)proxy_pass將用戶(hù)的請(qǐng)求轉(zhuǎn)發(fā)給webserver集群}...2)啟動(dòng)nginx
[root@proxy nginx-1.22.1]# /usr/local/nginx/sbin/nginx3)使用真機(jī)瀏覽器做測(cè)試192.168.99.5,刷新可以看到網(wǎng)站的輪詢(xún)效果,出現(xiàn)結(jié)果為 web1 或者 web2
3)配置upstream服務(wù)器集群池屬性
1)設(shè)置權(quán)重
weight可以設(shè)置后臺(tái)服務(wù)器的權(quán)重,權(quán)重越大任務(wù)的分配量就越大
[root@proxy nginx-1.22.1]# vim /usr/local/nginx/conf/nginx.conf
.. ..upstream webserver {server 192.168.99.100:80 weight=2;server 192.168.99.200:80;}server {.. ..2)重新加載配置并訪問(wèn),可以看到web1的任務(wù)量增加
[root@proxy nginx-1.22.1]# /usr/local/nginx/sbin/nginx -s reload真機(jī)瀏覽器測(cè)試http://192.168.99.5,不太明顯
可以使用命令行測(cè)試
[root@server1 ~]# curl 192.168.99.5
web1
[root@server1 ~]# curl 192.168.99.5
web2
[root@server1 ~]# curl 192.168.99.5
web1
[root@server1 ~]# curl 192.168.99.5
web13)設(shè)置健康檢查max_fails可以設(shè)置后臺(tái)服務(wù)器連不上的失敗次數(shù),fail_timeout可以設(shè)置后臺(tái)服務(wù)器的失敗超時(shí)時(shí)間,等待多長(zhǎng)時(shí)間再次嘗試連接
[root@proxy nginx-1.22.1]# vim /usr/local/nginx/conf/nginx.conf
...upstream webserver {server 192.168.99.100:80;server 192.168.99.200:80 max_fails=2 fail_timeout=30;}server {...4)重新加載配置并訪問(wèn)
[root@proxy nginx-1.22.1]# /usr/local/nginx/sbin/nginx -s reload測(cè)試時(shí),先將web2的httpd服務(wù)關(guān)閉
[root@web2 ~]# systemctl stop httpd使用真機(jī)命令行訪問(wèn)集群頁(yè)面curl 192.168.99.5,只會(huì)顯示web1的頁(yè)面
[root@server1 ~]# curl 192.168.99.5
web1
[root@server1 ~]# curl 192.168.99.5
web1此時(shí)即使將web2的httpd服務(wù)開(kāi)啟也無(wú)效,因?yàn)橐却?0秒
[root@web2 ~]# systemctl start httpd
[root@server1 ~]# curl 192.168.99.5 #30秒之后再訪問(wèn),web2會(huì)出現(xiàn)
web2
4)配置upstream服務(wù)器集群的調(diào)度算法
測(cè)試ip_hash
1)設(shè)置相同客戶(hù)端訪問(wèn)相同Web服務(wù)器
[root@proxy nginx-1.22.1]# vim /usr/local/nginx/conf/nginx.conf
...upstream webserver {ip_hash;server 192.168.99.100:80;server 192.168.99.200:80;}server {
...
2)重新加載配置
[root@proxy nginx-1.22.1]# /usr/local/nginx/sbin/nginx -s reload3)測(cè)試只會(huì)見(jiàn)到一個(gè)頁(yè)面
[root@server1 ~]# curl 192.168.99.5
web1
[root@server1 ~]# curl 192.168.99.5
web1
添加down標(biāo)記
down標(biāo)記可以讓集群主機(jī)暫時(shí)不參與集群活動(dòng)
[root@proxy nginx-1.22.1]# vim /usr/local/nginx/conf/nginx.conf
...upstream webserver {server 192.168.99.100:80;server 192.168.99.200:80 down;}server {...
重新加載配置
[root@proxy nginx-1.22.1]# /usr/local/nginx/sbin/nginx -s reload測(cè)試,只會(huì)見(jiàn)到web1
[root@server1 ~]# curl 192.168.99.5
web1
[root@server1 ~]# curl 192.168.99.5
web1二、Nginx的TCP/UDP調(diào)度器(四層代理)實(shí)驗(yàn)要求
使用Nginx實(shí)現(xiàn)TCP/UDP調(diào)度器功能,實(shí)現(xiàn)如下功能: 后端SSH服務(wù)器兩臺(tái)Nginx編譯安裝時(shí)需要使用--with-stream,開(kāi)啟ngx_stream_core_module模塊Nginx采用輪詢(xún)的方式調(diào)用后端SSH服務(wù)器
實(shí)驗(yàn)環(huán)境
以下機(jī)器全部已經(jīng)存在,無(wú)須再次配置主機(jī)名 IP地址 角色
server1(已存在) eth0:192.168.99.254/24 客戶(hù)端
proxy(已存在) eth1:192.168.99.5/24 代理服務(wù)器
web1(已存在) eth1:192.168.99.100/24 ssh服務(wù)器
web2(已存在) eth1:192.168.99.200/24 ssh服務(wù)器
image-202410082133113331)部署nginx服務(wù)
支持4層TCP/UDP代理的Nginx服務(wù)器 1)部署nginx服務(wù)器
編譯安裝必須要使用--with-stream參數(shù)開(kāi)啟4層代理模塊
[root@proxy nginx-1.22.1]# /usr/local/nginx/sbin/nginx -s stop
[root@proxy nginx-1.22.1]# rm -rf /usr/local/nginx/
[root@proxy nginx-1.22.1]# cd /root/lnmp_soft/nginx-1.22.1/
[root@proxy nginx-1.22.1]# yum -y install gcc make pcre-devel openssl-devel
[root@proxy nginx-1.22.1]# ./configure --with-stream #開(kāi)啟4層代理功能
[root@proxy nginx-1.22.1]# make && make install
[root@proxy nginx-1.22.1]# /usr/local/nginx/sbin/nginx -V #查看安裝模塊情況
nginx version: nginx/1.22.1
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-10) (GCC)
configure arguments: --with-stream
2)配置Nginx服務(wù)
添加服務(wù)器池,實(shí)現(xiàn)四層代理功能1)修改nginx配置文件[root@proxy nginx-1.22.1]# vim /usr/local/nginx/conf/nginx.conf #配置寫(xiě)到http的上方即可
...stream {upstream backend { #創(chuàng)建集群,名稱(chēng)為backendserver 192.168.99.100:22; #后端SSH服務(wù)器IP和端口server 192.168.99.200:22;}server { #調(diào)用集群listen 12345; #Nginx代理監(jiān)聽(tīng)的端口,可以自己定義proxy_pass backend; #調(diào)用backend集群}}
http {
.. ..
}
2)啟動(dòng)nginx
[root@proxy nginx-1.22.1]# /usr/local/nginx/sbin/nginx 3)客戶(hù)端使用訪問(wèn)代理服務(wù)器測(cè)試輪詢(xún)效果
[root@server1 ~]# ssh 192.168.99.5 -p 12345 #使用該命令多次訪問(wèn)查看輪詢(xún)效果
[root@web1 ~]# exit
[root@server1 ~]# ssh 192.168.99.5 -p 12345
[root@web2 ~]#
一、Nginx反向代理(七層代理)
實(shí)驗(yàn)要求
使用Nginx實(shí)現(xiàn)Web反向代理功能,實(shí)現(xiàn)如下功能:?
? ? 后端Web服務(wù)器兩臺(tái),可以使用httpd實(shí)現(xiàn)
? ? Nginx采用輪詢(xún)的方式調(diào)用后端Web服務(wù)器
? ? 兩臺(tái)Web服務(wù)器的權(quán)重要求設(shè)置為不同的值
? ? 最大失敗次數(shù)為2,失敗超時(shí)時(shí)間為30秒
實(shí)驗(yàn)環(huán)境
以下機(jī)器全部已經(jīng)存在,無(wú)須再次配置
主機(jī)名?? ?IP地址?? ?角色
server1(已存在)?? ?eth0:192.168.99.254/24?? ?客戶(hù)端
proxy(已存在)?? ?eth1:192.168.99.5/24?? ?代理服務(wù)器
web1(已存在)?? ?eth1:192.168.99.100/24?? ?web服務(wù)器
web2(已存在)?? ?eth1:192.168.99.200/24?? ?web服務(wù)器
image-20241008213058281
1)部署后端Web服務(wù)器
1)部署后端Web1服務(wù)器
后端Web服務(wù)器可以簡(jiǎn)單使用yum方式安裝httpd實(shí)現(xiàn)Web服務(wù),為了可以看出后端服務(wù)器的不同,可以將兩臺(tái)后端服務(wù)器的首頁(yè)文檔內(nèi)容設(shè)置為不同的內(nèi)容
[root@web1 ~]# yum -y install httpd
[root@web1 ~]# echo "web1" > /var/www/html/index.html
[root@web1 ~]# systemctl enable --now httpd
2)部署后端Web2服務(wù)器
[root@web2 ~]# yum -y install ?httpd
[root@web2 ~]# echo "web2" > /var/www/html/index.html
[root@web2 ~]# systemctl enable --now httpd
3)使用proxy主機(jī)測(cè)試
[root@proxy ~]# curl 192.168.99.100
web1
[root@proxy ~]# curl 192.168.99.200
web2
2)配置Nginx代理服務(wù)器
添加服務(wù)器池,實(shí)現(xiàn)反向代理功能
之前proxy主機(jī)安裝的nginx已經(jīng)改過(guò)很多配置,避免實(shí)驗(yàn)沖突,先還原proxy主機(jī)的nginx,重新安裝nginx
[root@proxy ~]# /usr/local/nginx/sbin/nginx ?-s stop ? ?#如果之前沒(méi)有啟動(dòng),可以不用執(zhí)行停止的命令
[root@proxy ~]# rm -rf /usr/local/nginx/
[root@proxy ~]# cd /root/lnmp_soft/
[root@proxy lnmp_soft]# rm -rf nginx-1.22.1
[root@proxy lnmp_soft]# tar -xf nginx-1.22.1.tar.gz
[root@proxy lnmp_soft]# cd nginx-1.22.1/
[root@proxy nginx-1.22.1]# yum -y install gcc make pcre-devel openssl-devel
[root@proxy nginx-1.22.1]# ./configure?
[root@proxy nginx-1.22.1]# make && make ?install ? ?
1)修改nginx的配置文件
[root@proxy nginx-1.22.1]# vim /usr/local/nginx/conf/nginx.conf
...
http {
...
#使用upstream定義后端服務(wù)器集群,集群名稱(chēng)任意(如webserver)
#使用server定義集群中的具體服務(wù)器和端口
? ? ? upstream webserver {
? ? ? ? ?server 192.168.99.100:80;
? ? ? ? ?server 192.168.99.200:80;
? ? ? ? }
? ? ? server {
? ? ? ? ? listen ? ? ? 80;
? ? ? ? ? server_name ?localhost;
? ? ? ? ??
? ? ? ? ? #charset koi8-r;
? ? ? ? ??
? ? ? ? ? #access_log ?logs/host.access.log ?main;
??
? ? ? ? ? location / {
? ? ? ? ? ? ? root ? html;
? ? ? ? ? ? ? index ?index.html index.htm;
? ? ? ? ? ? ? proxy_pass http://webserver; ?#通過(guò)proxy_pass將用戶(hù)的請(qǐng)求轉(zhuǎn)發(fā)給webserver集群
? ? ? ? ? }
?...
?
2)啟動(dòng)nginx
[root@proxy nginx-1.22.1]# /usr/local/nginx/sbin/nginx
3)使用真機(jī)瀏覽器做測(cè)試192.168.99.5,刷新可以看到網(wǎng)站的輪詢(xún)效果,出現(xiàn)結(jié)果為 web1 或者 web2
3)配置upstream服務(wù)器集群池屬性
1)設(shè)置權(quán)重
weight可以設(shè)置后臺(tái)服務(wù)器的權(quán)重,權(quán)重越大任務(wù)的分配量就越大
[root@proxy nginx-1.22.1]# vim /usr/local/nginx/conf/nginx.conf
.. ..
? ? ? upstream webserver {
? ? ? ? ?server 192.168.99.100:80 weight=2;
? ? ? ? ?server 192.168.99.200:80;
? ? ? ? }
? ? ? server {
?.. ..
2)重新加載配置并訪問(wèn),可以看到web1的任務(wù)量增加
[root@proxy nginx-1.22.1]# /usr/local/nginx/sbin/nginx -s reload
真機(jī)瀏覽器測(cè)試http://192.168.99.5,不太明顯
可以使用命令行測(cè)試
[root@server1 ~]# curl 192.168.99.5
web1
[root@server1 ~]# curl 192.168.99.5
web2
[root@server1 ~]# curl 192.168.99.5
web1
[root@server1 ~]# curl 192.168.99.5
web1
3)設(shè)置健康檢查max_fails可以設(shè)置后臺(tái)服務(wù)器連不上的失敗次數(shù),fail_timeout可以設(shè)置后臺(tái)服務(wù)器的失敗超時(shí)時(shí)間,等待多長(zhǎng)時(shí)間再次嘗試連接
[root@proxy nginx-1.22.1]# vim /usr/local/nginx/conf/nginx.conf
...
? ? ? upstream webserver {
? ? ? ? ?server 192.168.99.100:80;
? ? ? ? ?server 192.168.99.200:80 max_fails=2 fail_timeout=30;
? ? ? ? }
? ? ? server {
?...
4)重新加載配置并訪問(wèn)
[root@proxy nginx-1.22.1]# /usr/local/nginx/sbin/nginx -s reload
測(cè)試時(shí),先將web2的httpd服務(wù)關(guān)閉
[root@web2 ~]# systemctl stop ?httpd
使用真機(jī)命令行訪問(wèn)集群頁(yè)面curl 192.168.99.5,只會(huì)顯示web1的頁(yè)面
[root@server1 ~]# curl 192.168.99.5
web1
[root@server1 ~]# curl 192.168.99.5
web1
此時(shí)即使將web2的httpd服務(wù)開(kāi)啟也無(wú)效,因?yàn)橐却?0秒
[root@web2 ~]# systemctl start ?httpd
[root@server1 ~]# curl 192.168.99.5 #30秒之后再訪問(wèn),web2會(huì)出現(xiàn)
web2
4)配置upstream服務(wù)器集群的調(diào)度算法
測(cè)試ip_hash
1)設(shè)置相同客戶(hù)端訪問(wèn)相同Web服務(wù)器
[root@proxy nginx-1.22.1]# vim /usr/local/nginx/conf/nginx.conf
...
? ? ? upstream webserver {
? ? ? ? ?ip_hash;
? ? ? ? ?server 192.168.99.100:80;
? ? ? ? ?server 192.168.99.200:80;
? ? ? ? }
? ? ? server {
...
2)重新加載配置
[root@proxy nginx-1.22.1]# /usr/local/nginx/sbin/nginx -s reload
3)測(cè)試只會(huì)見(jiàn)到一個(gè)頁(yè)面
[root@server1 ~]# curl 192.168.99.5
web1
[root@server1 ~]# curl 192.168.99.5
web1
添加down標(biāo)記
down標(biāo)記可以讓集群主機(jī)暫時(shí)不參與集群活動(dòng)
[root@proxy nginx-1.22.1]# vim /usr/local/nginx/conf/nginx.conf
...
? ? ? upstream webserver {
? ? ? ? ?server 192.168.99.100:80;
? ? ? ? ?server 192.168.99.200:80 down;
? ? ? ? }
? ? ? server {
?...
重新加載配置
[root@proxy nginx-1.22.1]# /usr/local/nginx/sbin/nginx -s reload
測(cè)試,只會(huì)見(jiàn)到web1
[root@server1 ~]# curl 192.168.99.5
web1
[root@server1 ~]# curl 192.168.99.5
web1
二、Nginx的TCP/UDP調(diào)度器(四層代理)
實(shí)驗(yàn)要求
使用Nginx實(shí)現(xiàn)TCP/UDP調(diào)度器功能,實(shí)現(xiàn)如下功能:?
? ? 后端SSH服務(wù)器兩臺(tái)
? ? Nginx編譯安裝時(shí)需要使用--with-stream,開(kāi)啟ngx_stream_core_module模塊
? ? Nginx采用輪詢(xún)的方式調(diào)用后端SSH服務(wù)器
實(shí)驗(yàn)環(huán)境
以下機(jī)器全部已經(jīng)存在,無(wú)須再次配置
主機(jī)名?? ?IP地址?? ?角色
server1(已存在)?? ?eth0:192.168.99.254/24?? ?客戶(hù)端
proxy(已存在)?? ?eth1:192.168.99.5/24?? ?代理服務(wù)器
web1(已存在)?? ?eth1:192.168.99.100/24?? ?ssh服務(wù)器
web2(已存在)?? ?eth1:192.168.99.200/24?? ?ssh服務(wù)器
image-20241008213311333
1)部署nginx服務(wù)
支持4層TCP/UDP代理的Nginx服務(wù)器?
1)部署nginx服務(wù)器
編譯安裝必須要使用--with-stream參數(shù)開(kāi)啟4層代理模塊
[root@proxy nginx-1.22.1]# /usr/local/nginx/sbin/nginx -s stop
[root@proxy nginx-1.22.1]# rm -rf /usr/local/nginx/
[root@proxy nginx-1.22.1]# cd /root/lnmp_soft/nginx-1.22.1/
[root@proxy nginx-1.22.1]# yum -y install gcc make pcre-devel openssl-devel
[root@proxy nginx-1.22.1]# ./configure --with-stream ? ? ? ? ? ? ? ? ? ?#開(kāi)啟4層代理功能
[root@proxy nginx-1.22.1]# make && make install
[root@proxy nginx-1.22.1]# /usr/local/nginx/sbin/nginx -V ? ? ? ? ? #查看安裝模塊情況
nginx version: nginx/1.22.1
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-10) (GCC)?
configure arguments: --with-stream
2)配置Nginx服務(wù)
添加服務(wù)器池,實(shí)現(xiàn)四層代理功能
?1)修改nginx配置文件
?[root@proxy nginx-1.22.1]# vim /usr/local/nginx/conf/nginx.conf ? ?#配置寫(xiě)到http的上方即可
...
? ? ? stream {
? ? ? ? upstream backend { ? ? ? ? ? ? #創(chuàng)建集群,名稱(chēng)為backend
? ? ? ? ? ? server 192.168.99.100:22; ?#后端SSH服務(wù)器IP和端口
? ? ? ? ? ? server 192.168.99.200:22;
? ? ? ? ? }
? ? ? ? ?server { ? ? ? ? ? ? ? #調(diào)用集群
? ? ? ? ? ? listen 12345; ? ? ? #Nginx代理監(jiān)聽(tīng)的端口,可以自己定義
? ? ? ? ? ? proxy_pass backend; #調(diào)用backend集群
? ? ? ? ? }
? ? ?}
http {
.. ..
}
2)啟動(dòng)nginx
[root@proxy nginx-1.22.1]# /usr/local/nginx/sbin/nginx?
3)客戶(hù)端使用訪問(wèn)代理服務(wù)器測(cè)試輪詢(xún)效果
[root@server1 ~]# ssh 192.168.99.5 -p 12345 ? ? #使用該命令多次訪問(wèn)查看輪詢(xún)效果
[root@web1 ~]# exit
[root@server1 ~]# ssh 192.168.99.5 -p 12345
[root@web2 ~]#