Nginx4大模块——proxy、headers、upstream、stream模块

Nginx

应用程序发布:

灰度模型:

         (1) 如果存在用户会话;

             从服务器上拆除会话;

         (2) 新版本应用程序存在bug;

             回滚;

ngx_http_proxy_module模块:

1、proxy_pass URL;

Context: location, if in location, limit_except

    

注意:proxy_pass后面的路径不带uri时,其会将location的uri传递给后端主机

         server {

                

                 server_name HOSTNAME;

                 location /uri/ {

                      proxy http://hos[:port];

                 }

                

         }

        

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

    

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

         server {

                

                 server_name HOSTNAME;

                 location /uri/ {

                      proxy http://host/new_uri/;

                 }

                

         }

        

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

    

如果location定义其uri时使用了正则表达式的模式,则proxy_pass之后必须不能使用uri; 用户请求时传递的uri将直接附加代理到的服务的之后

         server {

                

                 server_name HOSTNAME;

                 location ~|~* /uri/ {

                      proxy http://host;

                 }

                

         }

        

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

2、proxy_set_header field value;

设定发往后端主机的请求报文的请求首部的值;Context: http, server, location

    

proxy_set_header X-Real-IP  $remote_addr;

    $remote_addr:记录的是上一台主机的IP,而上一台主机有可能也是代理服务器

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    $proxy_add_x_forwarded_for:记录的是源IP地址

    

在http客户端还有修改/etc/httpd/conf/httpd.conf文件

    LogFormat "%{X-Real-IP}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

    

    通过上述方法则可以在后端主机上记录真实的httpd资源请求者,而不再是只记录前端代理服务器的IP地址

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];

    

proxy_cache_path /var/cache/nginx/proxy_cache levels=1:2:1 keys_zone=gmtest:20M max_size=1G;

4、proxy_cache zone | off;

指明要调用的缓存,或关闭缓存机制;Context: http, server, location

    

proxy_cache gmtest;

5、 proxy_cache_key string;

缓存中用于“键”的内容;

    

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

    建议定义成方法和url

6、proxy_cache_valid [code …] time;

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

    

定义在http{…}中;

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

    

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

    proxy_cache gmtest;

    proxy_cache_key $request_uri;

    proxy_cache_valid 200 302 301 1h;

    proxy_cache_valid any 1m;

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.

    

默认方法就是GET HEAD方法

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.

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

11、buffer相关的配置

a:proxy_buffer_size size;

Sets the size of the buffer used for reading the first part of the response received from the proxied server. This part usually contains a small response header. By default, the buffer size is equal to one memory page.

    

默认为4k|8k

b:proxy_buffering on | off;

Enables or disables buffering of responses from the proxied server.

    

默认为on

c:proxy_buffers number size;

Sets the number and size of the buffers used for reading a response from the proxied server, for a single connection. By default, the buffer size is equal to one memory page.

    

默认为8 4k|8k

d:proxy_busy_buffers_size size;

When buffering of responses from the proxied server is enabled, limits the total size of buffers that can be busy sending a response to the client while the response is not yet fully read.

    

默认为8k|16k

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];

添加自定义首部;

    

add_header X-Via  $server_addr;

    经由的代理服务器地址

add_header X-Accel $server_name;

2、expires [modified] time;

expires epoch | max | off;

    

用于定义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

    

upstream httpdsrvs {

         server …

         server…

        

}

2、server address [parameters];

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

    

address的表示格式:

    unix:/PATH/TO/SOME_SOCK_FILE

    IP[:PORT]

    HOSTNAME[:PORT]

    

parameters:

         weight=number

             权重,默认为1;默认算法是wrr

         max_fails=number

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

         fail_timeout=time

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

         max_conns

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

         backup

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

         down

             标记为“不可用”

             先在nginx前端配置down,然后在下架后端服务器,上架新的web程序,然后上架,在修改配置文件立马的down

3、least_conn;

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

要在后端服务器是长连接时,效果才好,比如mysql

4、ip_hash;

源地址hash调度方法

5、hash key [consistent];

基于指定的key的hash表来实现对请求的调度,此处的key可以直接文本、变量或二者的组合

    

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

    

If the consistent parameter is specified the ketama consistent hashing method will be used instead.

    

示例:

    hash $request_uri consistent;

    hash $remote_addr;

    hash $cookie_name; 对同一浏览器的请求,发往同一个upstream server

6、keepalive connections;

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

    

nginx的其它的二次发行版:

    tengine

    OpenResty

    

1.9版本之后可以反代tcp/udp的协议,基于stream模块,工作与传输层

ngx_stream_core_module模块

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

1、stream { … }

定义stream相关的服务;Context: main

    

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;

         }

}

    

stream模块中管的upstream模块的用法同上

2、listen

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

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

(26)
megedugaomegedugao
上一篇 2016-10-29 10:08
下一篇 2016-10-29 10:47

相关推荐

  • N24_阿龙弟弟 学习计划/目标/宣言

    嗨,大家好,这是我的第一篇文章。很高兴来到马帮门徒这个大家庭。 学习计划:没有自己的计划,跟着马哥课程的进度来吧,以周为单位,确保每周任务完成; 学习目标:掌握应有的运维能力,做一名合格的Linux运维工程师,提升自我价值,过更好的生活; 学习宣言:Be a better man(Not Only Linux)!

    Linux干货 2016-10-25
  • 文本处理三剑客–sed使用详解

    处理文本的工具sed Stream EDitor     行编辑器 sed是一种流编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space ),接着用sed 命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。…

    Linux干货 2017-05-21
  • 了解sed

    本博客分为四个部分:sed介绍、sed用法、sed高级用法(简略带过)、相关例题。通过本文可以大致了解sed命令。深度可以当成是课前预习吧。 1、sed介绍Stream EDitor, 行编辑器 sed是一种流编辑器,它一次处理一行内容。处理时,把 当前处理的行存储在临时缓冲区中,称为“模式空间”( pattern space),接着用sed命令处理缓冲区中…

    Linux干货 2017-04-25
  • 三次握手和四次挥手

    今天来聊一下事实标准协议TCP/IP中传输层里TCP协议中,主机与服务器建立连接时的三次握手,和断开连接时的四次挥手。 本博文分两部分介绍,    一:状态详解    二:三次握手和四次挥手状态介绍 这里总共涉及到十种状态,其实总共有十一种状态,接下来分别介绍一下它们; 一:状态详解 CLOSED:关闭—&…

    2017-09-01
  • N25-第四周作业

    第四周 1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限。 [root@zf ~]# cp -r /etc/skel/ /home/tuser1 [root@zf ~]# chmod -R&nbs…

    Linux干货 2016-12-21
  • Python from entry to abandon 3

        第十章的内容是解决问题————编写一个Python脚本。在我的电脑上因为Zip命令不能正常工作所以无法给出演示。该章给出了很有意义的编程思路,对以后学习和工作都有不错的参考意义,这部分有兴趣的同学还是自己去看原版教程吧。    这篇博客结合个人笔记整理了《简明Python教程》第十一章到第十…

    Linux干货 2017-04-07