nginx_http_proxy,upstream,stream模块简析

一. ngx_http_proxy_module模块:

        模块功能: 为后端httpd服务做反向代理, 并且与Httpd 之间使用http进行通信


      1、proxy_pass URL;

         Context: location, if in location, limit_except

        

        当root 与proxy_pass 同时存在是,proxy 优先级更高


    ——————————————————————————————

         A:注意:proxy_pass后面的路径不带uri时,其会将location的uri传递(添加到结尾) \

        给后端主机;

        

         server {

         …

         server_name HOSTNAME;

         location /uri/ {

         proxy_pass http://hos[:port];   #即此处结尾无"/"

         }

         …

         }

        

         访问时候: http://HOSTNAME/uri –> http://host/uri   #补充

        

         B:  proxy_pass后面的路径是一个uri时,其会将location的uri替换为proxy_pass的uri,

        即new_uri;

        

         server {

         …

         server_name HOSTNAME;

         location /uri/ {

         proxy_pass http://host/new_uri/;

         }

         …

         }

        

         http://HOSTNAME/uri/ –> http://host/new_uri/  #替换

        

         C:  如果location定义其uri时使用了正则表达式的模式,则proxy_pass之后必须不能使用

        uri; 用户请求时传递的uri将直接附加代理到的服务的之后;

        

         server {

         …

         server_name HOSTNAME;

         location ~|~* /uri/ {

         proxy_pass http://host;

         }

         …

         }

        

         http://HOSTNAME/uri/ –> http://host/uri/;

    ————————————————————————————

        =========================================================

        使用示例:(备注:此处仅为单台httpd 服务器代理)


        1.   在前端nginx 调度器配置中,修改/etc/nginx/nginx.conf:

         添加 proxy_pass http://ip; 即可

        wKiom1gUi5qDicezAAAu8fHQGpc970.png-wh_50

        示例2.

        当访问 http://10.1.249.143 时,为nginx本地web服务,

        访问 http:// 10.1.249.143/admin 时,则代理到后端的服务器

        wKioL1gUi5rjrU7gAAAg4Pbvlbg067.png-wh_50

         =======================================================


      2、proxy_set_header field value;

         作用: 设定发往后端主机的请求报文的请求首部的值;

        允许使用自定义的 首部信息;

        Context: http, server, location

         补充:

        前端的nginx代理,可以捕获客户端发送来的请求报文首部,并

        保存为$proxy_add_x_forwarded_for, 此值可以传递给后续的代理服务器

        eg:

         proxy_set_header X-Real-IP  $remote_addr;

        #将请求的客户端远程地址传送给后端服务器

        wKiom1gUi53ykED8AAD3_df8JmI227.png-wh_50

        此时需要修改一下后端httpd 服务器的日志格式,以便可以直观的看到效果:

        修改/etc/httpd/conf/httpd.conf

        LogFormat 中的combined项,在前面的%h 修改为%{X-Real-IP}i

        wKioL1gUi6Kgoc6rAADWwTTecwo896.png-wh_50

        

