一、反向代理
反向代理實體一
1.實作效果
打開瀏覽器,在瀏覽器地址欄輸入地址www.pluto.com,跳轉到 liunx 系統 tomcat 主頁面中
2.準備作業
[1].安裝tomcat
|
[root@host79 sbin]# cd /opt/apache-tomcat-7.0.70/ [root@host79 apache-tomcat-7.0.70]# ls bin conf lib LICENSE logs NOTICE RELEASE-NOTES RUNNING.txt temp webapps work [root@host79 apache-tomcat-7.0.70]# cd bin/ [root@host79 bin]# ./startup.sh Using CATALINA_BASE: /opt/apache-tomcat-7.0.70 Using CATALINA_HOME: /opt/apache-tomcat-7.0.70 Using CATALINA_TMPDIR: /opt/apache-tomcat-7.0.70/temp Using JRE_HOME: /opt/jdk1.7.0_79 Using CLASSPATH: /opt/apache-tomcat-7.0.70/bin/bootstrap.jar:/opt/apache-tomcat-7.0.70/bin/tomcat-juli.jar Tomcat started. [root@host79 bin]# cd .. [root@host79 apache-tomcat-7.0.70]# cd logs/ [root@host79 logs]# ls catalina.2020-07-20.log host-manager.2020-08-12.log localhost_access_log.2020-08-12.txt catalina.2020-07-29.log localhost.2020-07-20.log manager.2020-07-20.log catalina.2020-08-12.log localhost.2020-07-29.log manager.2020-08-12.log catalina.out localhost.2020-08-12.log host-manager.2020-07-20.log localhost_access_log.2020-07-20.txt [root@host79 logs]# tail -f catalina.out 八月 12, 2020 11:21:04 下午 org.apache.catalina.startup.HostConfig deployDirectory 資訊: Deploying web application directory /opt/apache-tomcat-7.0.70/webapps/ROOT 八月 12, 2020 11:21:04 下午 org.apache.catalina.startup.HostConfig deployDirectory 資訊: Deployment of web application directory /opt/apache-tomcat-7.0.70/webapps/ROOT has finished in 183 ms 八月 12, 2020 11:21:04 下午 org.apache.coyote.AbstractProtocol start 資訊: Starting ProtocolHandler ["http-bio-8080"] 八月 12, 2020 11:21:04 下午 org.apache.coyote.AbstractProtocol start 資訊: Starting ProtocolHandler ["ajp-bio-8009"] 八月 12, 2020 11:21:04 下午 org.apache.catalina.startup.Catalina start 信息: Server startup in 5663 ms |
[2].開放埠
|
[root@host79 /]# vim /etc/sysconfig/iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT [root@host79 /]# service iptables restart [root@host79 /]# /etc/init.d/iptables status |
3.具體配置
[1].windows系統修改hosts檔案
在 windows 系統的 host 檔案進行域名和 ip 對應關系的配置
|
#C:\Windows\System32\drivers\etc\hosts 192.168.188.188 www.pluto.com |
[2].nginx反向代理配置(請求轉發配置)
|
[root@host79 /]# cd /usr/local/nginx/conf/ [root@host79 conf]# vim nginx.conf |
[3].測驗
|
[root@host79 /]# cd /usr/local/nginx/sbin/ [root@host79 sbin]# ./nginx -s reload |
反向代理實體二
1.實作效果
使用 nginx 反向代理,根據訪問的路徑跳轉到不同埠的服務中nginx 監聽埠為 8001.
訪問 http://192.168.188.188:8001/edu/ 直接跳轉到 127.0.0.1:8080
訪問 http://192.168.188.188:8001/vod/ 直接跳轉到 127.0.0.1:8081
2.準備作業
[1].準備兩個tomcat服務器,一個8080埠,一個8081埠
|
#8080tomcat [root@host79 bin]# pwd /opt/tomcat8080/apache-tomcat-7.0.70/bin [root@host79 bin]# ./start
#8081tomcat [root@host79 conf]# pwd /opt/tomcat8081/apache-tomcat-7.0.70/conf [root@host79 conf]# vim server.xml <Server port="8015" shutdown="SHUTDOWN"> <Connector port="8081" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <Connector port="8019" protocol="AJP/1.3" redirectPort="8443" /> |
[2].將埠添加到防火墻
|
[root@host79 conf]# vim /etc/sysconfig/iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 8081 -j ACCEPT |
[3].測驗頁面
[4].創建檔案夾
|
[root@host79 /]# cd /opt/tomcat8080/apache-tomcat-7.0.70/webapps/ [root@host79 webapps]# mkdir edu [root@host79 webapps]# cd edu [root@host79 edu]# vim a.html <h1>8080</h1>
[root@host79 /]# cd /opt/tomcat8081/apache-tomcat-7.0.70/webapps/ [root@host79 webapps]# mkdir vod [root@host79 webapps]# cd vod [root@host79 webapps]# vim a.html <h1>8081</h1> |
3.具體配置
[1].nginx組態檔(反向代理配置)
|
[root@host79 conf]# pwd /usr/local/nginx/conf [root@host79 conf]# vim nginx.conf #通過以下命令查看防火墻是否放行8080 8081 8001埠 [root@host79 conf]# service iptables status [root@host79 sbin]# pwd /usr/local/nginx/sbin [root@host79 sbin]# ./nginx -s reload #若出現nginx: [emerg] invalid URL prefix in URL/nginx.conf,則配置nginx.conf時一定出錯了 #若出現nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory) ,則是https://www.cnblogs.com/houss/p/11291629.html |
[2].測驗
二、負載均衡
1.實作效果
瀏覽器地址欄輸入地址 http://192.168.188.188/edu/a.html,負載均衡效果,平均 8080和 8081 埠中
2.準備作業
[1].準備兩個tomcat服務器,一個8080埠,一個8081埠
|
#8080tomcat [root@host79 bin]# pwd /opt/tomcat8080/apache-tomcat-7.0.70/bin [root@host79 bin]# ./start
#8081tomcat [root@host79 conf]# pwd /opt/tomcat8081/apache-tomcat-7.0.70/conf [root@host79 conf]# vim server.xml <Server port="8015" shutdown="SHUTDOWN"> <Connector port="8081" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <Connector port="8019" protocol="AJP/1.3" redirectPort="8443" /> |
[2].將埠添加到防火墻
|
[root@host79 conf]# vim /etc/sysconfig/iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 8081 -j ACCEPT |
[3].創建檔案
|
[root@host79 ~]# cd /opt/tomcat8080/apache-tomcat-7.0.70/webapps/ [root@host79 webapps]# mkdir pluto [root@host79 webapps]# cd pluto [root@host79 pluto]# vim a.html <h1>8080!!!!!!</h1>
[root@host79 ~]# cd /opt/tomcat8081/apache-tomcat-7.0.70/webapps/ [root@host79 webapps]# mkdir pluto [root@host79 webapps]# cd pluto [root@host79 pluto]# vim a.html <h1>8081!!!!!!!!!</h1> |
[4].測驗
3.nginx組態檔(負載均衡配置)
|
[root@host79 conf]# pwd /usr/local/nginx/conf [root@host79 conf]# vim nginx.conf #gzip on;
upstream myserver{ server 192.168.188.188:8080; server 192.168.188.188:8081; } server { listen 80; server_name 192.168.188.188; location / { root html; proxy_pass http://myserver; index index.html index.htm; } |
4.nginx分配服務器策略
[1].輪詢(默認)
每個請求按時間順序逐一分配到不同的后端服務器,如果后端服務器 down 掉,能自動剔除,
|
|
[2].weight
weight 代表權重默認為 1,權重越高被分配的客戶端越多.weight 和訪問比率成正比,用于后端服務器性能不均的情況,
|
[root@host79 conf]# pwd /usr/local/nginx/conf [root@host79 conf]# vim nginx.conf #gzip on;
upstream myserver{ server 192.168.188.188:8080 weight=10; server 192.168.188.188:8081 weight=5; } server { listen 80; server_name 192.168.188.188; location / { root html; proxy_pass http://myserver; index index.html index.htm; } |
[3].ip_hash
每個請求按訪問 ip 的 hash 結果分配,這樣每個訪客固定訪問一個后端服務器,可以解決 session 的問題
|
upstream myserver{ ip_hash server 192.168.188.188:8080; server 192.168.188.188:8081; } |
[4].fair(第三方)
按后端服務器的回應時間來分配請求,回應時間短的優先分配,
|
upstream myserver{ server 192.168.188.188:8080; server 192.168.188.188:8081; fair } |
三、動靜分離
1.實作效果
2.準備作業
|
[root@host79 /]# mkdir data [root@host79 data]# mkdir www [root@host79 data]# mkdir image [root@host79 data]# ll 總用量 8 drwxr-xr-x. 2 root root 4096 8月 13 15:43 image drwxr-xr-x. 2 root root 4096 8月 13 15:49 www [root@host79 data]# cd www/ [root@host79 www]# vim a.html <h1>test html!!!</h1>
[root@host79 data]# cd /data/image/ #上傳一張圖片
|
3.具體配置
|
[root@host79 /]# cd /usr/local/nginx/conf/ [root@host79 conf]# vim nginx.conf |
4.測驗
注:因為nginx.conf中添加了autoindex on;所以會自動列出所有的檔案資訊
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/197483.html
標籤:其他
上一篇:openpyxl 遇到序列例外
下一篇:使用微創聯合M5S空氣檢測儀、樹莓派3b+、prometheus、grafana實作空氣質量持續監控告警WEB可視化
