﻿id	summary	reporter	owner	description	type	status	priority	milestone	component	version	resolution	keywords	cc	uname	nginx_version
187	incorrect handling of bind option on wildcard listen addresses	Roman Odaisky	somebody	"Given the configuration

    listen 10.2.2.2:80;
    ...
    listen *:80;

or alternatively,

    listen [2001:db8::42]:80;
    ...
    listen [::]:80;

or any other configuration where a wildcard listen directive is present along with non-wildcard entries for the same port, nginx may fail to perform bind() correctly only once, depending on the order in which it encounters the directives. This is due to faulty sorting in ngx_http_optimize_servers according to ngx_http_cmp_conf_addrs criterion, which is not guaranteed to place the wildcard entry/entries at the end of the list as expected by ngx_http_init_listening. Eventually the problem causes EADDRINUSE upon startup due to multiple bind() calls. The following simple patch fixes the problem, presuming the wildcard field always equals 0 or 1.

{{{#!diff
--- a/src/http/ngx_http.c 2012-07-11 22:17:51.000000000 +0000
+++ b/src/http/ngx_http.c 2012-07-11 22:18:15.000000000 +0000
@@ -1608,9 +1608,9 @@
     first = (ngx_http_conf_addr_t *) one;
     second = (ngx_http_conf_addr_t *) two;
 
-    if (first->opt.wildcard) {
+    if (first->opt.wildcard != second->opt.wildcard) {
         /* a wildcard address must be the last resort, shift it to the end */
-        return 1;
+        return (int)first->opt.wildcard - (int)second->opt.wildcard;
     }
 
     if (first->opt.bind && !second->opt.bind) {

}}}

-- 
HTH
Roman Odaisky"	defect	closed	minor		nginx-core	1.3.x	fixed			N/A	"nginx/1.2.1 — fix tested;
nginx/1.3.3 — code unchanged, issue presumed to persist."