wKiom1gUi6WxI9GbAABPrH3C2_4194.png-wh_50


        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    

          wKiom1gUi7OR9PcZAAA7OM20aOA471.png-wh_50

        ================================================================

    

    3、proxy_cache_path

         定义可用于proxy功能的缓存;

           Context: http

    

     proxy_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size]     [manager_files=number] [manager_sleep=time] [manager_threshold=time] [loader_files=number] [loader_sleep=time]     [loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time];

    

     4、proxy_cache zone | off;

         指明要调用的缓存,或关闭缓存机制;

        Context: http, server, location

        

     5、 proxy_cache_key string;

         #指明缓存中用于“键”的指定内容;

        

         默认值:proxy_cache_key $scheme$proxy_host$request_uri;

           #若希望公用缓存,则只是用$request_uri


     6、proxy_cache_valid [code …] time;

         #定义对特定响应码的响应内容的缓存时长;

         #若想全局生效,可以在server中定义,若希望局部uri生效,则在location中定义

         定义在http{…}中;

        eg:

         proxy_cache_path /var/cache/nginx/proxy_cache levels=1:1:1 keys_zone=pxycache:20m max_size=1g;

         #注意,缓存的目录需要事先创建


         定义在需要调用缓存功能的配置段,例如server{…};

         proxy_cache pxycache;

         proxy_cache_key $request_uri;

         proxy_cache_valid 200 302 301 1h;

         proxy_cache_valid any 1m;

    wKioL1gUi8iBaewiAAFIXln-fuY210.png-wh_50

    

     7、proxy_cache_use_stale

         #指定缓存服务器与后端服务器无法通信时,

        何种情况下依旧使用过期的缓存内容来响应客户端

         proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 |

        http_502 | http_503 | http_504 | http_403 | http_404 | off …;

        

         Determines in which cases a stale cached response can be used when an error

        occurs during communication with the proxied server.

    

     8、proxy_cache_methods GET | HEAD | POST …;

    #定义允许使用缓存的请求方法

     If the client request method is listed in this directive then the response will be cached. “GET” and “HEAD” methods are     always added to the list, though it is recommended to specify them explicitly.

    

     9、proxy_hide_header field;

    #定义需要隐藏的响应报文首部

     By default, nginx does not pass the header fields “Date”, “Server”, “X-Pad”, and “X-Accel-…” from the response of     a proxied server to a client. The proxy_hide_header directive sets additional fields that will not be passed.

     一般nginx反向代理会配置很多站点,每个站点配置费时费力而且少有遗漏,主机信息还是会被泄露的。根据上面的说明,我们将        

                proxy_hide_header 配置在http区段

    注意: 部分header 信息无法用此方法,如关闭server信息,需要用此方式:

    Syntax:server_tokens on | off | string;

    Default:server_tokens on;

    Context:http, server, location;


     10、proxy_connect_timeout time;

    #代理服务器与后端服务器连接的超时时长

     Defines a timeout for establishing a connection with a proxied server. It should be

    noted that this timeout cannot usually exceed 75 seconds.

    

     默认为60s;,最大不超过75s

    

    ———————————————————————————————二 . buffer相关的配置;

 二. ngx_http_headers_module模块

     The ngx_http_headers_module module allows adding the “Expires” and “Cache-Control” header fields, and arbitrary fields,     to a response header.

    

     模块功能:向由代理服务器响应给客户端的响应报文添加自定义首部,或修改指定首部的值;

    

     1、add_header name value [always];

         添加自定义首部;

        

        eg:

         add_header X-Via  $server_addr;    $ 添加代理服务器地址

        

          wKioL1gUi8jBA4ygAAAdNGLifm0432.png-wh_50




        效果示例:  请求页面后, 在浏览器调试控制台中可看到代理服务器信息

        wKiom1gUi8qQ9JiCAABYtqKq8F0468.png-wh_50

         add_header X-Accel $server_name;

        

     2、expires [modified] time;

         expires epoch | max | off;

         给出的日期/时间后,被响应认为是过时。如Expires:Thu, 02 Apr 2009 05:14:08

        GMT需和Last-Modified结合使用。用于控制请求文件的有效时间,当请求数据在有效期

        内时客户端浏览器从缓存请求数据而不是服务器端

        .当缓存中数据失效或过期,才决定从服务器更新数据。

         用于定义Expire或Cache-Control首部的值;

         =========================================================


  三. ngx_http_upstream_module模块

     The ngx_http_upstream_module module is used to define groups of servers that can be referenced by the proxy_pass,     fastcgi_pass, uwsgi_pass, scgi_pass, and memcached_pass directives.

     模块功能: 用于实现后端服务器的负载均衡, 使用该模块来定义后端服务器组

    定义以后,需要在使用的地方进行调用,即可实现负载均衡;


     1、upstream name { … }

         # 定义后端服务器组,会引入一个新的上下文;Context: http

        

         eg:

             upstream httpd_srvs {

             server 192.168.0.1

            #注意: server 后面只需要添加地址即可!

             server  192.168.0.2

             …

             }

            

           

    wKioL1gUi8yg8gR6AAD2amVjZP0026.png-wh_50


     2、server address [parameters];

         在upstream上下文中server成员,以及相关的参数;

        Context: upstream

        

         address的表示格式:

         unix:/PATH/TO/SOME_SOCK_FILE

         IP[:PORT]

         HOSTNAME[:PORT]

        

         parameters:

         weight=number

         权重,默认为1;

         max_fails=number

         失败尝试最大次数;超出此处指定的次数时,server将被标记为不可用;

         fail_timeout=time

         设置将服务器标记为不可用状态的超时时长;

         max_conns

         当前的服务器的允许的最大并发连接数;

         backup

         将服务器标记为“备用”,即所有服务器均不可用时此服务器才启用;

         down

         标记为“不可用”;


         eg:

        upstream httpd_srvs {

         server 192.168.0.1 down ;

        #定义服务器为下线状态;

        server  192.168.0.2 backup;

        #定义为备用服务器

         server  192.168.0.3 weight 2 max_conns 100;

        #权重为2,最发并发为100

         }

    




     3、least_conn;

        Context: upstream

         最少连接调度算法,当server拥有不同的权重时其为wlc;

        

     4、 ip_hash;

        Context: upstream

         源地址hash调度方法;

         使用示例:

        wKiom1gUi83Q8ft4AABBV8OOnMo239.png-wh_50

     5、hash key [consistent];

        Context: upstream

        If the consistent parameter is specified the ketama consistent hashing

        method will be used instead.

        #[consistent]; 使用一致性哈希算法, 建议开启此项

         基于指定的key的hash表来实现对请求的调度,此处的key可以直接文本、

        变量或二者的组合;

        wKiom1gUi8-gpeNHAABkgGYEPOA489.png-wh_50

        作用:将请求分类,同一类请求将发往同一个upstream server;

    

    

         示例:

         hash $request_uri consistent;

         hash $remote_addr;

        

    

     6、keepalive connections;

        补充: 由于短连接消耗前端代理服务器的资源现象严重,因此会将一部分连接定义为

        长连接以节省资源

         #为每个worker进程保留的空闲的长连接数量;

         #定义nginx与后端服务器的保持连接的数量

    wKioL1gUi9CSQAaPAAApHgtgM_0009.png-wh_50

    ============================================================


  四 . ngx_stream_core_module模块

     模拟反代基于tcp或udp的服务连接,即工作于传输层的反代或调度器;

     #此模块可以定义非http服务的反代功能


     1、stream { … }

         定义stream相关的服务;

        Context:main

         #用法与upstream 类似


        eg: #反代ssh 服务

         stream {

         upstream sshsrvs {

         server 192.168.22.2:22;

         server 192.168.22.3:22;

         least_conn;

         }


        #定义服务器组

         server {

         listen 10.1.0.6:22022;

         proxy_pass sshsrvs;

        #调用sshsrvs服务器组

         }

         # 前端监听22022端口,并反代到后端服务器组的22端口

         }


     2、listen

     listen address:port [ssl] [udp] [proxy_protocol] [backlog=number] [bind] [ipv6only=on|off] [reuseport]     [so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]];

    

  

    

     思考:

     (1) 动态资源存储一组服务器、图片资源存在一组服务器、静态的文本类资源存储在一组服

    务器;如何分别调度?

     (2) 动态资源基于fastcgi或http协议(ap)?

     lnamp

