1、反向代理html
反向代理实例一nginx
1.实现效果web
打开浏览器,在浏览器地址栏输入地址www.pluto.com,跳转到 liunx 系统 tomcat 主页面中apache
2.准备工做bootstrap
[1].安装tomcatvim
[root@host79 sbin]# cd /opt/apache-tomcat-7.0.70/windows [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/tomcat [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].测试
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 } |
3、动静分离
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;因此会自动列出全部的文件信息