nginx upstream模块对于nginx 最为代理服务器提供了基础功能 结合upstream功能 nginx能对代理进行各种处理
nginx upstream基础数据结构
typedef struct {
ngx_uint_t status; //http状态码
ngx_msec_t response_time; //上游服务器响应时间
ngx_msec_t connect_time; //成功连接上游服务器时间
ngx_msec_t header_time; //头部时间耗费 处理完成上游服务器返回头部的时间和响应时间差
off_t response_length; //http包体和头部总长度
off_t bytes_received; //upstream接收到的字节数
ngx_str_t *peer; //上游服务器地址(ip或名称)
} ngx_http_upstream_state_t;
state结构体中的成员可以绑定到nginx http变量中 从而可以在nginx access中打印出请求的对应上述变量信息
upstream main作用域可配置信息
typedef struct {
ngx_hash_t headers_in_hash; //其他模块配置的hash头部信息ngx_array_t upstreams; //可配置多个upstream
} ngx_http_upstream_main_conf_t;
upstream server结构
typedef struct {
ngx_str_t name; //server名称ngx_addr_t *addrs; //server地址
ngx_uint_t naddrs; //地址数量
ngx_uint_t weight; //server对应的权重
ngx_uint_t max_conns; //指定server最大连接数
ngx_uint_t max_fails; //允许最多连接失败次数
time_t fail_timeout; //连接超时时间
ngx_msec_t slow_start; //未使用
ngx_uint_t down; //踢出
unsigned backup:1; //作为备用
NGX_COMPAT_BEGIN(6)
NGX_COMPAT_END
} ngx_http_upstream_server_t;
upstream peer结构
typedef struct {
ngx_http_upstream_init_pt init_upstream; //peer配置初始化
ngx_http_upstream_init_peer_pt init; //peer初始化
void *data; //传入的数据地址
} ngx_http_upstream_peer_t;
nginx server作用域配置结构
struct ngx_http_upstream_srv_conf_s {
ngx_http_upstream_peer_t peer; //upstream peer
void **srv_conf;
ngx_array_t *servers;
ngx_uint_t flags; //可支持的标志ngx_str_t host; //server主机地址
u_char *file_name; //conf配置文件名
ngx_uint_t line; //所在配置文件里行数
in_port_t port; //端口号
ngx_uint_t no_port; //没有端口号
#if (NGX_HTTP_UPSTREAM_ZONE)
ngx_shm_zone_t *shm_zone; // upstream共享内存
#endif};
upstream local 上游服务器解析绑定到本机地址
typedef struct {
ngx_addr_t *addr;
ngx_http_complex_value_t *value;
#if (NGX_HAVE_TRANSPARENT_PROXY)
ngx_uint_t transparent; /* unsigned transparent:1; */
#endif
} ngx_http_upstream_local_t