• Nginx反向代理讲解和配置


    首先来介绍下Nginx的反向代理。代理服务器一般分为正向代理(通常直接称为代理服务器)和反向代理。

    画个图我们就好理解了。

    正向代理:可以想象成是路由器,我们要通过它来上网的那种。(可以说是客户端的代理)

    Nginx反向代理搭建配置及搭建过程一些思考

    反向代理:客户端的请求过来之后交给反向代理服务器,然后反向代理服务器再交给后台真实的服务器。(这个是服务器端的代理)

    Nginx反向代理搭建配置及搭建过程一些思考

    我们今天说的是nginx的反向代理功能的实现。同时,反向代理还可以实现负载均衡的功能。可以自己思考下。

    由于实验比较简单,这边环境就简单处理。

    http-server1:192.168.10.156(Apache服务)

    http-server2:192.168.10.157(nginx服务)

    nginx-proxy:192.168.10.159

    保证http服务器搭建完成,并能正常访问

    nginx-proxy安装(其他依赖条件自己安装)。版本:nginx-1.4.7.tar.gz

    tar -zxvf nginx-1.4.7.tar.gz /home

    ./configure

    --prefix=/usr/local/nginx

    --sbin-path=/usr/local/bin 

    --conf-path=/etc 

    --error-log-path=/usr/local/nginx/error.log 

    --pid-path=/usr/local/nginx/ngnix.pid

    --lock-path=/usr/local/nginx/nginx.lock 

    --with-http_ssl_module 

    --with-http_gzip_static_module 

    --with-http_perl_module

    --http-log-path=/usr/local/nginx/access.log

    --http-fastcgi-temp-path=/usr/local/nginx/html

    --with-pcre --with-zlib=/usr --with-openssl=/usr

    make && make install

    编辑nginx的配置文件。

    vim /etc/nginx.conf

    #user  nobody;


    worker_processes  1;


    events {

        worker_connections  1024;

    }

    http {

         upstream loadbance {

                    server 192.168.10.156;

                    server 192.168.10.157;(如果其其他端口,后面请加端口号:X.X.X.X:8888)

            }

         server {

                    listen 80;

                    location / {

                            proxy_pass http://loadbance;

                    }

            }

        }

    红色部分为搭建反向代理服务器所需要的简单配置。(注意,nginx行尾的分号)

    upstream:反向代理的关键字

    loadbance:你可以理解为后台一组服务器的名称

    server: 后台真实的服务器地址,可以是IP或者FQDN(如果是FQDN,别忘记解析)

    listen:监听的端口,如果没有把原来nginx做web服务器的配置内容注销掉,建议改为其他端口

    proxy_pass:后面跟http://loadbance  此一定要和upstream后面(服务器组名称)的名称保持一致


    OK,大功告成。访问进行测试。

    访问代理服务器地址:http://192.168.10.159,然后刷新

    Nginx反向代理搭建配置及搭建过程一些思考

    Nginx反向代理搭建配置及搭建过程一些思考

    这就是nginx的反向代理。其实我们还可以延伸下。

    nginx的反向代理功能我认为就可以当做一个简单的负载均衡来使用,我们可以指定nginx的调度算法:

    nginx的官网上说(其实个人认为不止4中,你可以在服务器的ip地址后面加上服务器的权值,但是按官网上加上权值是不算一种。)

    NGINX supports four load balancing methods:

    1):The round-robin method    轮询

    2):The least_conn method  最少连接数

    a request is sent to the server with the least number of active connections with server weights taken into consideration。请求会送到活跃链接最少的服务器上(服务器的权值要考虑进来的)

    3):The ip_hash method 

    The method guarantees that requests from the same address get to the same server unless it is not available。这种方法保证了来自同一个IP地址的请求会得到同一个服务器的响应,除非挂了。(其是通过客户端的ip(IPV4 or IPV6)地址来计算出此IP的hash值的)

    4);The generic hash method:

     the server to which a request is sent is determined from a user-defined key which may be a text, variable, or their combination. For example, the key may be a source IP and port,

    请求会发送到一个用户定义键值(可以是text,变量,或者混合的)的服务器上

    再来看一个官网说的:

    If a method other than the default one is used, the corresponding directive (least_conn,ip_hash or hash) should be specified inside the upstream before the server directives.如果你要使用其他的调度算法(默认使用的round-robin),相应的指令必须在upstream内部指定,并且要在server指令之前。

    比如:最小数链接/ip_hash/hash $request_uri consistent;(如果是使用轮询的方式的话,就不用写了,因为默认算法就是轮询) 

    upstream loadbance {

                    least_conn;   //或者是ip_hash和(hash $request_uri consistent)

                    server 192.168.10.156 weight=3;  //可以设置权值的哦

                    server 192.168.10.157;(如果其其他端口,后面请加端口号:X.X.X.X:8888)

                    } 

    哎呀,我擦,这就扯远了。其实,nginx还有一个健康检查的功能,如果有台服务器挂了,请求不会分发到该服务器上。比如下面的写法: 

    upstream loadbance {

                    server 192.168.10.156 weight=3; 

                    server 192.168.10.157;

                    server 192.168.10.158 backup;//当备机使用

                    server 192.168.10.155 down;  //如果你要临时移除一台服务器进行升级或者其他操作,可以把其标记为down的状态,这样请求就不会到其上。

                    }


    其实,我们还可以通过server内部的location指令来指定不同的路径分发到不同的服务器上去。来实现分流。

    比如,可以定义多个upstream。upstream1,upstream2.......然后

    location  /mp3 {

        proxy_pass http://upstream1;

        }


    location /flv {

        proxy_pass http://upstream2;

        }

  • 相关阅读:
    重定向是否可以重定向到post接口
    ForkJoin(工作窃取)初步使用,计算偶数集合
    Dubbo服务的三种发布模式
    mysql开启慢查询日志
    Hashmap的结构,1.7和1.8有哪些区别
    idea回滚已经push的代码
    rabbitmq集群安装配置
    restful好处,表单提交put/delete
    BIO/NIO/AIO待完成
    判断一个对象是否可以被回收
  • 原文地址:https://www.cnblogs.com/anruy/p/4989043.html
Copyright © 2020-2023  润新知