Klaus Demo nginx / 3be5257
$hostname variable Igor Sysoev 14 years ago
6 changed file(s) with 48 addition(s) and 32 deletion(s). Raw diff Collapse all Expand all
115115 #define INET_ADDRSTRLEN 16
116116 #endif
117117
118 #define NGX_MAXHOSTNAMELEN 64
119 /*
120 #define NGX_MAXHOSTNAMELEN MAXHOSTNAMELEN
121 */
118 #ifdef MAXHOSTNAMELEN
119 #define NGX_MAXHOSTNAMELEN MAXHOSTNAMELEN
120 #else
121 #define NGX_MAXHOSTNAMELEN 256
122 #endif
122123
123124
124125 #if ((__GNU__ == 2) && (__GNUC_MINOR__ < 8))
5656 ngx_listening_t *ls, *nls;
5757 ngx_core_conf_t *ccf, *old_ccf;
5858 ngx_core_module_t *module;
59 char hostname[NGX_MAXHOSTNAMELEN];
5960
6061 log = old_cycle->log;
6162
167168 ngx_destroy_pool(pool);
168169 return NULL;
169170 }
171
172
173 if (gethostname(hostname, NGX_MAXHOSTNAMELEN) == -1) {
174 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "gethostname() failed");
175 ngx_destroy_pool(pool);
176 return NULL;
177 }
178
179 /* on Linux gethostname() silently truncates name that does not fit */
180
181 hostname[NGX_MAXHOSTNAMELEN - 1] = '\0';
182 cycle->hostname.len = ngx_strlen(hostname);
183
184 cycle->hostname.data = ngx_palloc(pool, cycle->hostname.len);
185 if (cycle->hostname.data == NULL) {
186 ngx_destroy_pool(pool);
187 return NULL;
188 }
189
190 ngx_memcpy(cycle->hostname.data, hostname, cycle->hostname.len);
170191
171192
172193 for (i = 0; ngx_modules[i]; i++) {
6161 ngx_str_t conf_file;
6262 ngx_str_t root;
6363 ngx_str_t lock_file;
64 ngx_str_t hostname;
6465 };
6566
6667
26052605 }
26062606
26072607 if (conf->server_name.data == NULL) {
2608 conf->server_name.data = ngx_palloc(cf->pool, NGX_MAXHOSTNAMELEN);
2609 if (conf->server_name.data == NULL) {
2610 return NGX_CONF_ERROR;
2611 }
2612
2613 if (gethostname((char *) conf->server_name.data, NGX_MAXHOSTNAMELEN)
2614 == -1)
2615 {
2616 ngx_conf_log_error(NGX_LOG_EMERG, cf, ngx_errno,
2617 "gethostname() failed");
2618 return NGX_CONF_ERROR;
2619 }
2620
2621 conf->server_name.len = ngx_strlen(conf->server_name.data);
2608 conf->server_name = cf->cycle->hostname;
26222609
26232610 sn = ngx_array_push(&conf->server_names);
26242611 if (sn == NULL) {
7373 ngx_http_variable_value_t *v, uintptr_t data);
7474
7575 static ngx_int_t ngx_http_variable_nginx_version(ngx_http_request_t *r,
76 ngx_http_variable_value_t *v, uintptr_t data);
77 static ngx_int_t ngx_http_variable_hostname(ngx_http_request_t *r,
7678 ngx_http_variable_value_t *v, uintptr_t data);
7779
7880 /*
220222 { ngx_string("nginx_version"), NULL, ngx_http_variable_nginx_version,
221223 0, 0, 0 },
222224
225 { ngx_string("hostname"), NULL, ngx_http_variable_hostname,
226 0, 0, 0 },
227
223228 { ngx_null_string, NULL, NULL, 0, 0, 0 }
224229 };
225230
12711276 }
12721277
12731278
1279 static ngx_int_t
1280 ngx_http_variable_hostname(ngx_http_request_t *r,
1281 ngx_http_variable_value_t *v, uintptr_t data)
1282 {
1283 v->len = ngx_cycle->hostname.len;
1284 v->valid = 1;
1285 v->no_cacheable = 0;
1286 v->not_found = 0;
1287 v->data = ngx_cycle->hostname.data;
1288
1289 return NGX_OK;
1290 }
1291
1292
12741293 ngx_int_t
12751294 ngx_http_variables_add_core_vars(ngx_conf_t *cf)
12761295 {
184184 ngx_conf_merge_str_value(conf->server_name, prev->server_name, "");
185185
186186 if (conf->server_name.len == 0) {
187 conf->server_name.data = ngx_palloc(cf->pool, NGX_MAXHOSTNAMELEN);
188 if (conf->server_name.data == NULL) {
189 return NGX_CONF_ERROR;
190 }
191
192 if (gethostname((char *) conf->server_name.data, NGX_MAXHOSTNAMELEN)
193 == -1)
194 {
195 ngx_log_error(NGX_LOG_EMERG, cf->log, ngx_errno,
196 "gethostname() failed");
197 return NGX_CONF_ERROR;
198 }
199
200 conf->server_name.len = ngx_strlen(conf->server_name.data);
187 conf->server_name = cf->cycle->hostname;
201188 }
202189
203190 if (conf->protocol == NULL) {