ngx_int_t
ngx_parse_url(ngx_conf_t *cf, ngx_url_t *u)
{
- u_char *p, *host;
- size_t len;
+ u_char *p, *host, *port_start;
+ size_t len, port_len;
ngx_int_t port;
ngx_uint_t i;
struct hostent *h;
u->host.len = len;
u->host.data = p;
- u->host_header.len = sizeof("localhost") - 1;
- u->host_header.data = (u_char *) "localhost";
+ u->unix_socket = 1;
return NGX_OK;
}
u->host.data = p;
- u->host_header.len = len;
- u->host_header.data = p;
+
+ port_start = NULL;
+ port_len = 0;
for (i = 0; i < len; i++) {
if (p[i] == ':') {
- u->port.data = &p[i + 1];
+ port_start = &p[i + 1];
u->host.len = i;
if (!u->uri_part) {
- u->port.len = len - (i + 1);
+ port_len = len - (i + 1);
break;
}
}
if (p[i] == '/') {
u->uri.len = len - i;
u->uri.data = &p[i];
- u->host_header.len = i;
if (u->host.len == 0) {
u->host.len = i;
}
- if (u->port.data == NULL) {
+ if (port_start == NULL) {
u->no_port = 1;
goto no_port;
}
- u->port.len = &p[i] - u->port.data;
+ port_len = &p[i] - port_start;
- if (u->port.len == 0) {
+ if (port_len == 0) {
u->err = "invalid port";
return NGX_ERROR;
}
}
}
- if (u->port.data) {
+ if (port_start) {
- if (u->port.len == 0) {
- u->port.len = &p[i] - u->port.data;
+ if (port_len == 0) {
+ port_len = &p[i] - port_start;
- if (u->port.len == 0) {
+ if (port_len == 0) {
u->err = "invalid port";
return NGX_ERROR;
}
}
- port = ngx_atoi(u->port.data, u->port.len);
+ port = ngx_atoi(port_start, port_len);
if (port == NGX_ERROR || port < 1 || port > 65536) {
u->err = "invalid port";
goto no_port;
}
- u->port.len = len;
- u->port.data = p;
u->wildcard = 1;
}
- u->portn = (in_port_t) port;
+ u->port = (in_port_t) port;
no_port:
if (u->listen) {
- if (u->portn == 0) {
- if (u->default_portn == 0) {
+ if (u->port == 0) {
+ if (u->default_port == 0) {
u->err = "no port";
return NGX_ERROR;
}
- u->portn = u->default_portn;
+ u->port = u->default_port;
}
if (u->host.len == 1 && u->host.data[0] == '*') {
return NGX_ERROR;
}
- u->addr.in_addr = *(in_addr_t *)(h->h_addr_list[0]);
+ u->addr.in_addr = *(in_addr_t *) (h->h_addr_list[0]);
}
} else {
return NGX_OK;
}
- if (u->no_port) {
-
- if (u->default_portn == 0 && !u->upstream) {
- u->err = "no port";
- return NGX_ERROR;
- }
-
- u->portn = u->default_portn;
-
- u->port.data = ngx_palloc(cf->pool, sizeof("65536") - 1);
- if (u->port.data == NULL) {
- return NGX_ERROR;
- }
-
- u->port.len = ngx_sprintf(u->port.data, "%d", u->portn) - u->port.data;
-
- } else if (u->portn == 0) {
-
- u->err = "no port";
- return NGX_ERROR;
- }
-
if (u->host.len == 0) {
u->err = "no host";
return NGX_ERROR;
return NGX_OK;
}
+ if (u->no_port) {
+ u->port = u->default_port;
+ }
+
+ if (u->port == 0) {
+ u->err = "no port";
+ return NGX_ERROR;
+ }
+
if (ngx_inet_resolve_host(cf, u) != NGX_OK) {
return NGX_ERROR;
}
ngx_int_t
ngx_inet_resolve_host(ngx_conf_t *cf, ngx_url_t *u)
{
- u_char *host;
+ u_char *p, *host;
size_t len;
in_addr_t in_addr;
ngx_uint_t i;
}
sin->sin_family = AF_INET;
- sin->sin_port = htons(u->portn);
+ sin->sin_port = htons(u->port);
sin->sin_addr.s_addr = *(in_addr_t *) (h->h_addr_list[i]);
u->addrs[i].sockaddr = (struct sockaddr *) sin;
len = INET_ADDRSTRLEN - 1 + 1 + sizeof(":65536") - 1;
- u->addrs[i].name.data = ngx_palloc(cf->pool, len);
- if (u->addrs[i].name.data == NULL) {
+ p = ngx_palloc(cf->pool, len);
+ if (p == NULL) {
return NGX_ERROR;
}
- len = ngx_sock_ntop(AF_INET, (struct sockaddr *) sin,
- u->addrs[i].name.data, len);
+ len = ngx_sock_ntop(AF_INET, (struct sockaddr *) sin, p, len);
- u->addrs[i].name.len = ngx_sprintf(&u->addrs[i].name.data[len],
- ":%d", u->portn)
- - u->addrs[i].name.data;
+ u->addrs[i].name.len = ngx_sprintf(&p[len], ":%d", u->port) - p;
+ u->addrs[i].name.data = p;
}
} else {
u->naddrs = 1;
sin->sin_family = AF_INET;
- sin->sin_port = htons(u->portn);
+ sin->sin_port = htons(u->port);
sin->sin_addr.s_addr = in_addr;
u->addrs[0].sockaddr = (struct sockaddr *) sin;
u->addrs[0].socklen = sizeof(struct sockaddr_in);
- u->addrs[0].name.data = ngx_palloc(cf->pool,
- u->host.len + sizeof(":65536") - 1);
- if (u->addrs[0].name.data == NULL) {
+ p = ngx_palloc(cf->pool, u->host.len + sizeof(":65536") - 1);
+ if (p == NULL) {
return NGX_ERROR;
}
- u->addrs[0].name.len = ngx_sprintf(u->addrs[0].name.data, "%V:%d",
- &u->host, u->portn)
- - u->addrs[0].name.data;
+ u->addrs[0].name.len = ngx_sprintf(p, "%V:%d", &u->host, u->port) - p;
+ u->addrs[0].name.data = p;
}
return NGX_OK;
ngx_str_t url;
ngx_str_t host;
- ngx_str_t host_header;
- ngx_str_t port;
ngx_str_t uri;
- in_port_t portn;
- in_port_t default_portn;
+ in_port_t port;
+ in_port_t default_port;
unsigned listen:1;
unsigned uri_part:1;
- unsigned upstream:1;
unsigned no_resolve:1;
unsigned one_addr:1;
unsigned wildcard:1;
unsigned no_port:1;
+ unsigned unix_socket:1;
ngx_url_addr_t addr;
ngx_memzero(&u, sizeof(ngx_url_t));
u.url = value[1];
- u.upstream = 1;
u.no_resolve = 1;
lcf->upstream.upstream = ngx_http_upstream_add(cf, &u, 0);
ngx_memzero(&u, sizeof(ngx_url_t));
u.url = value[1];
- u.upstream = 1;
u.no_resolve = 1;
/* u.uri_part = 1; may be used as namespace */
ngx_str_t method;
ngx_str_t host_header;
- ngx_str_t port_text;
+ ngx_str_t port;
ngx_flag_t redirect;
} ngx_http_proxy_loc_conf_t;
plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);
- v->len = plcf->port_text.len;
+ v->len = plcf->port.len;
v->valid = 1;
v->no_cachable = 0;
v->not_found = 0;
- v->data = plcf->port_text.data;
+ v->data = plcf->port.data;
return NGX_OK;
}
conf->upstream.upstream = prev->upstream.upstream;
conf->host_header = prev->host_header;
- conf->port_text = prev->port_text;
+ conf->port = prev->port;
conf->upstream.schema = prev->upstream.schema;
}
{
ngx_http_proxy_loc_conf_t *plcf = conf;
+ u_char *p;
size_t add;
u_short port;
ngx_str_t *value, *url;
u.url.len = url->len - add;
u.url.data = url->data + add;
- u.default_portn = port;
- u.upstream = 1;
- u.no_resolve = 1;
+ u.default_port = port;
u.uri_part = 1;
+ u.no_resolve = 1;
plcf->upstream.upstream = ngx_http_upstream_add(cf, &u, 0);
if (plcf->upstream.upstream == NULL) {
return NGX_CONF_ERROR;
}
- plcf->host_header = u.host_header;
- plcf->port_text = u.port;
+ if (!u.unix_socket) {
+ if (u.no_port || u.port == port) {
+ plcf->host_header = u.host;
+
+ if (port == 80) {
+ plcf->port.len = sizeof("80") - 1;
+ plcf->port.data = (u_char *) "80";
+ } else {
+ plcf->port.len = sizeof("443") - 1;
+ plcf->port.data = (u_char *) "443";
+ }
+
+ } else {
+ p = ngx_palloc(cf->pool, u.host.len + sizeof(":65536") - 1);
+ if (p == NULL) {
+ return NGX_CONF_ERROR;
+ }
+
+ plcf->host_header.len = ngx_sprintf(p, "%V:%d", &u.host, u.port)
+ - p;
+ plcf->host_header.data = p;
+
+ plcf->port.len = plcf->host_header.len - u.host.len - 1;
+ plcf->port.data = p + u.host.len + 1;
+ }
+
+
+ } else {
+ plcf->host_header.len = sizeof("localhost") - 1;
+ plcf->host_header.data = (u_char *) "localhost";
+ plcf->port.len = 0;
+ plcf->port.data = (u_char *) "";
+ }
+
plcf->upstream.uri = u.uri;
plcf->upstream.schema.len = add;
u.url = value[1];
u.listen = 1;
- u.default_portn = 80;
+ u.default_port = 80;
if (ngx_parse_url(cf, &u) != NGX_OK) {
if (u.err) {
ls->family = AF_INET;
ls->addr = u.addr.in_addr;
- ls->port = u.portn;
+ ls->port = u.port;
ls->file_name = cf->conf_file->file.name;
ls->line = cf->conf_file->line;
ls->conf.backlog = -1;
value = cf->args->elts;
u.host = value[1];
- u.upstream = 1;
u.no_resolve = 1;
uscf = ngx_http_upstream_add(cf, &u, NGX_HTTP_UPSTREAM_CREATE
ngx_memzero(&u, sizeof(ngx_url_t));
u.url = value[1];
- u.default_portn = 80;
+ u.default_port = 80;
if (ngx_parse_url(cf, &u) != NGX_OK) {
if (u.err) {
uscfp = umcf->upstreams.elts;
for (i = 0; i < umcf->upstreams.nelts; i++) {
- if ((uscfp[i]->port && uscfp[i]->port != u->portn)
- || uscfp[i]->host.len != u->host.len
+
+ if (uscfp[i]->host.len != u->host.len
|| ngx_strncasecmp(uscfp[i]->host.data, u->host.data, u->host.len)
!= 0)
{
return NULL;
}
- if (uscfp[i]->port == 0 && u->portn && !u->no_port) {
+ if ((uscfp[i]->flags & NGX_HTTP_UPSTREAM_CREATE) && u->port) {
ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
- "upstream \"%V\" port %d is ignored",
- &u->host, u->portn);
+ "upstream \"%V\" may not have port %d",
+ &u->host, u->port);
+ return NULL;
+ }
+
+ if ((flags & NGX_HTTP_UPSTREAM_CREATE) && uscfp[i]->port) {
+ ngx_log_error(NGX_LOG_WARN, cf->log, 0,
+ "upstream \"%V\" may not have port %d in %s:%ui",
+ &u->host, uscfp[i]->port,
+ uscfp[i]->file_name.data, uscfp[i]->line);
+ return NULL;
+ }
+
+ if (uscfp[i]->port != u->port) {
+ continue;
}
return uscfp[i];
uscf->host = u->host;
uscf->file_name = cf->conf_file->file.name;
uscf->line = cf->conf_file->line;
- uscf->port = u->portn;
+ uscf->port = u->port;
+ uscf->default_port = u->default_port;
if (u->naddrs == 1) {
uscf->servers = ngx_array_create(cf->pool, 1,
ngx_str_t file_name;
ngx_uint_t line;
in_port_t port;
+ in_port_t default_port;
};
/* an upstream implicitly defined by proxy_pass, etc. */
+ if (us->port == 0 && us->default_port == 0) {
+ ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
+ "no port in upstream \"%V\" in %s:%ui",
+ &us->host, us->file_name.data, us->line);
+ return NGX_ERROR;
+ }
+
ngx_memzero(&u, sizeof(ngx_url_t));
u.host = us->host;
- u.portn = us->port;
+ u.port = us->port ? us->port : us->default_port;
if (ngx_inet_resolve_host(cf, &u) != NGX_OK) {
if (u.err) {
return NGX_ERROR;
}
- if (us->port == 0) {
- ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
- "no port in upstream \"%V\" in %s:%ui",
- &us->host, us->file_name.data, us->line);
- return NGX_ERROR;
- }
-
n = u.naddrs;
peers = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_rr_peers_t)
ngx_memzero(&u, sizeof(ngx_url_t));
u.url = value[1];
- u.default_portn = 80;
+ u.default_port = 80;
u.uri_part = 1;
u.one_addr = 1;
ahcf->peer = u.addrs;
- ahcf->host_header = u.host_header;
+ ahcf->host_header = u.host;
ahcf->uri = u.uri;
if (ahcf->uri.len == 0) {
for (i = 0; i < cmcf->listen.nelts; i++) {
- if (imls[i].addr != u.addr.in_addr || imls[i].port != u.portn) {
+ if (imls[i].addr != u.addr.in_addr || imls[i].port != u.port) {
continue;
}
ngx_memzero(imls, sizeof(ngx_imap_listen_t));
imls->addr = u.addr.in_addr;
- imls->port = u.portn;
+ imls->port = u.port;
imls->family = AF_INET;
imls->ctx = cf->ctx;
ngx_memzero(&u, sizeof(ngx_url_t));
u.url = value[1];
- u.default_portn = 3306;
+ u.default_port = 3306;
if (ngx_parse_url(cf, &u) != NGX_OK) {
if (u.err) {