正向代理 、反向代理, 和 Linux系统配置nginx。

一、正向代理和反向代理的简单介绍。

2.代理
        中间商,赚差价
        在没有代理的时候:
            茅台酒厂--->生产了一批酒--->通过物流发送到客户的家中/客户直接到酒厂购买酒--->突然有一个客户晚上需要情况吃饭(现在已经15:00)(客户在东北)-->耽误了各种情况
        代理:
            茅台酒厂--->开始招代理(省级)--->市的代理-->客户需要购买茅台酒的时候只需要代理商购买就可以

    3.正向代理
        场景:
            A向B借钱-->B不借-->A发现C和B关系很好(A和C关系很好)-->A拜托C向B借钱(不要说是我借的)-->C就找B借钱-->B把钱借给C-->C把钱给A-->B并不知道谁真正在向自己借钱-->A达到了自己的目的
        定义:
            客户端向真实的服务器端发送请求,但是出于某种原因无法向真实的客户端发送请求,客户端就找到代理服务器,把请求发送给代理服务器,再由代理服务器把请求发送给真实的服务器,真实服务器并不知道具体访问我的客户端是谁(真实服务器看到的访问自己的是代理,并不是真实的客户端)
        特点:
            正向代理是和客户端在一起的,所有的配置都必须在客户端完成,真实的服务器端并不知道真实的客户端是谁

    4.反向代理
        场景:
            某个人向某个单位打电话-->打给单位的总机-->总机会根据这个人的描述(找到某个部门,某个人)-->转接到这个部门(该部门有5个人)-->打电话的人知不知道具体是这个部门的哪一个人接听
            交话费没有到账--->10086/10010/10001--->移动公司的总机-->人工操作-->总机会转接到人工-->打电话的人不知道具体谁接听电话-->达到了自己想要的结果
        定义:
            客户端向服务器端发送请求(服务器端是一个集群(4台服务器)),客户端并不知道具体访问哪一台服务器,客户端的请求就会被代理服务器所拦截,再由代理服务器把请求转交给集群中的某一个真实服务器,真实服务器最终把结果响应给代理服务器,代理服务器再把结果返回给客户端,客户端并不知道具体请求的服务器是真实服务器还是代理服务器
        特点:
            反向代理是和服务器端在一起的,所有的配置都必须要在服务器端完成,客户端并不知道真实的服务器是谁(客户端并不知道自己请求的是代理服务器还是真实服务器)

    5.比对正向代理和反向代理
        正向代理是和客户端在一起,两个人合伙欺骗服务器端
        反向代理是和服务器端在一起,反向代理过滤/淘汰了某些客户端的非法请求
        //nginx的优点。
        nginx做反向代理的时候
            1.保证了整个系统的安全性
            2.起到了分发请求的作用

二、新建一个Linux系统,配置nginx。

  1 1.修改hostname
  2     vi /etc/sysconfig/wetwork
  3 
  4 2.修改hostname 和 ip的映射
  5     vi /etc/hosts
  6 
  7 3.关闭防火墙
  8     service iptables stop 
  9 
 10 4.关闭防火墙的开机自启动
 11     chkconfig iptables off
 12 
 13 5.重启Linux虚拟机
 14     reboot
 15 
 16         
 17         6.使用xftp工具把nginx压缩包上传linux服务器上 我用的是 1.17.3版本
 18         
 19         7.解压nginx
 20             tar -zxvf xxxxx
 21             
 22         8.创建快捷方式
 23             进入nginx目录
 24             ./configure --prefix=/usr/local/nginx-1.17.3(需要手动输入,linux上没有该路径)
 25             
 26             
 27             报错:
 28                 ./configure: error: the HTTP rewrite module requires the PCRE library.
 29                 You can either disable the module by using --without-http_rewrite_module
 30                 option, or install the PCRE library into the system, or build the PCRE library
 31                 statically from the source with nginx by using --with-pcre=<path> option.
 32             linux服务器上并没有nginx所必须要的配置包
 33             
 34         9.安装nginx所需要的配置包
 35         命令:
 36             yum -y install gcc pcre-devel openssl openssl-devel
 37             
 38             /*
 39                 yum -y install gcc pcre-devel openssl openssl-devel
 40                 yum -y install gcc pcre-devel openssl openssl-devel
 41                 yum -y install gcc pcre-devel openssl openssl-devel
 42                 yum -y install gcc pcre-devel openssl openssl-devel 
 43                 yum -y install gcc pcre-devel openssl openssl-devel 
 44                 yum -y install gcc pcre-devel openssl openssl-devel
 45                 
 46             */
 47             
 48             看到以下信息说明安装成功:
 49               Dependency Updated:
 50               cpp.x86_64 0:4.4.7-23.el6             e2fsprogs.x86_64 0:1.41.12-24.el6    e2fsprogs-libs.x86_64 0:1.41.12-24.el6   
 51               libcom_err.x86_64 0:1.41.12-24.el6    libgcc.x86_64 0:4.4.7-23.el6         libgomp.x86_64 0:4.4.7-23.el6            
 52               libss.x86_64 0:1.41.12-24.el6        
 53             Complete!
 54             
 55             
 56         10.再次创建快捷方式      需要在nginx的目录中创建
 57         
 58             ./configure --prefix=/usr/local/nginx-1.17.3
 59             
 60             /*
 61                 ./configure --prefix=/usr/local/nginx-1.17.3
 62                 ./configure --prefix=/usr/local/nginx-1.17.3
 63                 ./configure --prefix=/usr/local/nginx-1.17.3
 64                 ./configure --prefix=/usr/local/nginx-1.17.3
 65                 ./configure --prefix=/usr/local/nginx-1.17.3
 66                 ./configure --prefix=/usr/local/nginx-1.17.3
 67             
 68             */
 69         
 70             
 71             看到以下信息说明创建成功:
 72                 Configuration summary
 73               + using system PCRE library
 74               + OpenSSL library is not used
 75               + using system zlib library
 76 
 77               nginx path prefix: "/usr/local/nginx-1.17.3"
 78               nginx binary file: "/usr/local/nginx-1.17.3/sbin/nginx"
 79               nginx modules path: "/usr/local/nginx-1.17.3/modules"
 80               nginx configuration prefix: "/usr/local/nginx-1.17.3/conf"
 81               nginx configuration file: "/usr/local/nginx-1.17.3/conf/nginx.conf"
 82               nginx pid file: "/usr/local/nginx-1.17.3/logs/nginx.pid"
 83               nginx error log file: "/usr/local/nginx-1.17.3/logs/error.log"
 84               nginx http access log file: "/usr/local/nginx-1.17.3/logs/access.log"
 85               nginx http client request body temporary files: "client_body_temp"
 86               nginx http proxy temporary files: "proxy_temp"
 87               nginx http fastcgi temporary files: "fastcgi_temp"
 88               nginx http uwsgi temporary files: "uwsgi_temp"
 89               nginx http scgi temporary files: "scgi_temp"
 90               
 91         11.编译nginx  还需要在nginx的目录
 92             
 93             make && make install
 94         /*    make && make install
 95             make && make install
 96             make && make install
 97             make && make install
 98             make && make install
 99         */    
