all request is redirected to one port if there is multiple listen in default server section

Igor Sysoev is at rambler-co.ru
Fri Apr 10 10:03:27 MSD 2009


On Fri, Apr 10, 2009 at 10:24:59AM +0800, Delta Yeh wrote:

> I use the following config:
> 
> server {
>                     listen 192.168.1.102:80;
>                         listen 192.168.1.102:81;
> 
>         server_name  _;  #default
>         return 444;
>     }
> 
> #upstream cfg
>         upstream web_upstream0 {
>         server 192.168.39.100:81 max_fails=3  fail_timeout=30s;
> 
>     }
>         upstream web_upstream1 {
>         server 192.168.39.100 max_fails=3  fail_timeout=30s;
> 
>     }
> 
> server {
>      listen 192.168.1.102:81;
>     server_name www.test1.com;
>      location / {
> 
>             proxy_pass http://web_upstream0/;
>     }
> }
> server {
>      listen 192.168.1.102:80;
>     server_name www.test2.com;
>      location / {
> 
>             proxy_pass http://web_upstream1/;
>     }
> }
> 
> 
> It seems all request to  www.test1.com is passed to www.test2.com.
> 
> 
> If I use:
> 
> server {
>                     listen 192.168.1.102:80;
> 
>         server_name  _;  #default
>         return 444;
>     }
> server {
>                         listen 192.168.1.102:81;
> 
>         server_name  _;  #default
>         return 444;
>     }
> 
> instead, every thing is OK.
> Is this a bug ?

Yes, this is a bug, introduced in 0.7.39. The attached patch fixes it.


-- 
Igor Sysoev
http://sysoev.ru/en/
-------------- next part --------------
Index: src/http/ngx_http_request.c
===================================================================
--- src/http/ngx_http_request.c	(revision 2005)
+++ src/http/ngx_http_request.c	(working copy)
@@ -372,6 +372,8 @@
         }
     }
 
+    r->virtual_names = addr_conf->virtual_names;
+
     /* the default server configuration for the address:port */
     cscf = addr_conf->core_srv_conf;
 
@@ -1609,15 +1611,11 @@
 {
     u_char                    *server;
     ngx_uint_t                 hash;
-    ngx_http_virtual_names_t  *vn;
     ngx_http_core_loc_conf_t  *clcf;
     ngx_http_core_srv_conf_t  *cscf;
     u_char                     buf[32];
 
-    cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
-    vn = cscf->virtual_names;
-
-    if (vn == NULL) {
+    if (r->virtual_names == NULL) {
         return NGX_DECLINED;
     }
 
@@ -1633,7 +1631,7 @@
 
     hash = ngx_hash_strlow(server, host, len);
 
-    cscf = ngx_hash_find_combined(&vn->names, hash, server, len);
+    cscf = ngx_hash_find_combined(&r->virtual_names->names, hash, server, len);
 
     if (cscf) {
         goto found;
@@ -1641,7 +1639,7 @@
 
 #if (NGX_PCRE)
 
-    if (vn->nregex) {
+    if (r->virtual_names->nregex) {
         size_t                   ncaptures;
         ngx_int_t                n;
         ngx_uint_t               i;
@@ -1653,9 +1651,9 @@
 
         ncaptures = 0;
 
-        sn = vn->regex;
+        sn = r->virtual_names->regex;
 
-        for (i = 0; i < vn->nregex; i++) {
+        for (i = 0; i < r->virtual_names->nregex; i++) {
 
             if (sn[i].captures && r->captures == NULL) {
 
Index: src/http/ngx_http_request.h
===================================================================
--- src/http/ngx_http_request.h	(revision 2005)
+++ src/http/ngx_http_request.h	(working copy)
@@ -387,6 +387,8 @@
     ngx_http_post_subrequest_t       *post_subrequest;
     ngx_http_posted_request_t        *posted_requests;
 
+    ngx_http_virtual_names_t         *virtual_names;
+
     ngx_int_t                         phase_handler;
     ngx_http_handler_pt               content_handler;
     ngx_uint_t                        access_code;
Index: src/http/ngx_http_core_module.h
===================================================================
--- src/http/ngx_http_core_module.h	(revision 2005)
+++ src/http/ngx_http_core_module.h	(working copy)
@@ -153,8 +153,6 @@
     /* server ctx */
     ngx_http_conf_ctx_t        *ctx;
 
-    ngx_http_virtual_names_t   *virtual_names;
-
     ngx_str_t                   server_name;
 
     size_t                      connection_pool_size;
@@ -180,6 +178,8 @@
     /* the default server configuration for this address:port */
     ngx_http_core_srv_conf_t  *core_srv_conf;
 
+    ngx_http_virtual_names_t  *virtual_names;
+
 #if (NGX_HTTP_SSL)
     ngx_uint_t                 ssl;   /* unsigned  ssl:1; */
 #endif
Index: src/http/ngx_http.c
===================================================================
--- src/http/ngx_http.c	(revision 2005)
+++ src/http/ngx_http.c	(working copy)
@@ -1825,7 +1825,7 @@
             return NGX_ERROR;
         }
 
-        addrs[i].conf.core_srv_conf->virtual_names = vn;
+        addrs[i].conf.virtual_names = vn;
 
         vn->names.hash = addr[i].hash;
         vn->names.wc_head = addr[i].wc_head;
@@ -1882,7 +1882,7 @@
             return NGX_ERROR;
         }
 
-        addrs6[i].conf.core_srv_conf->virtual_names = vn;
+        addrs6[i].conf.virtual_names = vn;
 
         vn->names.hash = addr[i].hash;
         vn->names.wc_head = addr[i].wc_head;


More information about the nginx mailing list