原创文章,作者:ldt195175108,如若转载,请注明出处:http://www.178linux.com/55594

(4)
上一篇 2016-10-29 19:31
下一篇 2016-10-29 20:01

相关推荐

  • selinux与systemd

    selinux :service enhanced linux 安全增强的Linux  软件的安全性 选择安全系数较高的系统提高现有系统的安全性  计算机的安全等级D  最低的安全级别,提供最少的安全防护,系统访问无限制。DOS   C  访问控制的权限,能够实现可控的安全防护,个人账户管理,审计和…

    Linux干货 2017-05-22
  • 从新开始,坚持记录

    从新开始,从心开始,记录自己的学习,不逼自己一把,永远不知道自己的潜力。

    Linux干货 2016-12-05
  • Linux基础知识之逻辑卷管理器(LVM)

    逻辑卷管理器(LVM)        允许对卷进行方便操作的抽象层,包括重新设定文件系统的大小     允许在多个物理设备间重新组织文件系统将设备指定为物理卷     用一个或者多个物理卷来创建一个卷组  &n…

    Linux干货 2016-09-01
  • 系统基础之文件查找工具find

    文件查找:        在运维人员操作系统时,要接触大量的文件,为了避免忘记文件存放位置的尴尬,就需要我们有一种文件查找工具的帮忙,下面是两个文件查找工具的详解,locate以及find,分别分享给大家. 第一款工具: Locate locate – find files by name loc…

    Linux干货 2016-08-16
  • TCP连接的状态转移

    TCP是一个面向连接的传输层协议,因此不论哪一方需要传输数据,都需要在双方之间建立一条传输连接。 用TCP的三次握手与四次挥手来解释TCP的各个状态之间的会比较清晰。 一、TCP的三次握手: a)         单方主动发起连接: 1、  服务器端应用层的应用程序创建…

    2017-03-19
  • N25第四周博客作业

    第四周博客作业 1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。 2、编辑/etc/group文件,添加组hadoop。      3、手动编辑/etc/passwd文件新增一行,添加用户hadoop,其基本组ID为hadoop组的i…

    Linux干货 2016-12-19