100             看到以下信息说明编译成功:
101                 make[1]: Leaving directory `/home/apps/nginx-1.17.3'
102                 
103                 
104         12.启动nginx
105             不要再nginx的解压包目录操作,需要在快捷方式的目录执行(/usr/local/nginx-1.17.3)
106             
107             ./nginx(在sbin目录)
108         /*    ./nginx 
109             ./nginx
110             ./nginx 
111             ./nginx 
112             ./nginx
113         */
114             如果没有报错,说明启动成功
115             
116             
117         13.在浏览器中输入Linux的ip检测
118             192.168.132.130
119             
120             看到以下页面说明安装成功:
121                 Welcome to nginx!
122                 If you see this page, the nginx web server is successfully installed and working. 
123                 Further configuration is required.
124 
125                 For online documentation and support please refer to nginx.org.
126                 Commercial support is available at nginx.com.
127 
128                 Thank you for using nginx.

三、反向代理的配置

14.到/usr/local/nginx-1.17.3的config中进行配置
        
            vim nginx.conf
            /*
                vim nginx.conf
                vim nginx.conf
                vim nginx.conf
                vim nginx.conf
                vim nginx.conf
            
            */
        //    注意 必须要在http{}标签里面实现所有的配置

                upstream tomcat_server {   //第一次安装  加 “_”  我的报错了  网上搜索说是版本原因,去掉 "_" 即可。  
                          //这个ip 是安装有Tomcat服务器的 虚拟机 web01    
                    server 192.168.128.131:8080;  (分号不要忘了!!!!)
                }
                
                
        //    upstream必须要配置在server{}标签的上面
            有加载顺序:
                如果配置在了server的下面,最终因为加载顺序缘故(自上而下的加载顺序),server中加载不到upstream,则就会报错!!!!
                配置server{}标签中location /{}标签
                
                把location /{}标签中的所有内容全部删除
                location / {
                    proxy_pass http://tomcat_server; //upstream的名字,必须要保持一致,否则无法找到映射)
                }
                
        15.重新nginx
            需要在sbin目录
            ./nginx -s reload
            
            /*
                ./nginx -s reload 
                ./nginx -s reload
                ./nginx -s reload
                ./nginx -s reload
            */
            

        16.配置1311服务器上的tomcat
        
            
        17.检测nginx的反向代理配置成功
        
            两台服务器
            nginx代理服务器 192.168.128.130
            web服务器(运行的web项目) 192.168.128.131
            客户端发送请求--->web服务器--->被代理服务器拦截--->再由代理服务器把请求转发给web服务器
            客户端访问的nginx代理服务器ip,获取的web上的tomcat的页面

        6.18.其实反向代理很好的保证真实服务器的安全性,也就是说隐藏了真实服务器的地址!!!
            客户端可以通过请求代理服务器的形式直接访问到真实服务器,客户端毫无察觉
原文地址:https://www.cnblogs.com/ZXF6/p/11437869.html