Mercurial > hg > nginx-quic
annotate src/stream/ngx_stream_upstream.c @ 6682:db422604ceb0
Stream: allow using the session context inside handlers.
Previously, it was not possible to use the stream context
inside ngx_stream_init_connection() handlers. Now, limit_conn,
access handlers, as well as those added later, can create
their own contexts.
author | Dmitry Volyntsev <xeioex@nginx.com> |
---|---|
date | Tue, 06 Sep 2016 21:28:17 +0300 |
parents | c02290241cbe |
children | 9cf2dce316e5 |
rev | line source |
---|---|
6115 | 1 |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4 * Copyright (C) Nginx, Inc. | |
5 */ | |
6 | |
7 | |
8 #include <ngx_config.h> | |
9 #include <ngx_core.h> | |
10 #include <ngx_stream.h> | |
11 | |
12 | |
6675
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
13 static ngx_int_t ngx_stream_upstream_add_variables(ngx_conf_t *cf); |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
14 static ngx_int_t ngx_stream_upstream_addr_variable(ngx_stream_session_t *s, |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
15 ngx_stream_variable_value_t *v, uintptr_t data); |
6677
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
16 static ngx_int_t ngx_stream_upstream_response_time_variable( |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
17 ngx_stream_session_t *s, ngx_stream_variable_value_t *v, uintptr_t data); |
6676
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
18 static ngx_int_t ngx_stream_upstream_bytes_variable(ngx_stream_session_t *s, |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
19 ngx_stream_variable_value_t *v, uintptr_t data); |
6675
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
20 |
6115 | 21 static char *ngx_stream_upstream(ngx_conf_t *cf, ngx_command_t *cmd, |
22 void *dummy); | |
23 static char *ngx_stream_upstream_server(ngx_conf_t *cf, ngx_command_t *cmd, | |
24 void *conf); | |
25 static void *ngx_stream_upstream_create_main_conf(ngx_conf_t *cf); | |
26 static char *ngx_stream_upstream_init_main_conf(ngx_conf_t *cf, void *conf); | |
27 | |
28 | |
29 static ngx_command_t ngx_stream_upstream_commands[] = { | |
30 | |
31 { ngx_string("upstream"), | |
32 NGX_STREAM_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE1, | |
33 ngx_stream_upstream, | |
34 0, | |
35 0, | |
36 NULL }, | |
37 | |
38 { ngx_string("server"), | |
39 NGX_STREAM_UPS_CONF|NGX_CONF_1MORE, | |
40 ngx_stream_upstream_server, | |
41 NGX_STREAM_SRV_CONF_OFFSET, | |
42 0, | |
43 NULL }, | |
44 | |
45 ngx_null_command | |
46 }; | |
47 | |
48 | |
49 static ngx_stream_module_t ngx_stream_upstream_module_ctx = { | |
6675
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
50 ngx_stream_upstream_add_variables, /* preconfiguration */ |
6174
68c106e6fa0a
Stream: added postconfiguration method to stream modules.
Vladimir Homutov <vl@nginx.com>
parents:
6115
diff
changeset
|
51 NULL, /* postconfiguration */ |
68c106e6fa0a
Stream: added postconfiguration method to stream modules.
Vladimir Homutov <vl@nginx.com>
parents:
6115
diff
changeset
|
52 |
6115 | 53 ngx_stream_upstream_create_main_conf, /* create main configuration */ |
54 ngx_stream_upstream_init_main_conf, /* init main configuration */ | |
55 | |
56 NULL, /* create server configuration */ | |
6629 | 57 NULL /* merge server configuration */ |
6115 | 58 }; |
59 | |
60 | |
61 ngx_module_t ngx_stream_upstream_module = { | |
62 NGX_MODULE_V1, | |
63 &ngx_stream_upstream_module_ctx, /* module context */ | |
64 ngx_stream_upstream_commands, /* module directives */ | |
65 NGX_STREAM_MODULE, /* module type */ | |
66 NULL, /* init master */ | |
67 NULL, /* init module */ | |
68 NULL, /* init process */ | |
69 NULL, /* init thread */ | |
70 NULL, /* exit thread */ | |
71 NULL, /* exit process */ | |
72 NULL, /* exit master */ | |
73 NGX_MODULE_V1_PADDING | |
74 }; | |
75 | |
76 | |
6675
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
77 static ngx_stream_variable_t ngx_stream_upstream_vars[] = { |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
78 |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
79 { ngx_string("upstream_addr"), NULL, |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
80 ngx_stream_upstream_addr_variable, 0, |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
81 NGX_STREAM_VAR_NOCACHEABLE, 0 }, |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
82 |
6676
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
83 { ngx_string("upstream_bytes_sent"), NULL, |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
84 ngx_stream_upstream_bytes_variable, 0, |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
85 NGX_STREAM_VAR_NOCACHEABLE, 0 }, |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
86 |
6677
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
87 { ngx_string("upstream_connect_time"), NULL, |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
88 ngx_stream_upstream_response_time_variable, 2, |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
89 NGX_STREAM_VAR_NOCACHEABLE, 0 }, |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
90 |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
91 { ngx_string("upstream_first_byte_time"), NULL, |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
92 ngx_stream_upstream_response_time_variable, 1, |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
93 NGX_STREAM_VAR_NOCACHEABLE, 0 }, |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
94 |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
95 { ngx_string("upstream_session_time"), NULL, |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
96 ngx_stream_upstream_response_time_variable, 0, |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
97 NGX_STREAM_VAR_NOCACHEABLE, 0 }, |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
98 |
6676
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
99 { ngx_string("upstream_bytes_received"), NULL, |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
100 ngx_stream_upstream_bytes_variable, 1, |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
101 NGX_STREAM_VAR_NOCACHEABLE, 0 }, |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
102 |
6675
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
103 { ngx_null_string, NULL, NULL, 0, 0, 0 } |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
104 }; |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
105 |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
106 |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
107 static ngx_int_t |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
108 ngx_stream_upstream_add_variables(ngx_conf_t *cf) |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
109 { |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
110 ngx_stream_variable_t *var, *v; |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
111 |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
112 for (v = ngx_stream_upstream_vars; v->name.len; v++) { |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
113 var = ngx_stream_add_variable(cf, &v->name, v->flags); |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
114 if (var == NULL) { |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
115 return NGX_ERROR; |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
116 } |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
117 |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
118 var->get_handler = v->get_handler; |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
119 var->data = v->data; |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
120 } |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
121 |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
122 return NGX_OK; |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
123 } |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
124 |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
125 |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
126 static ngx_int_t |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
127 ngx_stream_upstream_addr_variable(ngx_stream_session_t *s, |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
128 ngx_stream_variable_value_t *v, uintptr_t data) |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
129 { |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
130 u_char *p; |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
131 size_t len; |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
132 ngx_uint_t i; |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
133 ngx_stream_upstream_state_t *state; |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
134 |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
135 v->valid = 1; |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
136 v->no_cacheable = 0; |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
137 v->not_found = 0; |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
138 |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
139 if (s->upstream_states == NULL || s->upstream_states->nelts == 0) { |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
140 v->not_found = 1; |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
141 return NGX_OK; |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
142 } |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
143 |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
144 len = 0; |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
145 state = s->upstream_states->elts; |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
146 |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
147 for (i = 0; i < s->upstream_states->nelts; i++) { |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
148 if (state[i].peer) { |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
149 len += state[i].peer->len; |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
150 } |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
151 |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
152 len += 2; |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
153 } |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
154 |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
155 p = ngx_pnalloc(s->connection->pool, len); |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
156 if (p == NULL) { |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
157 return NGX_ERROR; |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
158 } |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
159 |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
160 v->data = p; |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
161 |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
162 i = 0; |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
163 |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
164 for ( ;; ) { |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
165 if (state[i].peer) { |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
166 p = ngx_cpymem(p, state[i].peer->data, state[i].peer->len); |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
167 } |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
168 |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
169 if (++i == s->upstream_states->nelts) { |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
170 break; |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
171 } |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
172 |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
173 *p++ = ','; |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
174 *p++ = ' '; |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
175 } |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
176 |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
177 v->len = p - v->data; |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
178 |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
179 return NGX_OK; |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
180 } |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
181 |
ab9b4fd8c5b7
Stream: the $upstream_addr variable.
Vladimir Homutov <vl@nginx.com>
parents:
6629
diff
changeset
|
182 |
6676
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
183 static ngx_int_t |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
184 ngx_stream_upstream_bytes_variable(ngx_stream_session_t *s, |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
185 ngx_stream_variable_value_t *v, uintptr_t data) |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
186 { |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
187 u_char *p; |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
188 size_t len; |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
189 ngx_uint_t i; |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
190 ngx_stream_upstream_state_t *state; |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
191 |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
192 v->valid = 1; |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
193 v->no_cacheable = 0; |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
194 v->not_found = 0; |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
195 |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
196 if (s->upstream_states == NULL || s->upstream_states->nelts == 0) { |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
197 v->not_found = 1; |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
198 return NGX_OK; |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
199 } |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
200 |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
201 len = s->upstream_states->nelts * (NGX_OFF_T_LEN + 2); |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
202 |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
203 p = ngx_pnalloc(s->connection->pool, len); |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
204 if (p == NULL) { |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
205 return NGX_ERROR; |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
206 } |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
207 |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
208 v->data = p; |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
209 |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
210 i = 0; |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
211 state = s->upstream_states->elts; |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
212 |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
213 for ( ;; ) { |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
214 |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
215 if (data == 1) { |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
216 p = ngx_sprintf(p, "%O", state[i].bytes_received); |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
217 |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
218 } else { |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
219 p = ngx_sprintf(p, "%O", state[i].bytes_sent); |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
220 } |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
221 |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
222 if (++i == s->upstream_states->nelts) { |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
223 break; |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
224 } |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
225 |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
226 *p++ = ','; |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
227 *p++ = ' '; |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
228 } |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
229 |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
230 v->len = p - v->data; |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
231 |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
232 return NGX_OK; |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
233 } |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
234 |
df3a7c029dec
Stream: $upstream_bytes_sent and $upstream_bytes_received.
Vladimir Homutov <vl@nginx.com>
parents:
6675
diff
changeset
|
235 |
6677
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
236 static ngx_int_t |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
237 ngx_stream_upstream_response_time_variable(ngx_stream_session_t *s, |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
238 ngx_stream_variable_value_t *v, uintptr_t data) |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
239 { |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
240 u_char *p; |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
241 size_t len; |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
242 ngx_uint_t i; |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
243 ngx_msec_int_t ms; |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
244 ngx_stream_upstream_state_t *state; |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
245 |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
246 v->valid = 1; |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
247 v->no_cacheable = 0; |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
248 v->not_found = 0; |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
249 |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
250 if (s->upstream_states == NULL || s->upstream_states->nelts == 0) { |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
251 v->not_found = 1; |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
252 return NGX_OK; |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
253 } |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
254 |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
255 len = s->upstream_states->nelts * (NGX_TIME_T_LEN + 4 + 2); |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
256 |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
257 p = ngx_pnalloc(s->connection->pool, len); |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
258 if (p == NULL) { |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
259 return NGX_ERROR; |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
260 } |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
261 |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
262 v->data = p; |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
263 |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
264 i = 0; |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
265 state = s->upstream_states->elts; |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
266 |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
267 for ( ;; ) { |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
268 |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
269 if (data == 1) { |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
270 if (state[i].first_byte_time == (ngx_msec_t) -1) { |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
271 *p++ = '-'; |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
272 goto next; |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
273 } |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
274 |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
275 ms = state[i].first_byte_time; |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
276 |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
277 } else if (data == 2 && state[i].connect_time != (ngx_msec_t) -1) { |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
278 ms = state[i].connect_time; |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
279 |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
280 } else { |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
281 ms = state[i].response_time; |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
282 } |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
283 |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
284 ms = ngx_max(ms, 0); |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
285 p = ngx_sprintf(p, "%T.%03M", (time_t) ms / 1000, ms % 1000); |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
286 |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
287 next: |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
288 |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
289 if (++i == s->upstream_states->nelts) { |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
290 break; |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
291 } |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
292 |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
293 *p++ = ','; |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
294 *p++ = ' '; |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
295 } |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
296 |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
297 v->len = p - v->data; |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
298 |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
299 return NGX_OK; |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
300 } |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
301 |
c02290241cbe
Stream: upstream response time variables.
Vladimir Homutov <vl@nginx.com>
parents:
6676
diff
changeset
|
302 |
6115 | 303 static char * |
304 ngx_stream_upstream(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy) | |
305 { | |
306 char *rv; | |
307 void *mconf; | |
308 ngx_str_t *value; | |
309 ngx_url_t u; | |
310 ngx_uint_t m; | |
311 ngx_conf_t pcf; | |
312 ngx_stream_module_t *module; | |
313 ngx_stream_conf_ctx_t *ctx, *stream_ctx; | |
314 ngx_stream_upstream_srv_conf_t *uscf; | |
315 | |
316 ngx_memzero(&u, sizeof(ngx_url_t)); | |
317 | |
318 value = cf->args->elts; | |
319 u.host = value[1]; | |
320 u.no_resolve = 1; | |
321 u.no_port = 1; | |
322 | |
323 uscf = ngx_stream_upstream_add(cf, &u, NGX_STREAM_UPSTREAM_CREATE | |
324 |NGX_STREAM_UPSTREAM_WEIGHT | |
325 |NGX_STREAM_UPSTREAM_MAX_FAILS | |
326 |NGX_STREAM_UPSTREAM_FAIL_TIMEOUT | |
327 |NGX_STREAM_UPSTREAM_DOWN | |
328 |NGX_STREAM_UPSTREAM_BACKUP); | |
329 if (uscf == NULL) { | |
330 return NGX_CONF_ERROR; | |
331 } | |
332 | |
333 | |
334 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_stream_conf_ctx_t)); | |
335 if (ctx == NULL) { | |
336 return NGX_CONF_ERROR; | |
337 } | |
338 | |
339 stream_ctx = cf->ctx; | |
340 ctx->main_conf = stream_ctx->main_conf; | |
341 | |
342 /* the upstream{}'s srv_conf */ | |
343 | |
344 ctx->srv_conf = ngx_pcalloc(cf->pool, | |
345 sizeof(void *) * ngx_stream_max_module); | |
346 if (ctx->srv_conf == NULL) { | |
347 return NGX_CONF_ERROR; | |
348 } | |
349 | |
350 ctx->srv_conf[ngx_stream_upstream_module.ctx_index] = uscf; | |
351 | |
352 uscf->srv_conf = ctx->srv_conf; | |
353 | |
6379
cf5e822cf470
Dynamic modules: changed ngx_modules to cycle->modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6174
diff
changeset
|
354 for (m = 0; cf->cycle->modules[m]; m++) { |
cf5e822cf470
Dynamic modules: changed ngx_modules to cycle->modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6174
diff
changeset
|
355 if (cf->cycle->modules[m]->type != NGX_STREAM_MODULE) { |
6115 | 356 continue; |
357 } | |
358 | |
6379
cf5e822cf470
Dynamic modules: changed ngx_modules to cycle->modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6174
diff
changeset
|
359 module = cf->cycle->modules[m]->ctx; |
6115 | 360 |
361 if (module->create_srv_conf) { | |
362 mconf = module->create_srv_conf(cf); | |
363 if (mconf == NULL) { | |
364 return NGX_CONF_ERROR; | |
365 } | |
366 | |
6379
cf5e822cf470
Dynamic modules: changed ngx_modules to cycle->modules.
Maxim Dounin <mdounin@mdounin.ru>
parents:
6174
diff
changeset
|
367 ctx->srv_conf[cf->cycle->modules[m]->ctx_index] = mconf; |
6115 | 368 } |
369 } | |
370 | |
371 uscf->servers = ngx_array_create(cf->pool, 4, | |
372 sizeof(ngx_stream_upstream_server_t)); | |
373 if (uscf->servers == NULL) { | |
374 return NGX_CONF_ERROR; | |
375 } | |
376 | |
377 | |
378 /* parse inside upstream{} */ | |
379 | |
380 pcf = *cf; | |
381 cf->ctx = ctx; | |
382 cf->cmd_type = NGX_STREAM_UPS_CONF; | |
383 | |
384 rv = ngx_conf_parse(cf, NULL); | |
385 | |
386 *cf = pcf; | |
387 | |
388 if (rv != NGX_CONF_OK) { | |
389 return rv; | |
390 } | |
391 | |
392 if (uscf->servers->nelts == 0) { | |
393 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
394 "no servers are inside upstream"); | |
395 return NGX_CONF_ERROR; | |
396 } | |
397 | |
398 return rv; | |
399 } | |
400 | |
401 | |
402 static char * | |
403 ngx_stream_upstream_server(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
404 { | |
405 ngx_stream_upstream_srv_conf_t *uscf = conf; | |
406 | |
407 time_t fail_timeout; | |
408 ngx_str_t *value, s; | |
409 ngx_url_t u; | |
410 ngx_int_t weight, max_fails; | |
411 ngx_uint_t i; | |
412 ngx_stream_upstream_server_t *us; | |
413 | |
414 us = ngx_array_push(uscf->servers); | |
415 if (us == NULL) { | |
416 return NGX_CONF_ERROR; | |
417 } | |
418 | |
419 ngx_memzero(us, sizeof(ngx_stream_upstream_server_t)); | |
420 | |
421 value = cf->args->elts; | |
422 | |
423 weight = 1; | |
424 max_fails = 1; | |
425 fail_timeout = 10; | |
426 | |
427 for (i = 2; i < cf->args->nelts; i++) { | |
428 | |
429 if (ngx_strncmp(value[i].data, "weight=", 7) == 0) { | |
430 | |
431 if (!(uscf->flags & NGX_STREAM_UPSTREAM_WEIGHT)) { | |
432 goto not_supported; | |
433 } | |
434 | |
435 weight = ngx_atoi(&value[i].data[7], value[i].len - 7); | |
436 | |
437 if (weight == NGX_ERROR || weight == 0) { | |
438 goto invalid; | |
439 } | |
440 | |
441 continue; | |
442 } | |
443 | |
444 if (ngx_strncmp(value[i].data, "max_fails=", 10) == 0) { | |
445 | |
446 if (!(uscf->flags & NGX_STREAM_UPSTREAM_MAX_FAILS)) { | |
447 goto not_supported; | |
448 } | |
449 | |
450 max_fails = ngx_atoi(&value[i].data[10], value[i].len - 10); | |
451 | |
452 if (max_fails == NGX_ERROR) { | |
453 goto invalid; | |
454 } | |
455 | |
456 continue; | |
457 } | |
458 | |
459 if (ngx_strncmp(value[i].data, "fail_timeout=", 13) == 0) { | |
460 | |
461 if (!(uscf->flags & NGX_STREAM_UPSTREAM_FAIL_TIMEOUT)) { | |
462 goto not_supported; | |
463 } | |
464 | |
465 s.len = value[i].len - 13; | |
466 s.data = &value[i].data[13]; | |
467 | |
468 fail_timeout = ngx_parse_time(&s, 1); | |
469 | |
470 if (fail_timeout == (time_t) NGX_ERROR) { | |
471 goto invalid; | |
472 } | |
473 | |
474 continue; | |
475 } | |
476 | |
477 if (ngx_strcmp(value[i].data, "backup") == 0) { | |
478 | |
479 if (!(uscf->flags & NGX_STREAM_UPSTREAM_BACKUP)) { | |
480 goto not_supported; | |
481 } | |
482 | |
483 us->backup = 1; | |
484 | |
485 continue; | |
486 } | |
487 | |
488 if (ngx_strcmp(value[i].data, "down") == 0) { | |
489 | |
490 if (!(uscf->flags & NGX_STREAM_UPSTREAM_DOWN)) { | |
491 goto not_supported; | |
492 } | |
493 | |
494 us->down = 1; | |
495 | |
496 continue; | |
497 } | |
498 | |
499 goto invalid; | |
500 } | |
501 | |
502 ngx_memzero(&u, sizeof(ngx_url_t)); | |
503 | |
504 u.url = value[1]; | |
505 | |
506 if (ngx_parse_url(cf->pool, &u) != NGX_OK) { | |
507 if (u.err) { | |
508 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
509 "%s in upstream \"%V\"", u.err, &u.url); | |
510 } | |
511 | |
512 return NGX_CONF_ERROR; | |
513 } | |
514 | |
515 if (u.no_port) { | |
516 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
517 "no port in upstream \"%V\"", &u.url); | |
518 return NGX_CONF_ERROR; | |
519 } | |
520 | |
521 us->name = u.url; | |
522 us->addrs = u.addrs; | |
523 us->naddrs = u.naddrs; | |
524 us->weight = weight; | |
525 us->max_fails = max_fails; | |
526 us->fail_timeout = fail_timeout; | |
527 | |
528 return NGX_CONF_OK; | |
529 | |
530 invalid: | |
531 | |
532 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
533 "invalid parameter \"%V\"", &value[i]); | |
534 | |
535 return NGX_CONF_ERROR; | |
536 | |
537 not_supported: | |
538 | |
539 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
540 "balancing method does not support parameter \"%V\"", | |
541 &value[i]); | |
542 | |
543 return NGX_CONF_ERROR; | |
544 } | |
545 | |
546 | |
547 ngx_stream_upstream_srv_conf_t * | |
548 ngx_stream_upstream_add(ngx_conf_t *cf, ngx_url_t *u, ngx_uint_t flags) | |
549 { | |
550 ngx_uint_t i; | |
551 ngx_stream_upstream_server_t *us; | |
552 ngx_stream_upstream_srv_conf_t *uscf, **uscfp; | |
553 ngx_stream_upstream_main_conf_t *umcf; | |
554 | |
555 if (!(flags & NGX_STREAM_UPSTREAM_CREATE)) { | |
556 | |
557 if (ngx_parse_url(cf->pool, u) != NGX_OK) { | |
558 if (u->err) { | |
559 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
560 "%s in upstream \"%V\"", u->err, &u->url); | |
561 } | |
562 | |
563 return NULL; | |
564 } | |
565 } | |
566 | |
567 umcf = ngx_stream_conf_get_module_main_conf(cf, ngx_stream_upstream_module); | |
568 | |
569 uscfp = umcf->upstreams.elts; | |
570 | |
571 for (i = 0; i < umcf->upstreams.nelts; i++) { | |
572 | |
573 if (uscfp[i]->host.len != u->host.len | |
574 || ngx_strncasecmp(uscfp[i]->host.data, u->host.data, u->host.len) | |
575 != 0) | |
576 { | |
577 continue; | |
578 } | |
579 | |
580 if ((flags & NGX_STREAM_UPSTREAM_CREATE) | |
581 && (uscfp[i]->flags & NGX_STREAM_UPSTREAM_CREATE)) | |
582 { | |
583 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
584 "duplicate upstream \"%V\"", &u->host); | |
585 return NULL; | |
586 } | |
587 | |
588 if ((uscfp[i]->flags & NGX_STREAM_UPSTREAM_CREATE) && !u->no_port) { | |
589 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, | |
590 "upstream \"%V\" may not have port %d", | |
591 &u->host, u->port); | |
592 return NULL; | |
593 } | |
594 | |
595 if ((flags & NGX_STREAM_UPSTREAM_CREATE) && !uscfp[i]->no_port) { | |
596 ngx_log_error(NGX_LOG_WARN, cf->log, 0, | |
597 "upstream \"%V\" may not have port %d in %s:%ui", | |
598 &u->host, uscfp[i]->port, | |
599 uscfp[i]->file_name, uscfp[i]->line); | |
600 return NULL; | |
601 } | |
602 | |
603 if (uscfp[i]->port != u->port) { | |
604 continue; | |
605 } | |
606 | |
607 if (flags & NGX_STREAM_UPSTREAM_CREATE) { | |
608 uscfp[i]->flags = flags; | |
609 } | |
610 | |
611 return uscfp[i]; | |
612 } | |
613 | |
614 uscf = ngx_pcalloc(cf->pool, sizeof(ngx_stream_upstream_srv_conf_t)); | |
615 if (uscf == NULL) { | |
616 return NULL; | |
617 } | |
618 | |
619 uscf->flags = flags; | |
620 uscf->host = u->host; | |
621 uscf->file_name = cf->conf_file->file.name.data; | |
622 uscf->line = cf->conf_file->line; | |
623 uscf->port = u->port; | |
624 uscf->no_port = u->no_port; | |
625 | |
6459
78fc2dce69e7
Stream: detect port absence in proxy_pass with IP literal.
Roman Arutyunyan <arut@nginx.com>
parents:
6379
diff
changeset
|
626 if (u->naddrs == 1 && (u->port || u->family == AF_UNIX)) { |
6115 | 627 uscf->servers = ngx_array_create(cf->pool, 1, |
628 sizeof(ngx_stream_upstream_server_t)); | |
629 if (uscf->servers == NULL) { | |
630 return NULL; | |
631 } | |
632 | |
633 us = ngx_array_push(uscf->servers); | |
634 if (us == NULL) { | |
635 return NULL; | |
636 } | |
637 | |
638 ngx_memzero(us, sizeof(ngx_stream_upstream_server_t)); | |
639 | |
640 us->addrs = u->addrs; | |
641 us->naddrs = 1; | |
642 } | |
643 | |
644 uscfp = ngx_array_push(&umcf->upstreams); | |
645 if (uscfp == NULL) { | |
646 return NULL; | |
647 } | |
648 | |
649 *uscfp = uscf; | |
650 | |
651 return uscf; | |
652 } | |
653 | |
654 | |
655 static void * | |
656 ngx_stream_upstream_create_main_conf(ngx_conf_t *cf) | |
657 { | |
658 ngx_stream_upstream_main_conf_t *umcf; | |
659 | |
660 umcf = ngx_pcalloc(cf->pool, sizeof(ngx_stream_upstream_main_conf_t)); | |
661 if (umcf == NULL) { | |
662 return NULL; | |
663 } | |
664 | |
665 if (ngx_array_init(&umcf->upstreams, cf->pool, 4, | |
666 sizeof(ngx_stream_upstream_srv_conf_t *)) | |
667 != NGX_OK) | |
668 { | |
669 return NULL; | |
670 } | |
671 | |
672 return umcf; | |
673 } | |
674 | |
675 | |
676 static char * | |
677 ngx_stream_upstream_init_main_conf(ngx_conf_t *cf, void *conf) | |
678 { | |
679 ngx_stream_upstream_main_conf_t *umcf = conf; | |
680 | |
681 ngx_uint_t i; | |
682 ngx_stream_upstream_init_pt init; | |
683 ngx_stream_upstream_srv_conf_t **uscfp; | |
684 | |
685 uscfp = umcf->upstreams.elts; | |
686 | |
687 for (i = 0; i < umcf->upstreams.nelts; i++) { | |
688 | |
689 init = uscfp[i]->peer.init_upstream | |
690 ? uscfp[i]->peer.init_upstream | |
691 : ngx_stream_upstream_init_round_robin; | |
692 | |
693 if (init(cf, uscfp[i]) != NGX_OK) { | |
694 return NGX_CONF_ERROR; | |
695 } | |
696 } | |
697 | |
698 return NGX_CONF_OK; | |
699 } |