Nginx系列教程:HTTP Upstream模块

该模块为后端服务器提供简单的">负载均衡(轮循调度和客户端 IP)。

示例:

upstream backend { server backend1.example.com weight=5; server backend2.example.com:
8080; server unix:/tmp/backend3;} server { location / { proxy_pass http://backend; }}

指令

ip_hash

语法: ip_hash

默认值: 无

语境: upstream

This directive causes requests to be distributed between upstreams based on the IP-address of the client.
The key for the hash is the class-C network address of the client. This method guarantees that the client request will always be transferred to the same server. But if this server is considered inoperative, then the request of this client will be transferred to another server. This gives a high probability clients will always connect to the same server.

It is not possible to combine ip_hash and weight methods for connection distribution. If one of the servers must be removed for some time, you must mark that server as *down*.

示例:

upstream backend { ip_hash; server backend1.example.com; server backend2.example.com; server backend3.example.com down; server backend4.example.com;}

server

语法: server name [parameters]

默认值: 无

语境: upstream

Directive assigns the name and the parameters of server. For the name it is possible to use a domain name, an address, port or unix socket. If domain name resolves to several addresses, then all are used.

weight = NUMBER - set weight of the server, if not set weight is equal to one. max_fails = NUMBER - number of unsuccessful attempts at communicating with the server within the time period (assigned by parameter fail_timeout) after
which it is considered inoperative. If not set, the number of attempts is one. A value of 0 turns off this check. What is considered a failure is
defined by proxy_next_upstream or fastcgi_next_upstream (except http_404 errors which do not count towards max_fails). fail_timeout = TIME - the time during which must occur *max_fails* number of unsuccessful attempts at communication with the server that would cause the server to be considered inoperative, and also the time for which the server will be considered inoperative (before another attempt is made). If not set the time is 10 seconds. fail_timeout has nothing to do with upstream response time, use proxy_connect_timeout and proxy_read_timeout for controlling this. down - marks server as permanently offline, to be used with the directive ip_hash. backup - (0.6.7 or later) only uses this server if the non-backup servers are all down or busy

示例:

upstream backend { server backend1.example.com weight=5; server 127.0.0.1:8080 max_fails=3 fail_timeout=30s; server unix:/tmp/backend3;}

upstream

语法: upstream name { ... }

默认值: 无

语境: http

这个指令描述了一个服务器的集合,该集合可被用于 proxy_pass 和 fastcgi_pass 指令中,作为一个单独的实体。

这些服务器可以是监听在不同的端口,另外,并发使用同时监听 TCP 端口和 Unix 套接字的服务器是可能的。

这些服务器能被分配不同的权重。如果没有指定,则都为一。

示例:

upstream backend { server backend1.example.com weight=5; server 127.0.0.1:8080 max_fails=3 fail_timeout=30s; server unix:/tmp/backend3;}

Requests are distributed according to the servers in round-robin manner with respect of the server weight.
For example of every 7 seven requests given above they will be distributed like this: 5 requests on backend1.example.com and one request to the second and the third of server. If with an attempt at the work with the server error occurred, then the request will be transmitted to the following server and then until all workers of server not are tested. If successful answer is not succeeded in obtaining from all servers, then to client will be returned the result of work with the last server.

变量

自版本 0.5.18 之后,可通过 log_module 模块将变量值写入日志。

示例:

log_format timing '$remote_addr - $remote_
user [$time_local] $request ' 'upstream_response_time $upstream_response_time ' 'msec $msec request_time $request_time'; log_format up_head '$remote_addr - $remote_user [$time_local] $request ' 'upstream_http_content_type $upstream_http_content_type';

$upstream_addr

Address of the upstream server that handled the request

$upstream_cache_status

Appeared in 0.8.3. Possible values:

MISS EXPIRED - expired, request was passed to backend UPDATING - expired, stale response was used due to proxy/fastcgi_cache_use_stale updating STALE - expired, stale response was used due to proxy/fastcgi_cache_use_stale HIT

$upstream_status

Upstream server status of the answer

$upstream_response_time

Response time in milliseconds. Several answers are divided by commas and colons.

$upstream_http_$HEADER

Arbitrary HTTP protocol headers, for example:

$upstream_http_host

时间: 2024-10-07 08:37:34

Nginx系列教程:HTTP Upstream模块的相关文章

Nginx系列教程:nginx_module_development模块开发

好像这是唯一的一份模块开发文档.. 以下简称 guide 读代码!! nginx 程序启动 把 nginx.conf 读入内存 处理模块 ngx_http_module_t 定义的 configuration. 应该是如下的执行顺序 pre create_main_conf 执行 ngx_command_t 里面定义的函数. 可以视为 init_main_conf 的自动处理部分 init_main_conf ... postconfig listen fork 子进程(真正的服务器进程 mas

Nginx系列教程:nginx_substitutions_filter模块

nginx_substitutions_filter 请注意:此模块不是Nginx源的分布,可点击此链接找到安装说明, http://wiki.nginx.org/NginxHttpSubsModule#Installation . 概述 nginx_substitutions_filter 是一个过滤器模块,它可以在响应主体上运行正则表达式和固定字符串替换.该 模块不同于Nginx的本地替代模块.它能够扫描输出链缓冲区和匹配逐行字符串,类似于http://www.aliyun.com/zixu

Nginx系列教程:ngx_cache_purge模块

ngx_cache_purge 本模块由第三方提供,不包含在 Nginx 的源码发布版中. 概述 ngx_cache_purge是nginx模块,用于从FastCGI.proxy.SCGI 和uWSGI5603.html">缓存中增加内容清除功能. 安装 下载模块源码:ngx_cache_purge-1.2(更新记录)(SHA1: d9468cf42432e81ea3a110ec63aae2eb273f5516) 其他版本 解压,然后编译: ./configuremake &&am

Nginx系列教程:事件模块

设置Nginx处理连接请求 指令 accept_mutex Syntax: accept_mutex [ on | off ] Default: on nginx 使用连接互斥锁进行顺序的accept()系统调用. accept_mutex_delay Syntax: accept_mutex_delay Nms; Default: 500ms 如果一个进程没有互斥锁,它将延迟至少多长时间.默认情况下,延迟是500ms . debug_connection Syntax: debug_conne

Nginx系列教程:HTTP模块

控制Nginx HTTP 进程的核心属性. 指令 alias 语法: alias file-path|directory-path; 默认值: no 作用域: location 该指令设置指定location使用的路径.注意它跟 root 相似,但是不改变文件的根路径,仅仅是使用文件系统路径.比如: location&http://www.aliyun.com/zixun/aggregation/37954.html">nbsp; /i/ {  alias  /spool/w3/i

Nginx系列教程:NginxHttpOwnerMatch模块(解决链接型文件跨站访问)

概述 该模块提供了一个简单文件基于所有者的http://www.aliyun.com/zixun/aggregation/38609.html">访问控制. nginx_http_owner_match_module模块使得它可以控制具体文件的所有者和组织的访问. 访问规则检查是根据其声明的顺序. 配置范例: location / {  omallow heiher;  # allow access files of heiher  omallow jack sftp; # allow a

Nginx系列教程:HTTP Secure Link模块

此模块计算和检查要求所需的安全链接网址.此模块默认情况下不会被编译启用,要启用它请使用指令 --with-http_secure_link_module 参与编译安装 nginx .注意:此模块仅在 nginx 版本为0.7.18或更高时才被支持. 配置实例: location /p/ { location ~ ^/p/(?<secure>[\w\-=]+,\d+)(?<file>/.+)$ {  secure_link $secure; secure_link_md5 $secu

Nginx系列教程:LNMP多用户动态进程管理虚拟主机方案

A. 特点1. 高效.内存使用少.2. 权限分离,用户间互不干扰.3. 动态进程管理,资源分配均衡. B. 应用程序说明Nginx:事件驱动的 Web 服务器,采用模块化设计,小巧.高效.PHP-FPM:支持快速进程管理的 PHP FastCGI 接口版本,用它实现动态进程管理,提高资源使用效率. C. 整个架构的简单说明Nginx 处理所有的 Web 请求,它将 PHP 的请求 Match 出,发送给上游服务器处理,这里的上游服务器就是 PHP-CGI.PHP-CGI 工作在 FastCGI

Nginx系列教程:LNMP多用户虚拟主机方案

A. 特点1. 高效.内存使用少.2. 权限分离,用户间互不干扰. B. 应用程序说明Nginx : 事件驱动的 Web 服务器,采用模块化设计,小巧.高效.PHP-CGI : PHP的CGI接口版本(本文使用FastCGI高效接口). C. 整个架构的简单说明Nginx 处理所有的 Web 请求,它将 PHP 的请求 Match 出,发送给上游服务器处理,这里的上游服务器就是 PHP-CGI.PHP-CGI 工作在 FastCGI 模式,它侦听着一个地址端口(或 Unix socket文件,建

Nginx系列教程:HTTP Upstream Request Hash模块

ngx_http_upstream_hash_module 本模块由第三方提供,不包含在 Nginx 的源码发布版中. upstream_hash该模块提供了简单的上游负载分配,通过散列一个可配置的变量(例如,请求URI,传入的HTTP标头或一些组合).用法示例如下: upstream backend {: server server1;: server server2;: hash $request_uri;} 在这里,nginx将通过散列请求的URI($ REQUEST_URI)选择Serv