一、反向代理

反向代理实例一

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;所以会自动列出所有的文件信息

最新文章

  1. iscroll 下拉刷新功能
  2. maven引入本地jar
  3. 深入理解Ember-Data特性(上)
  4. JavaScript基础15——js的DOM对象
  5. 由源码密码文件转转化成keystore
  6. PHP 常用到的一些小程序
  7. typedef定义函数类型或函数指针
  8. SQL Server 2008 忘记sa密码的解决办法
  9. WPF之Binding深入探讨--Darren
  10. 为代码减负之&amp;lt;一&amp;gt;触发器(SQL)
  11. 根据XPATH去查看修改xml文件节点的内容
  12. hdu4352 数位dp+状态压缩+一个tip
  13. matplotlib坐标轴刻度-【老鱼学matplotlib】
  14. 使用jquey 须掌握的常见知识点
  15. linux下tomcat服务器的相关命令
  16. 【杂谈】对IO与NIO的认识
  17. .Net Core 简单定时任务框架封装
  18. JavaScript事件模型
  19. LeetCode题解之Binary Tree Tilt
  20. Android进程和线程(Android开发指南--译)

热门文章

  1. spring boot:用dynamic-datasource-spring-boot-starter配置多数据源访问seata(seata 1.3.0 / spring boot 2.3.3)
  2. centos8平台搭建mysql8数据库主从同步
  3. CentOS8安装本地mail工具-mailx-12.5-29.el8.x86_64
  4. Cypress系列(69)- route() 命令详解
  5. Activity去掉标题不成功的解决方法
  6. LruCache缓存bitmap(一)
  7. window 属性:自定义元素(custom elements)
  8. nacos 作为配置中心使用心得--配置使用
  9. 深入web workers (上)
  10. 【kotlin】adapterPosition方法返回-1 无法获取位置