80-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; 即可

示例2.

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

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

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

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;

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

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

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

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

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

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

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;

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;    $ 添加代理服务器地址

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

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

...

}

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调度方法;

使用示例:

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可以直接文本、

变量或二者的组合;

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

示例:

hash $request_uri consistent;

hash $remote_addr;

6、keepalive connections;

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

长连接以节省资源

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

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

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

四 . 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

时间: 2024-08-09 10:33:25

80-nginx_http_proxy,upstream,stream模块简析的相关文章

python中的random模块简析

在Python生成随机数用random模块,下面的文章是本人自己简单总结的ython生成随机数与random模块中最常用的几个函数的关系,希望对大家有所帮助. random.random()用于生成随机符点数,括号内没有参数:random.uniform(a,b)用于生成一个指定范围内的随机符点数,两个参数其中一个是上限,一个是下限;random.randint(a,b)用于生成一个指定范围内的整数.其中参数a是下限,参数b是上限;random.randrange(a,b)用于指定范围a--b内

借助LANMT构架,简析ngnix的使用

LNMP流程图 nginx PHP Mysql Nginx Fastcgi_pass <-FastCGI-> fastcgi-(php-fpm)<->wrapper Php 解析器 (Php.ini) <->mysql fastcgi-(php-fpm)<->wrapper fastcgi-(php-fpm)<->wrapper fastcgi-(php-fpm)<->wrapper fastcgi-(php-fpm)<->

Android -- MediaPlayer内部实现简析

Android -- MediaPlayer内部实现简析 在之前的博客中,已经介绍了使用MediaPlayer时要注意的内容.现在,这里就通过一个MediaPlayer代码实例,来进一步分析MediaPlayer内部是如何运作.实现的:当然这里的分析只截止到底层调用播放器之前,因为播放器这块实在是没搞懂. 我们使用的例子来源于之前MediaPlayer Playback译文中的官方实例: String url = "http://........"; // your URL here

Nginx基于TCP/UDP端口的四层负载均衡(stream模块)配置梳理

Nginx基于TCP/UDP端口的四层负载均衡(stream模块)配置梳理 通常我们会用Nginx的upstream做基于http/https端口的7层负载均衡,由于Nginx老版本不支持tcp协议,所以基于tcp/udp端口的四层负载均衡一般用LVS或Haproxy来做.至于4层负载均衡和7层负载均衡的区别,可以参考:http://www.cnblogs.com/kevingrace/p/6137881.html.然而Nginx从1.9.0版本开始,新增加了一个stream模块,用来实现四层协

web应用构架LAMT及tomcat负载简析

Httpd    (mod_jk.so) workers.properties文件 uriworkermap.properties文件 <--AJP1.3--> Tomcat  --> jdk 大致流程:apache服务器通过mod_jk.so 模块处理jsp文件的动态请求.通过tomcat worker等待执行servlet/JSP的tomcat实例.使用 AJP1.3协议与tomcat通信.tomcat有借助jdk解析. 负载就是 多台tomcat.共同解析apache发送的jsp请

RecycleView + CardView 控件简析

今天使用了V7包加入的RecycleView 和 CardView,写篇简析. 先上效果图: 原理图: 这是RecycleView的工作原理: 1.LayoutManager用来处理RecycleView的“列表”样式,Support包默认包含了:LinearLayoutManager  横向或纵向的滚动列表. GridLayoutManager  网格列表.StaggeredGridLayoutManager  交错的网格列表. 2.Adapter负责处理RecycleView的数据和样式 3

sed简析

写一个shell命令,统计apache日志文件(access_log)中某一天中每个URL的访问次数,并按照次数由小到大的顺序排序输出:# cat /application/nginx/logs/20170202_access_www.log |awk '{print $7}'|sort| uniq -c |sort -n awk  '{print $7}' 匹配到 url 记录即日志文件格式的($http_referer)字段sort  是排序 ,并且标准输出到屏幕uniq -c  统计重复的

简析 .NET Core 构成体系

简析 .NET Core 构成体系 Roslyn 编译器 RyuJIT 编译器 CoreCLR & CoreRT CoreFX(.NET Core Libraries) .NET Core 代码开发.部署.运行过程 总结 前文介绍了.NET Core 在整个.NET 平台所处的地位,以及与.NET Framework的关系(原文链接),本文将详细介绍.NET Core 框架的构成和各模块主要功能,以及如何实现跨平台. 上图描述了 .NET Core的系统构成,最上层是应用层,是开发基于UI应用的

Android RecycleView + CardView 控件简析

今天使用了V7包加入的RecycleView 和 CardView,写篇简析. 先上效果图: 原理图: 这是RecycleView的工作原理: 1.LayoutManager用来处理RecycleView的“列表”样式,Support包默认包含了:LinearLayoutManager  横向或纵向的滚动列表. GridLayoutManager  网格列表.StaggeredGridLayoutManager  交错的网格列表. 2.Adapter负责处理RecycleView的数据和样式 3