Malloc() errors - (some type of redirect loop?)

Hi everyone,
I am new to the mailing list and have only been using Nginx a few
months, so please bear with me.

We are currently getting MANY errors such as this:

2010/09/07 17:58:18 [emerg] 8851#0: *4086 malloc() 4294967295 bytes
failed (12: Cannot allocate memory), client: 66.249.71.238, server:
xxxx.net, request: “GET /robots.txt HTTP/1.1”, host: “www.xxxxx.net

These errors started occurring after changes to our site config.

We originally set up Nginx as only a reverse proxy, to forward to other
machines, so nginx.conf is as such:

server {
        listen       80;
        server_name  _;

        location / {
        resolver        192.168.1.1;
        proxy_pass      http://$host$uri;
        proxy_intercept_errors on;
        }

That was working VERY well (thanks to the Nginx developers for that!),
so we decided to add a local config on the gateway machine to serve up
some other stuff:

so this is the rest of current nginx.conf:

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            #root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME
/home/httpd/html$fastcgi_script_name;
            include        /etc/nginx/fastcgi_params;
        }

this is fastcgi_params:

fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

#fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

The site config then was as such:

cat /etc/nginx/sites-enabled/xxxxxx.net

server {

        listen   80;

        server_name xxxxxx.net;


        access_log /home/httpd/log/access.log;
        error_log /home/httpd/log/error.log;


        location /      {
                        root   /home/httpd/html/xxxxxxx.net;
                        index  index.html;
                        }

        location ~ \.php$ {
                          fastcgi_pass   127.0.0.1:9000;
                          fastcgi_index  index.php;
                          fastcgi_param  SCRIPT_FILENAME
/home/httpd/html/xxxxxx.net/$fastcgi_script_name;
                          include        /etc/nginx/fastcgi_params;
                          }
       }

At this point everything was still working fine. (Thanks again Nginx
developers!!)

But we are also running our Webmail, and a small download server, on the
gateway. Url’s such as:
http://www.xxxxxxx.net/webmail and
http://www.xxxxxxxxxx.net/download
proved to be inconvenient for our users, as they were used to the
previous url format such as:
http://webmail.xxxxxxx.net.

We also wanted to group subdomains such as webmail and download into
their own respective independent directories for maintenance and
security reasons. We decided upon a directory hierarchy such as this:

/home/httpd/html
/home/httpd/html/xxxxx.net
/home/httpd/html/xxxxx.net/www
/home/httpd/html/xxxxx.net/webmail
/home/httpd/html/xxxxx.net/download

Since Nginx can use regex to extract the subdomain, we decided to use a
regex if statement in the domain config to set the root directory for
each subdomain. That simplifies the config and makes adding subdomains a
simple matter of adding content. So now our domain config is as such:
cat /etc/nginx/sites-enabled/xxxxxx.net

server {

        listen   80;
        server_name xxxxxxxx.net www.xxxxxxx.net *.xxxxxx.net;

        access_log /home/httpd/log/xxxxxxxxxx.net-access.log;
        error_log /home/httpd/log/xxxxxxxxx.net-error.log;

        # Extracts the subdomain to a variable
        if ($host ~ "^(.*).xxxxxxxx.net") {
          set $sub $1;
          set $domain $2;
        }

        # If the directory doesn't exist then redirect to the main-page
        if (!-d /home/httpd/html/xxxxxxx.net/$sub) {
          rewrite . http://www.xxxxxxxxxx.net/ redirect;
        }

        location /      {

                        root   /home/httpd/html/xxxxxxxxx.net/$sub;
                        index  index.html;

                        }

        location ~ \.php$ {
                          fastcgi_pass   127.0.0.1:9000;
                          fastcgi_index  index.php;
                          fastcgi_param  SCRIPT_FILENAME
/home/httpd/html/xxxxxxxxx.net/$sub$fastcgi_script_name;
                          include        /etc/nginx/fastcgi_params;
                          }
        # redirect 404 server error pages to the static page

        # error_page  404         notfound.htm;


        }

At this point is when we started getting the malloc() errors. Sometimes
the pages will load properly. At other times random images will be
missing on the page. Reloading the page will cause the missing images to
randomly change. Other times a 500 error will occur. The byte size of
the file in the malloc() error will depend upon the file creating the
error, but the size will stay consistent per file and always be in the
MB/GB size range. All files on which the errors occur, including images,
are no more than 10kB. Also the error pages do not work, and that is why
they are commented out.

Since the errors only started occurring only after we made the recent
changes we suspect the problem is due to some type of error in our regex
expressions. I have a hard time getting a good grip on those. So the
regex code we are using was copied from other nginx configs posted on
the Web.

Thanks in advance for any assistance.

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,129739,129739#msg-129739

Hello!

On Sun, Sep 12, 2010 at 10:56:11AM -0400, gnexus wrote:

Hi everyone,
I am new to the mailing list and have only been using Nginx a few
months, so please bear with me.

We are currently getting MANY errors such as this:

2010/09/07 17:58:18 [emerg] 8851#0: *4086 malloc() 4294967295 bytes
failed (12: Cannot allocate memory), client: 66.249.71.238, server:
xxxx.net, request: “GET /robots.txt HTTP/1.1”, host: “www.xxxxx.net

[…]

    # Extracts the subdomain to a variable
    if ($host ~ "^(.*).xxxxxxxx.net") {
      set $sub $1;
      set $domain $2;
    }

You have regexp with one capture, but try to use $2.

Incorrect malloc attempts with such misconfiguration should be
fixed in 0.8.25+.

Maxim D.

Maxim D. Wrote:

    # Extracts the subdomain to a variable
    if ($host ~ "^(.*).xxxxxxxx.net") {
      set $sub $1;
      set $domain $2;
    }

You have regexp with one capture, but try to use
$2.

Thanks for your help!
But could you elaborate a bit more? Please. . .
Like I said before, I’m not too familiar with regex.
We tried using $2 with two captures also, but it did not seem to work
correctly.

It will give it a try again in the interim, however. Maybe I’ll get
lucky. . .
Gnexus

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,129739,129984#msg-129984

gnexus Wrote:

You have regexp with one capture, but try to
use
$2.

We tried using $2 with two captures also, but it
did not seem to work correctly.

Okay, it looks like this was the problem:

set $domain $2;

We initially had that in there to also set the domain directory
automatically. But since the regex was incorrect it did not function
correctly. We took out the code to set the subdirectory by domain, but
since we planned to try and use it again we did not remove that variable
statement. I didn’t realize that it would create a problem if not
removed or commented out. But it certainly did!

Seems to work good now.
I’ll be glad to see the malloc error problem resolved.
That way it will be easier to isolate my buggy code. . .
Thanks!
Gnexus

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,129739,129991#msg-129991

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs