Nginx只做请求的转发,后台有多个http服务器提供服务,nginx的功能就是把请求转发给后面的服务器,决定把请求转发给谁。
在一个虚拟机上创建两个tomcat实例,模拟多个服务器。
通过访问不同的域名访问运行在不同端口的tomcat
8080.itheima.com 访问运行8080端口的tomcat
8081.itheima.com 访问运行8081端口的tomcat
1 upstream tomcatserver1 { 2 server 192.168.83.133:8081; 3 } 4 upstream tomcatserver2 { 5 server 192.168.83.133:8082; 6 } 7 server { 8 listen 80; 9 server_name 8081.ccc.com; 10 11 #charset koi8-r; 12 13 #access_log logs/host.access.log main; 14 15 location / { 16 proxy_pass http://tomcatserver1; 17 index index.html index.htm; 18 } 19 20 21 } 22 server { 23 listen 80; 24 server_name 8082.ccc.com; 25 26 #charset koi8-r; 27 28 #access_log logs/host.access.log main; 29 30 location / { 31 proxy_pass http://tomcatserver2; 32 index index.html index.htm; 33 } 34 35 36 }
浏览器访问:
如果在同一个域名下有多台服务器提供服务,此时需要nginx负载均衡。
原文:https://www.cnblogs.com/116970u/p/10481125.html