使用nginx实现反向代理

1    使用nginx实现反向代理

Nginx只做请求的转发,后台有多个http服务器提供服务,nginx的功能就是把请求转发给后面的服务器,决定把请求转发给谁。

1.1   安装tomcat

在一个虚拟机上创建两个tomcat实例,模拟多个服务器。

1.2   需求

通过访问不同的域名访问运行在不同端口的tomcat

8080.itheima.com  访问运行8080端口的tomcat

8081.itheima.com  访问运行8081端口的tomcat

1.3   域名需要配置host(Windows)文件:

1.4   Nginx的配置

 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负载均衡。

posted @ 2019-03-06 08:47  清晨的第一抹阳光  阅读(177)  评论(0编辑  收藏  举报