# Nginx tries to allocate a huge number of memory and fails

**URL:** <https://www.ruby-forum.com/t/nginx-tries-to-allocate-a-huge-number-of-memory-and-fails/210281>\
**Category:** NGINX\
**Created:** [August 4, 2011, 5:47am UTC](https://www.ruby-forum.com/t/nginx-tries-to-allocate-a-huge-number-of-memory-and-fails/210281 "2011-08-04T05:47:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [August 4, 2011, 5:47am UTC](https://www.ruby-forum.com/t/nginx-tries-to-allocate-a-huge-number-of-memory-and-fails/210281/1 "2011-08-04T05:47:48Z")

</div>

hi

i’m having a strange case where nginx is trying to allocate some  
really huge amount of memory. we created a new vhost on nginx to  
provide an internal interface to flush the apc cache of php. in most  
of the cases i can request this and get what i expected, only in  
around 20% of the request nginx is trying to allocate a huge number of  
memory and fails, so it returns 500 internal server error.

2011/08/04 05:14:00 [emerg] 41529#0: \*89012062 malloc()  
18446744073709545066 bytes failed (12: Cannot allocate memory),  
client: 192.168.1.195, server: \*.kaufmich.vpn, request: “GET  
/web1/apc/flushApc.php HTTP/1.1”, host: “static.kaufmich.vpn”

does anybody have an idea why that would happen? i thought this almost  
has to be a bug because the amount of bytes it is trying to allocate  
is just so huge it just cant be right.

# nginx -V

nginx version: nginx/0.7.65  
built by gcc 4.2.4 (Ubuntu 4.2.4-1ubuntu4)  
configure arguments: --prefix=/usr/local/nginx-0.7.65  
–without-http\_ssi\_module --without-http\_userid\_module  
–without-http\_geo\_module --with-pcre=/tmp/pcre-8.02  
–add-module=src/http/modules/ngx\_cache\_purge/

server {  
server\_name \*.kaufmich.vpn;

```
    access_log /var/log/nginx/deployment_interface/access.log;
    error_log /var/log/nginx/deployment_interface/error.log debug;

    set $doc_dir /var/www/kaufmich;
    set $script_dir $doc_dir;
    root $doc_dir;

    location / {

    if ($request_uri !~* /.+/.+/.+) {
            return 501;
    }

    if ($request_uri ~* /(.+)/(.+\.php)) {
            set $prod_server $1;
            set $action $2;
            set $script $3;
            set $parameters "";
            set $script_dir $doc_dir/$action;
    }

    if ($request_uri ~* /(.+)/(.+)/(.+\.php)) {
            set $prod_server $1;
            set $action $2;
            set $script $3;
            set $parameters "";
            set $script_dir $doc_dir/$action;
    }

    if ($request_uri ~* /(.+)/(.+)/(.+\.php)\?(.+)) {
            set $prod_server $1;
            set $action $2;
            set $script $3;
            set $parameters $4;
            set $script_dir $doc_dir/$action;
    }

    include fastcgi_params;
    fastcgi_connect_timeout 3;
    fastcgi_send_timeout 25;
    fastcgi_read_timeout 25;
    fastcgi_intercept_errors on;
    fastcgi_param DOCUMENT_ROOT $script_dir;
    fastcgi_param SCRIPT_NAME $script;
    fastcgi_param SCRIPT_FILENAME $script_dir/$script;
    fastcgi_param PATH_INFO $parameters;
    fastcgi_param QUERY_STRING $parameters;
    fastcgi_param REQUEST_URI $request_uri;
    fastcgi_param REQUEST_METHOD $request_method;
    fastcgi_param CONTENT_TYPE $content_type;
    fastcgi_param CONTENT_LENGTH $content_length;
    fastcgi_param GATEWAY_INTERFACE CGI/1.1;
    fastcgi_param REMOTE_ADDR $remote_addr;

    if ($prod_server = "web1") {
            fastcgi_pass web1.kaufmich.com.cgi;
            break;
    }
    if ($prod_server = "web2") {
            fastcgi_pass web2.kaufmich.com.cgi;
            break;
    }
    if ($prod_server = "web3") {
            fastcgi_pass web3.kaufmich.com.cgi;
            break;
    }

    # else

    return 501;

    }

```

}

---

<div class="post-metadata">

**Author:** ![Mauro\_Stettler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/898d66/32.png) [@Mauro\_Stettler](https://www.ruby-forum.com/u/Mauro_Stettler)\
**Post date:** [August 4, 2011, 8:29am UTC](https://www.ruby-forum.com/t/nginx-tries-to-allocate-a-huge-number-of-memory-and-fails/210281/2 "2011-08-04T08:29:56Z")

</div>

Hello!

On Thu, Aug 04, 2011 at 11:47:08AM +0800, Mauro Stettler wrote:

> 18446744073709545066 bytes failed (12: Cannot allocate memory),  
> client: 192.168.1.195, server: \*.kaufmich.vpn, request: “GET  
> /web1/apc/flushApc.php HTTP/1.1”, host: “static.kaufmich.vpn”
> 
> does anybody have an idea why that would happen? i thought this almost  
> has to be a bug because the amount of bytes it is trying to allocate  
> is just so huge it just cant be right.

[…]

> # nginx -V
> 
> nginx version: nginx/0.7.65

[…]

> ```
> if ($request_uri ~* /(.+)/(.+\.php)) {
> set $prod_server $1;
> set $action $2;
> set $script $3;
> set $parameters "";
> 
> ```

This is a problem with using unitialized $n variables. Note the  
above regexp has only 2 captures but following “set” uses variable  
“$3”.

This problem is fixed in 0.8.25+. Please upgrade.

Maxim D.

---

<div class="post-metadata">

**Author:** ![Mauro\_Stettler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/898d66/32.png) [@Mauro\_Stettler](https://www.ruby-forum.com/u/Mauro_Stettler)\
**Post date:** [August 4, 2011, 8:47am UTC](https://www.ruby-forum.com/t/nginx-tries-to-allocate-a-huge-number-of-memory-and-fails/210281/3 "2011-08-04T08:47:50Z")

</div>

thanks a lot for the helpful answer maxim,

i will try that.

mauro
