bug with variables in fastcgi_pass

Igor Sysoev is at rambler-co.ru
Mon Dec 22 20:04:03 MSK 2008


On Mon, Dec 22, 2008 at 05:24:23PM +0100, Samuel Vogel wrote:

> Igor Sysoev schrieb:
> >On Mon, Dec 22, 2008 at 03:25:04PM +0100, Samuel Vogel wrote:
> >
> >  
> >>Samuel Vogel schrieb:
> >>    
> >>>Hey Igor,
> >>>
> >>>I think I have hit a bug while trying out the feature you added in 
> >>>0.7.27.
> >>>
> >>>My config looks like this:
> >>>server {
> >>>  listen 127.0.0.1:81;
> >>>  server_name _;
> >>>    access_log  /var/log/nginx/access.log;
> >>>
> >>>  set $subdir "default";
> >>>
> >>>  if ($host ~ "^(.+)\.user\.domain\.net$") {
> >>>      set $subdir $1;
> >>>  }
> >>>
> >>>  root   /www/$subdir;
> >>>  index  index.php index.html index.htm;
> >>>    location ~ \.php$ {
> >>>      fastcgi_pass   unix:/var/run/php-fpm/socket-$subdir;
> >>>  }
> >>>}
> >>>
> >>>
> >>>And I get the following error:
> >>>[error] 17238#0: *1 no resolver defined to resolve 
> >>>/var/run/php-fpm/socket-345232, client: 127.0.0.1, server: _, request: 
> >>>"GET /info.php HTTP/1.1", host: "345232.user.domain.net"
> >>>
> >>>If I remove the variable from fastcgi_pass everything works fine!
> >>>It seems like nginx is ignoring that I try to point it to a unix 
> >>>socket, when I use a variable!
> >>>
> >>>Regards,
> >>>Samy
> >>>      
> >>Hey Igor,
> >>
> >>I have just tried to 0.7.26 with the patch you sent to the list and I do 
> >>get the exact same error:
> >>
> >>2008/12/22 14:05:45 [error] 23482#0: *2 no resolver defined to resolve 
> >>/var/run/php-fpm/socket-345232, client: 127.0.0.1, server: _, request: 
> >>"GET /info.php HTTP/1.1", host: "345232.user.domain.net"
> >>
> >>Maybe this helps to isolate the issue a bit!
> >>    
> >
> >fastcgi_pass currently does not support variables for unix sockets:
> >it tries to resolve it as domain name. I will try to fix the bug before
> >vacantion
> This would be great, since this is what we need the variable support for!

The attached patch allows to use variables in unix sockets path
in fastcgi_pass and proxy_pass.


