nginx使用-反向代理 02

简介

正向代理:架设在客户机与目标主机之间,只用于代理内部网络对Internet的连接请求,客户机必须指定代理服务器,并将本来要直接发送到Web服务器上的http请求发送到代理服务器中。

反向代理(Reverse Proxy):方式是指以代理服务器来接受Internet上的连接请求,然后将请求转发给内部网络上的服务器;并将从服务器上得到的结果返回给Internet上请求连接的客户端,此时代理服务器对外就表现为一个服务器。

1.模拟三个http服务器作为目标主机,在本机搭建三个tomcat,分别为三个不同的服务,将端口改为8081,8082,8083并启动

2.修改host文件

127.0.0.1       test1.com
127.0.0.1       test2.com
127.0.0.1       test3.com

3.修改nginx.conf

upstream test1 {
    server 127.0.0.1:8081;  
    }
upstream test2 {
    server 127.0.0.1:8082;  
    }
upstream test3 { 
    server 127.0.0.1:8083;  
    }
server {
        listen       80;
        server_name  www.test1.com;
  
        #charset koi8-r;  
  
        #access_log  logs/host.access.log  main;  
  
        location / {  
            proxy_pass   http://test1;  
            index  index.html index.htm;  
        }
    }
server {
        listen       80;  
        server_name  www.test2.com;  
  
        #charset koi8-r;  
  
        #access_log  logs/host.access.log  main;  
  
        location / {  
            proxy_pass   http://test2;  
            index  index.html index.htm;  
        }        
    }
server {
        listen       80;  
        server_name  www.test3.com;  
  
        #charset koi8-r;  
  
        #access_log  logs/host.access.log  main;  
  
        location / {
            proxy_pass   http://test3;  
            index  index.html index.htm;  
        }     
    }

4.测试

在浏览器上输入www.test1.com,nginx反向代理接受客户机请求,找到server_name为test.com的server节点,根据proxy_pass对应的http路径,将请求转发到upstream test1指定的ip+port。


猜你喜欢

转载自blog.csdn.net/shy415502155/article/details/80935824