2

Yesterday I had a working optimized Nginx install serving a website and it was really fast. Today I just did some tests with some extra changes in the config ( just adding a redirect ) and suddenly now the server takes like 7 or 10 seconds to serve the page. Once the request is taken the pages loads very fast but there is a hang of 10 seconds when entering the site and while browsing too. It's my first nginx config with fastcgi and I don't really know what is happening, since I am in debug mode and the only thing I see are info messages at the error log, no errors or alerts at all. All the rewrites you can see down in the code were there yesterday, so I guess this is not the problem. Do you think my code is wrongly formatted ?

I will post here the config and hope that someone can give me a hint on this... thank you.

user  www-data;
worker_processes  2;

error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;
worker_rlimit_nofile 30000;
events {
    worker_connections  4096;
}


http {
    perl_modules perl;
      perl_require JavaScript/Minifier.pm;   
      perl_require CSS/Minifier.pm;
      perl_require JSMinify.pm;
      perl_require CSSMinify.pm;

    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;
    gzip_static on;
    gzip_http_version   1.1;
    gzip_proxied        expired no-cache no-store private auth;
    gzip_disable        "MSIE [1-6]\.";
    gzip_vary           on;

    sendfile        on;
    tcp_nopush     off;
    tcp_nodelay     on;

    #keepalive_timeout  0;
    keepalive_timeout  10;

    gzip  on;
    log_request_speed_filter on;
    log_request_speed_filter_timeout 3;

    server {
        listen       80;
        server_name  mysite.com;

        #charset koi8-r;

        access_log  logs/mysite.access.log;
    error_log   logs/mysite.error.log debug;






    location /nginx_status {
        stub_status on;
        access_log   off;
    }
    location ~ \.js$  {
            perl JSMinify::handler;
        }
    location ~ \.css$ { 
            perl CSSMinify::handler;
    }    

    location / {
                root  /usr/local/nginx/html;
        index index.php;
            rewrite ^/api/?(.*)$ /webservice/dispatcher.php?url=$1 last;
            rewrite ^/([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$1$2$3.jpg last;
            rewrite ^/([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$1$2$3$4.jpg last;
            rewrite ^/([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$1$2$3$4$5.jpg last;
            rewrite ^/([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$1$2$3$4$5$6.jpg last;
            rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$1$2$3$4$5$6$7.jpg last;
            rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$1$2$3$4$5$6$7$8.jpg last;
            rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$7/$1$2$3$4$5$6$7$8$9.jpg last;
            rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$7/$8/$1$2$3$4$5$6$7$8$9$10.jpg last;
            rewrite ^/c/([0-9]+)(\-[\.*_a-zA-Z0-9-]*)(-[0-9]+)?/.+\.jpg$ /img/c/$1$2$3.jpg last;
            rewrite ^/c/([a-zA-Z-]+)(-[0-9]+)?/.+\.jpg$ /img/c/$1$2.jpg last;
            try_files $uri $uri/ /index.php?$args;
        index  index.html index.htm index.php;
                strip on;
    }
    if ($http_user_agent ~* aesop_com_spiderman|alexibot|backweb|bandit|batchftp|bigfoot|black.hole|blackwidow|blowfish|botalot|buddy|builtbottough|bullseye|cheesebot|cherryicker|chinaclaw|collector|                    copier|copyrightcheck|cosmos|crescent|curl|custo|da|diibot|disco|dittospyder|dragonfly|drip|easydl|ebingbong|ecatch|eirgrabber) {
        rewrite ^/ http://mysite.com/robots.txt;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # 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  /usr/local/nginx/html$fastcgi_script_name;
            include        fastcgi_params;
        fastcgi_read_timeout 10;
        fastcgi_send_timeout 10;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

#    server {
#        listen 80;
#        server_name www.mysite.com;
#        rewrite ^ http://www.mysite.com$uri permanent;
#    }
    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443;
    #    server_name  localhost;

    #    ssl                  on;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers   on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}
Chroma Funk
  • 43
  • 1
  • 5
  • Those seem like some pretty interesting rewrites. In any case it would help if you had more information, like the sort of traffic you're getting, logs, server specs, cpu or disk monitoring. this sort of information can help track where your issue lies. – Drew Khoury May 14 '13 at 13:26
  • Hi, thank you ! I am getting no traffic, I'm just testing the server with an online store. Basically is Nginx with PHP5-fcgi and memcached. Then I have a couple of Perl modules minifying the JS and CSS files, being pushed by the nginx perl module. Nothing really fancy at all ... I don't have traffic as I'm doing this to do some tests only. The server is a micro EC2 Amazon instance. But again, yesterday worked and it was really fast. It started to work wrong today and what is happening is a delay when you access the site. Could be something related to the communication between nginx and fcgi ? – Chroma Funk May 14 '13 at 13:37
  • How did you go with this? Did you try my suggestion, at the very least to rule it out? Have you got any answers, or more information to help get to the bottom of this? – Drew Khoury Aug 10 '13 at 02:45

1 Answers1

0

You shouldn't do any sort of performance testing on micro instances, unless of course you're planning on running micro's in production (which I do not recommend).

Micro instances have a throttled CPU, and low I/O performance. Both of these are enough to shy away from using them in production, or worrying about troubleshooting performance issues.

My suggestion is to run some performance tests on a production grade instance type (small is a good start for little traffic). This would be the quickest and most sensible way to rule out the micro instance as the root cause of your intermittent performance issues.

http://aws.amazon.com/ec2/instance-types/

Micro instances (t1.micro) provide a small amount of consistent CPU resources and allow you to increase CPU capacity in short bursts when additional cycles are available. They are well suited for lower throughput applications and web sites that require additional compute cycles periodically

Micro Instance

  • 613 MiB memory
  • Up to 2 EC2 Compute Units (for short periodic bursts)
  • EBS storage only
  • 32-bit or 64-bit platform
  • I/O Performance: Low
  • EBS-Optimized Available: No
  • API name: t1.micro
Drew Khoury
  • 4,637
  • 8
  • 27
  • 28
  • While I agree with you, I highly doubt this could be a good response. The problem is not in the EC2 instance itself, as I have been monitoring the CPU sucking after the optimization and it takes about 0.3% of the tiny single CPU core the instance has for each request served. Actually, the waiting itself is not adding any load to the server ! It only takes CPU cycles when it's processing and serving the content. I want to detect what it is making it hang for 10 seconds before actually start loading ! Thanks for your answer anyway. – Chroma Funk May 14 '13 at 14:37
  • I think there is a good chance the micro is causing or contributing to your performance issues. It's easy to test with another instance type to prove or disprove this theory. My advice still stands, trying to diagnose performance issues on a micro doesn't make much sense. You will only end up doubling up on testing and troubleshooting and wondering if the micro is the cause. If you experience the same on other instance types, you can deal with it then and there. – Drew Khoury May 15 '13 at 07:31
  • I will try on a small instance and report back, but I'm pretty sure that is not the cause. We well see ... – Chroma Funk May 15 '13 at 08:01
  • How did you go? Did you run any further tests? – Drew Khoury Jun 25 '13 at 01:28
  • Yeah, got it working finally. I had to tell the Ecommerce app I was running that I was using Nginx optimization options. Worked like a charm, an EC2 micro can get about 60 users per second no problem with this optimization. If you have a low traffic shop or website, it's completely fine :) Thank you ! – Chroma Funk Aug 23 '13 at 12:22