-- 
Igor Sysoev
http://sysoev.ru/en/
-------------- next part --------------
Index: src/http/ngx_http_upstream.c
===================================================================
--- src/http/ngx_http_upstream.c	(revision 1739)
+++ src/http/ngx_http_upstream.c	(working copy)
@@ -414,6 +414,20 @@
 
     } else {
 
+        if (u->resolved->sockaddr) {
+
+            if (ngx_http_upstream_create_round_robin_peer(r, u->resolved)
+                != NGX_OK)
+            {
+                ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
+                return;
+            }
+
+            ngx_http_upstream_connect(r, u);
+
+            return;
+        }
+
         host = &u->resolved->host;
 
         umcf = ngx_http_get_module_main_conf(r, ngx_http_upstream_module);
Index: src/http/ngx_http_upstream_round_robin.c
===================================================================
--- src/http/ngx_http_upstream_round_robin.c	(revision 1739)
+++ src/http/ngx_http_upstream_round_robin.c	(working copy)
@@ -279,35 +279,47 @@
     peers->number = ur->naddrs;
     peers->name = &ur->host;
 
-    for (i = 0; i < ur->naddrs; i++) {
+    if (ur->sockaddr) {
+        peers->peer[0].sockaddr = ur->sockaddr;
+        peers->peer[0].socklen = ur->socklen;
+        peers->peer[0].name = ur->host;
+        peers->peer[0].weight = 1;
+        peers->peer[0].current_weight = 1;
+        peers->peer[0].max_fails = 1;
+        peers->peer[0].fail_timeout = 10;
 
-        len = NGX_INET_ADDRSTRLEN + sizeof(":65536") - 1;
+    } else {
 
-        p = ngx_pnalloc(r->pool, len);
-        if (p == NULL) {
-            return NGX_ERROR;
-        }
+        for (i = 0; i < ur->naddrs; i++) {
 
-        len = ngx_inet_ntop(AF_INET, &ur->addrs[i], p, NGX_INET_ADDRSTRLEN);
-        len = ngx_sprintf(&p[len], ":%d", ur->port) - p;
+            len = NGX_INET_ADDRSTRLEN + sizeof(":65536") - 1;
 
-        sin = ngx_pcalloc(r->pool, sizeof(struct sockaddr_in));
-        if (sin == NULL) {
-            return NGX_ERROR;
-        }
+            p = ngx_pnalloc(r->pool, len);
+            if (p == NULL) {
+                return NGX_ERROR;
+            }
 
-        sin->sin_family = AF_INET;
-        sin->sin_port = htons(ur->port);
-        sin->sin_addr.s_addr = ur->addrs[i];
+            len = ngx_inet_ntop(AF_INET, &ur->addrs[i], p, NGX_INET_ADDRSTRLEN);
+            len = ngx_sprintf(&p[len], ":%d", ur->port) - p;
 
-        peers->peer[i].sockaddr = (struct sockaddr *) sin;
-        peers->peer[i].socklen = sizeof(struct sockaddr_in);
-        peers->peer[i].name.len = len;
-        peers->peer[i].name.data = p;
-        peers->peer[i].weight = 1;
-        peers->peer[i].current_weight = 1;
-        peers->peer[i].max_fails = 1;
-        peers->peer[i].fail_timeout = 10;
+            sin = ngx_pcalloc(r->pool, sizeof(struct sockaddr_in));
+            if (sin == NULL) {
+                return NGX_ERROR;
+            }
+
+            sin->sin_family = AF_INET;
+            sin->sin_port = htons(ur->port);
+            sin->sin_addr.s_addr = ur->addrs[i];
+
+            peers->peer[i].sockaddr = (struct sockaddr *) sin;
+            peers->peer[i].socklen = sizeof(struct sockaddr_in);
+            peers->peer[i].name.len = len;
+            peers->peer[i].name.data = p;
+            peers->peer[i].weight = 1;
+            peers->peer[i].current_weight = 1;
+            peers->peer[i].max_fails = 1;
+            peers->peer[i].fail_timeout = 10;
+        }
     }
 
     rrp->peers = peers;
Index: src/http/ngx_http_upstream.h
===================================================================
--- src/http/ngx_http_upstream.h	(revision 1739)
+++ src/http/ngx_http_upstream.h	(working copy)
@@ -209,8 +209,13 @@
     ngx_str_t                       host;
     in_port_t                       port;
     ngx_uint_t                      no_port; /* unsigned  no_port:1 */
+
     ngx_uint_t                      naddrs;
     in_addr_t                      *addrs;
+
+    struct sockaddr                *sockaddr;
+    socklen_t                       socklen;
+
     ngx_resolver_ctx_t             *ctx;
 } ngx_http_upstream_resolved_t;
 
Index: src/http/modules/ngx_http_fastcgi_module.c
===================================================================
--- src/http/modules/ngx_http_fastcgi_module.c	(revision 1739)
+++ src/http/modules/ngx_http_fastcgi_module.c	(working copy)
@@ -512,9 +512,17 @@
         return NGX_ERROR;
     }
 
-    r->upstream->resolved->host = u.host;
-    r->upstream->resolved->port = u.port;
+    if (u.addrs[0].sockaddr) {
+        r->upstream->resolved->sockaddr = u.addrs[0].sockaddr;
+        r->upstream->resolved->socklen = u.addrs[0].socklen;
+        r->upstream->resolved->naddrs = 1;
+        r->upstream->resolved->host = u.addrs[0].name;
 
+    } else {
+        r->upstream->resolved->host = u.host;
+        r->upstream->resolved->port = u.port;
+    }
+
     return NGX_OK;
 }
 
Index: src/http/modules/ngx_http_proxy_module.c
===================================================================
--- src/http/modules/ngx_http_proxy_module.c	(revision 1739)
+++ src/http/modules/ngx_http_proxy_module.c	(working copy)
@@ -614,11 +614,19 @@
         return NGX_ERROR;
     }
 
-    r->upstream->resolved->host = u.host;
-    r->upstream->resolved->port = (in_port_t) (u.no_port ? u.default_port:
-                                                           u.port);
-    r->upstream->resolved->no_port = u.no_port;
+    if (u.addrs[0].sockaddr) {
+        r->upstream->resolved->sockaddr = u.addrs[0].sockaddr;
+        r->upstream->resolved->socklen = u.addrs[0].socklen;
+        r->upstream->resolved->naddrs = 1;
+        r->upstream->resolved->host = u.addrs[0].name;
 
+    } else {
+        r->upstream->resolved->host = u.host;
+        r->upstream->resolved->port = (in_port_t) (u.no_port ? u.default_port:
+                                                               u.port);
+        r->upstream->resolved->no_port = u.no_port;
+    }
+
     return NGX_OK;
 }
 


More information about the nginx mailing list