$hostname variable
Igor Sysoev
14 years ago
115 | 115 |
#define INET_ADDRSTRLEN 16
|
116 | 116 |
#endif
|
117 | 117 |
|
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
|
122 | 123 |
|
123 | 124 |
|
124 | 125 |
#if ((__GNU__ == 2) && (__GNUC_MINOR__ < 8))
|
56 | 56 |
ngx_listening_t *ls, *nls;
|
57 | 57 |
ngx_core_conf_t *ccf, *old_ccf;
|
58 | 58 |
ngx_core_module_t *module;
|
|
59 |
char hostname[NGX_MAXHOSTNAMELEN];
|
59 | 60 |
|
60 | 61 |
log = old_cycle->log;
|
61 | 62 |
|
|
167 | 168 |
ngx_destroy_pool(pool);
|
168 | 169 |
return NULL;
|
169 | 170 |
}
|
|
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);
|
170 | 191 |
|
171 | 192 |
|
172 | 193 |
for (i = 0; ngx_modules[i]; i++) {
|
2605 | 2605 |
}
|
2606 | 2606 |
|
2607 | 2607 |
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;
|
2622 | 2609 |
|
2623 | 2610 |
sn = ngx_array_push(&conf->server_names);
|
2624 | 2611 |
if (sn == NULL) {
|
73 | 73 |
ngx_http_variable_value_t *v, uintptr_t data);
|
74 | 74 |
|
75 | 75 |
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,
|
76 | 78 |
ngx_http_variable_value_t *v, uintptr_t data);
|
77 | 79 |
|
78 | 80 |
/*
|
|
220 | 222 |
{ ngx_string("nginx_version"), NULL, ngx_http_variable_nginx_version,
|
221 | 223 |
0, 0, 0 },
|
222 | 224 |
|
|
225 |
{ ngx_string("hostname"), NULL, ngx_http_variable_hostname,
|
|
226 |
0, 0, 0 },
|
|
227 |
|
223 | 228 |
{ ngx_null_string, NULL, NULL, 0, 0, 0 }
|
224 | 229 |
};
|
225 | 230 |
|
|
1271 | 1276 |
}
|
1272 | 1277 |
|
1273 | 1278 |
|
|
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 |
|
1274 | 1293 |
ngx_int_t
|
1275 | 1294 |
ngx_http_variables_add_core_vars(ngx_conf_t *cf)
|
1276 | 1295 |
{
|
184 | 184 |
ngx_conf_merge_str_value(conf->server_name, prev->server_name, "");
|
185 | 185 |
|
186 | 186 |
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;
|
201 | 188 |
}
|
202 | 189 |
|
203 | 190 |
if (conf->protocol == NULL) {
|