comparison src/stream/ngx_stream_upstream.c @ 6675:ab9b4fd8c5b7

Stream: the $upstream_addr variable. Keeps the full address of the upstream server. If several servers were contacted during proxying, their addresses are separated by commas, e.g. "192.168.1.1:80, 192.168.1.2:80".
author Vladimir Homutov <vl@nginx.com>
date Fri, 02 Sep 2016 18:27:05 +0300
parents 8ed51b02f655
children df3a7c029dec
comparison
equal deleted inserted replaced
6674:38143d1abdec 6675:ab9b4fd8c5b7
7 7
8 #include <ngx_config.h> 8 #include <ngx_config.h>
9 #include <ngx_core.h> 9 #include <ngx_core.h>
10 #include <ngx_stream.h> 10 #include <ngx_stream.h>
11 11
12
13 static ngx_int_t ngx_stream_upstream_add_variables(ngx_conf_t *cf);
14 static ngx_int_t ngx_stream_upstream_addr_variable(ngx_stream_session_t *s,
15 ngx_stream_variable_value_t *v, uintptr_t data);
12 16
13 static char *ngx_stream_upstream(ngx_conf_t *cf, ngx_command_t *cmd, 17 static char *ngx_stream_upstream(ngx_conf_t *cf, ngx_command_t *cmd,
14 void *dummy); 18 void *dummy);
15 static char *ngx_stream_upstream_server(ngx_conf_t *cf, ngx_command_t *cmd, 19 static char *ngx_stream_upstream_server(ngx_conf_t *cf, ngx_command_t *cmd,
16 void *conf); 20 void *conf);
37 ngx_null_command 41 ngx_null_command
38 }; 42 };
39 43
40 44
41 static ngx_stream_module_t ngx_stream_upstream_module_ctx = { 45 static ngx_stream_module_t ngx_stream_upstream_module_ctx = {
42 NULL, /* preconfiguration */ 46 ngx_stream_upstream_add_variables, /* preconfiguration */
43 NULL, /* postconfiguration */ 47 NULL, /* postconfiguration */
44 48
45 ngx_stream_upstream_create_main_conf, /* create main configuration */ 49 ngx_stream_upstream_create_main_conf, /* create main configuration */
46 ngx_stream_upstream_init_main_conf, /* init main configuration */ 50 ngx_stream_upstream_init_main_conf, /* init main configuration */
47 51
64 NULL, /* exit master */ 68 NULL, /* exit master */
65 NGX_MODULE_V1_PADDING 69 NGX_MODULE_V1_PADDING
66 }; 70 };
67 71
68 72
73 static ngx_stream_variable_t ngx_stream_upstream_vars[] = {
74
75 { ngx_string("upstream_addr"), NULL,
76 ngx_stream_upstream_addr_variable, 0,
77 NGX_STREAM_VAR_NOCACHEABLE, 0 },
78
79 { ngx_null_string, NULL, NULL, 0, 0, 0 }
80 };
81
82
83 static ngx_int_t
84 ngx_stream_upstream_add_variables(ngx_conf_t *cf)
85 {
86 ngx_stream_variable_t *var, *v;
87
88 for (v = ngx_stream_upstream_vars; v->name.len; v++) {
89 var = ngx_stream_add_variable(cf, &v->name, v->flags);
90 if (var == NULL) {
91 return NGX_ERROR;
92 }
93
94 var->get_handler = v->get_handler;
95 var->data = v->data;
96 }
97
98 return NGX_OK;
99 }
100
101
102 static ngx_int_t
103 ngx_stream_upstream_addr_variable(ngx_stream_session_t *s,
104 ngx_stream_variable_value_t *v, uintptr_t data)
105 {
106 u_char *p;
107 size_t len;
108 ngx_uint_t i;
109 ngx_stream_upstream_state_t *state;
110
111 v->valid = 1;
112 v->no_cacheable = 0;
113 v->not_found = 0;
114
115 if (s->upstream_states == NULL || s->upstream_states->nelts == 0) {
116 v->not_found = 1;
117 return NGX_OK;
118 }
119
120 len = 0;
121 state = s->upstream_states->elts;
122
123 for (i = 0; i < s->upstream_states->nelts; i++) {
124 if (state[i].peer) {
125 len += state[i].peer->len;
126 }
127
128 len += 2;
129 }
130
131 p = ngx_pnalloc(s->connection->pool, len);
132 if (p == NULL) {
133 return NGX_ERROR;
134 }
135
136 v->data = p;
137
138 i = 0;
139
140 for ( ;; ) {
141 if (state[i].peer) {
142 p = ngx_cpymem(p, state[i].peer->data, state[i].peer->len);
143 }
144
145 if (++i == s->upstream_states->nelts) {
146 break;
147 }
148
149 *p++ = ',';
150 *p++ = ' ';
151 }
152
153 v->len = p - v->data;
154
155 return NGX_OK;
156 }
157
158
69 static char * 159 static char *
70 ngx_stream_upstream(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy) 160 ngx_stream_upstream(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy)
71 { 161 {
72 char *rv; 162 char *rv;
73 void *mconf; 163 void *mconf;