Monitoring nginx Server Statistics With rrdtool

Posted in Nginx on March 8, 2008 by linxfo

This script is very easy to install and configure. You can use following steps to get pretty stats graph for your nginx-powered server:

  • Modify your nginx config file and add following location to it:
http {

server {
listen SOME.IP.ADD.RESS;

location /nginx_status {
stub_status on;
access_log   off;
allow SOME.IP.ADD.RESS;
deny all;
}

}

}
 * Test your config file with following command:
 # /your/installation/sbin/nginx -t
2006/04/29 04:24:36 [info] 31676#0: the configuration file /opt/nginx/conf/nginx.conf syntax is ok
2006/04/29 04:24:36 [info] 31676#0: the configuration file /opt/nginx/conf/nginx.conf was tested successfully
#
If you will get error about stub_status directive, check your nginx – it should be configured with —-with-http_stub_status_module option.

  • If your configuration is OK, you can restart nginx and test it:
  •  # GET http://your-domain.com/nginx_status
    Active connections: 1492
    server accepts handled requests
    2124355 2124355 8278635
    Reading: 6 Writing: 405 Waiting: 1081
     Download  perl script: rrd_nginx.pl and make it executable with
    # chmod +x rrd_nginx.pl
     Change settings in rrd_nginx.pl to let script know where it will save rrd-base and images:
     #!/usr/bin/perl
    use RRDs;
    use LWP::UserAgent;

    # define location of rrdtool databases
    my $rrd = ‘/opt/rrd’;
    # define location of images
    my $img = ‘/opt/rrd/html’;
    # define your nginx stats URL
    my $URL = “http://your-domain.com/nginx_status”;

     Last step is to insert following string to /etc/crontab file and to restart cron daemon:
    * *     * * *   root    /some/path/rrd_nginx.pl  When all preparations will be finished, you get  images in your $img directory: ---------------------  Quoted from blog.kovyrin.net

    Nginx HTTP(S) Reverse Proxy/Web Server

    Posted in Nginx on March 8, 2008 by linxfo

    This post with list of typical nginx configurations and example configuration snippets for these configurations.

    All sample configuration files are tested on up to date version of nginx, which has been compiled and installed with following commands:

    # ./configure –prefix=/usr/local/nginx –with-http_ssl_module
    # make
    # make install

    Using nginx as web server with PHP support

    If you need to use nginx with PHP, you can setup PHP as FastCGI and let nginx to forward all PHP queries to some FastCGI port (tcp/socket). To use this configuration you need to start PHP as FastCGI using some third party software like spawn-fcgi from lighttpd

    To enable PHP support, you need to add special location section to your config file:

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
    fastcgi_pass   127.0.0.1:12345;
    fastcgi_index  index.php;

    fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
    fastcgi_param  QUERY_STRING     $query_string;
    fastcgi_param  REQUEST_METHOD   $request_method;
    fastcgi_param  CONTENT_TYPE     $content_type;
    fastcgi_param  CONTENT_LENGTH   $content_length;
    }

    ——————-
    Using nginx as web server with SSI support

    Server-Side Includes (aka SSI) is another interesting feature of nginx. As for now, following ssi instructions are supported: config, echo, if, include, set.

    SSI support can be anabled by single line configuration command in your config file:

    location / {
    ssi  on;

    }
    Using nginx as https-enabled web server

    You need https-access to your Nginx-powered site? No problems! Nginx supports https and can be used to implement secured web-server with SSLv2, SSLv3 or TLSv1.

    To enable https-support you should have certificate and key files. How to obtain them, you can read in SSL FAQ. When you will obtain them, you can enable ssl-module:

    server {
    listen               443;
    ssl                  on;
    ssl_certificate      /usr/local/nginx/conf/cert.pem;
    ssl_certificate_key  /usr/local/nginx/conf/cert.key;
    keepalive_timeout    70;

    }
    Using nginx as reverse-proxy server before some another web-server

    If you have some large web-site and you have noticed, that your Apache can not handle more load, you can put nginx before your primary web-server to use it as light reverse-proxy and as web-server to handle requests to static files.

    Thanks to nginx flexibility, you can pass any types of requests to backend server by using location sections (all files, only dynamic content requests or some specific locations in your web-server tree):

    location / {
    proxy_pass        http://localhost:8000/;
    proxy_set_header  X-Real-IP  $remote_addr;
    }
    Using nginx for virtual hosting platforms

    One of the interesting use cases for Nginx is virtual hosting platform because it meets all requirements for good hosting server: it is efficient, it supports all popular virtual hosting methods and it has very good internal structure, so it can be easily extended in for any specific areas.

    As for now, it is being used by many hosting companies as reverse proxy and I am using it on my free hosting service with millions unique visitors per day.

    If you vant to try virtual hosting feature, you can create additional server sections in your config file (first section will be default):

    http {
    server {
    listen  192.168.10.1;
    listen  192.168.10.1:8000;

    server_name   one.example.com  www.one.example.com;

    }

    server {
    listen  192.168.10.1;
    listen  192.168.10.2:8000;
    listen  9000;

    server_name   two.example.com  www.two.example.com
    three.example.com  www.three.example.com;

    }

    server {
    listen  9000;

    server_name   four.example.com  www.four.example.com;

    }
    }

     Quoted from
    This post with list of typical nginx configurations and example configuration snippets for these configurations.

    All sample configuration files are tested on up to date version of nginx, which has been compiled and installed with following commands:

    # ./configure –prefix=/usr/local/nginx –with-http_ssl_module
    # make
    # make install

    Using nginx as web server with PHP support

    If you need to use nginx with PHP, you can setup PHP as FastCGI and let nginx to forward all PHP queries to some FastCGI port (tcp/socket). To use this configuration you need to start PHP as FastCGI using some third party software like spawn-fcgi from lighttpd

    To enable PHP support, you need to add special location section to your config file:

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
    fastcgi_pass   127.0.0.1:12345;
    fastcgi_index  index.php;

    fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
    fastcgi_param  QUERY_STRING     $query_string;
    fastcgi_param  REQUEST_METHOD   $request_method;
    fastcgi_param  CONTENT_TYPE     $content_type;
    fastcgi_param  CONTENT_LENGTH   $content_length;
    }

    ——————-
    Using nginx as web server with SSI support

    Server-Side Includes (aka SSI) is another interesting feature of nginx. As for now, following ssi instructions are supported: config, echo, if, include, set.

    SSI support can be anabled by single line configuration command in your config file:

    location / {
    ssi  on;

    }
    Using nginx as https-enabled web server

    You need https-access to your Nginx-powered site? No problems! Nginx supports https and can be used to implement secured web-server with SSLv2, SSLv3 or TLSv1.

    To enable https-support you should have certificate and key files. How to obtain them, you can read in SSL FAQ. When you will obtain them, you can enable ssl-module:

    server {
    listen               443;
    ssl                  on;
    ssl_certificate      /usr/local/nginx/conf/cert.pem;
    ssl_certificate_key  /usr/local/nginx/conf/cert.key;
    keepalive_timeout    70;

    }
    Using nginx as reverse-proxy server before some another web-server

    If you have some large web-site and you have noticed, that your Apache can not handle more load, you can put nginx before your primary web-server to use it as light reverse-proxy and as web-server to handle requests to static files.

    Thanks to nginx flexibility, you can pass any types of requests to backend server by using location sections (all files, only dynamic content requests or some specific locations in your web-server tree):

    location / {
    proxy_pass        http://localhost:8000/;
    proxy_set_header  X-Real-IP  $remote_addr;
    }
    Using nginx for virtual hosting platforms

    One of the interesting use cases for Nginx is virtual hosting platform because it meets all requirements for good hosting server: it is efficient, it supports all popular virtual hosting methods and it has very good internal structure, so it can be easily extended in for any specific areas.

    As for now, it is being used by many hosting companies as reverse proxy and I am using it on my free hosting service with millions unique visitors per day.

    If you vant to try virtual hosting feature, you can create additional server sections in your config file (first section will be default):

    http {
    server {
    listen  192.168.10.1;
    listen  192.168.10.1:8000;

    server_name   one.example.com  www.one.example.com;

    }

    server {
    listen  192.168.10.1;
    listen  192.168.10.2:8000;
    listen  9000;

    server_name   two.example.com  www.two.example.com
    three.example.com  www.three.example.com;

    }

    server {
    listen  9000;

    server_name   four.example.com  www.four.example.com;

    }

    )
     Quoted from blog.kovyrin.net

    HOWTO: Install WordPress on Nginx

    Posted in Nginx on March 8, 2008 by linxfo

    Apache is a very well-establish web server that can handle just about any situation. Unfortunately, that flexibility comes at the cost of size and relatively high demands on server resources. Nginx (“engine x”) is a lightweight web server/reverse proxy that is very efficient and perfect for hosting WordPress. Read on to see how that can be done…

    Step One: FastCGI

    First off, Nginx does not provide FastCGI for you (FastCGI is what your web server uses to interact with WordPress’s PHP code), so you’ve got to have a way to spawn your own FastCGI processes. My preferred method is to use the spawn-fcgi program provided by the web server lighttpd. You can use PHP’s built-in FastCGI manager php-cgi to do the same thing, but it’s not as straight-forward. Plus, if you learn how to use spawn-fcgi, you can easily adapt it for use with other web applications requiring FastCGI.

    Install spawn-fcgi

    To download and install spawn-fcgi, run the following commands. Don’t worry, all of the building happens in your current directory…nothing else will be installed on your machine.

    $ wget http://www.lighttpd.net/download/lighttpd-1.4.18.tar.bz2
    $ tar -xvjf lighttpd-1.4.18.tar.bz2
    $ cd lighttpd-1.4.18/
    $ ./configure
    $ make
    $ sudo cp src/spawn-fcgi /usr/bin/spawn-fcgi  After spawn-fcgi has been copied to the desired location, you can safely remove the build directory and original source file:    $ cd ..
    $ rm -rf lighttpd-1.4.18/
    $ rm lighttpd-1.4.18.tar.bz2

    Run spawn-fcgi

    This part will be fairly distribution-specific, but I’ll provide the basic command that you’ll need. What you want to do is find a way to run this command as part of your init scripts so the processes will be spawned automatically when you reboot your server.

    /usr/bin/spawn-fcgi -f /usr/bin/php-cgi -a 127.0.0.1 -p 53217 -P /var/run/fastcgi-php.pid

    • -f → the filename of the fcgi-application; in our case we want “php-cgi”, which is provided by your distribution’s PHP package. If you don’t know where to find it, try running which php-cgi on the command line.
    • -a → the IP address to bind the processes to; in our case we want the localhost
    • -p → the port number to bind the processes to; pick whatever you want that won’t cause a conflict (technically it would be best to pick a random number between 49152 and 65535), just make sure to remember the number and use that same port for your Nginx configuration file later on
    • -P → the location where to save the process id file; you can use this file to easily kill the processes later

    For better security, you can also spawn the processes as a non-privileged user by specifying the user/group with the -u and -g flags respectively. For more information on all the available options, run spawn-fcgi -h on the command line.

    If you’re interested in seeing the complete init script that I wrote for use with Arch Linux, you can download it here: fastcgi-php

    Step Two: Complete 83.33% of the Famous 5-Minute Install

    Next you should download the WordPress files and extract them to their final location on your server. Simply follow steps 1–5 of the Famous 5-Minute Install (the 6th and final step requires that your web server be up and running properly, so we’ll do it later). This guide will assume that you extracted the WordPress core files here: /srv/www/nginx/domain.com/

    Step Three: Nginx Configuration

    To get the web server up and running properly, the file you need to edit is called “nginx.conf” and is installed in different places depending on your Linux distribution. If you install Nginx from source, the default location is /usr/local/nginx/conf/nginx.conf, however yours may be somewhere else.

    Once you find that file, open it with your favorite text editor and add a server declaration that looks something like this (I’ll cover what each part means after posting the code):

    server {
        listen       12.34.56.78:80;  # your server's public IP address
        server_name  domain.com;      # your domain name
    
        location / {
            root   /srv/www/nginx/domain.com;  # absolute path to your WordPress installation
            index  index.php index.html index.htm;
    
            # this serves static files that exist without running other rewrite tests
            if (-f $request_filename) {
                expires 30d;
                break;
            }
    
            # this sends all non-existing file or directory requests to index.php
            if (!-e $request_filename) {
                rewrite ^(.+)$ /index.php?q=$1 last;
            }
        }
    
        location ~ \.php$ {
            fastcgi_pass   localhost:53217;  # port where FastCGI processes were spawned
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME    /srv/www/nginx/domain.com$fastcgi_script_name;  # same path as above
    
            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;
    
            # required if PHP was built with --enable-force-cgi-redirect
            fastcgi_param  REDIRECT_STATUS    200;
        } }

    You will need to edit all of the highlighted sections above using your own information. The first part is merely the server declaration where you define what your server’s publicly available IP address is and what domain name that address is associated with.

    Next we add to that some default settings for the root location. The key part here is that WordPress uses the “Front Controller” design pattern, meaning that any request for a file that does not exist on the server should be handled by the main index.php file. To do this, we need an appropriate set of rewrite rules pointing to the proper path of our installation.

    Last, we add one more location block that tells Nginx to dynamically forward PHP requests to the FastCGI processes we spawned earlier. That’s it!

    Step Four: Finishing Up

    Everything should be good to go…all you need to do now is start your Nginx server process (another distribution specific command), then complete the 6th step of the Famous 5-Minute Install and you should have WordPress up and running on Nginx!

    
    
    
    
    

    Quoted from elasticdog.com

    Ruby : Timeout code execution

    Posted in Ruby on Rails on March 7, 2008 by linxfo

    Just a small tip, if you wish to ensure a snippet of Ruby code doesn’t run for too long you can use the timeout function. You might want to do this when making a request to a remote server with net/http for example.

    timeout.rb

    A way of performing a potentially long-running operation in a thread, and terminating it‘s execution if it hasn‘t finished within fixed amount of time.

    Here’s a quick example using the excellent rFeedParser (Universal Feed Parser in Ruby) to fetch an RSS feed.

    require 'timeout'
    require 'zlib'
    require 'rubygems'
    require 'rfeedparser'
    
    fp = nil
    begin
      # Don't take longer than 20 seconds to retrieve & parse an RSS feed
      Timeout::timeout(20) do
        fp = FeedParser.parse("http://feeds.feedburner.com/slashdotdash")
      end
    rescue Timeout::Error
      # Too slow!! end   Quoted  from slashdotdash.net

    GIT Cheatsheet

    Posted in Ruby on Rails on March 7, 2008 by linxfo
    Setup
    -----
    
    git clone <repo>
      clone the repository specified by <repo>; this is similar to "checkout" in
      some other version control systems such as Subversion and CVS
    
    Who doesn't like colors?  Optionally add the following to your ~/.gitconfig
    file:
    
      [color]
        branch = auto
        diff = auto
        status = auto
      [color "branch"]
        current = yellow reverse
        local = yellow
        remote = green
      [color "diff"]
        meta = yellow bold
        frag = magenta bold
        old = red bold
        new = green bold
      [color "status"]
        added = yellow
        changed = green
        untracked = cyan
    
    Configuration
    -------------
    
    git config user.email johndoe@example.com
      Sets your email for commit messages.
    
    git config user.name 'John Doe'
      Sets your name for commit messages.
    
    git config branch.autosetupmerge true
      Tells git-branch and git-checkout to setup new branches so that git-pull(1)
      will appropriately merge from that remote branch.  Recommended.  Without this,
      you will have to add --track to your branch command or manually merge remote
      tracking branches with "fetch" and then "merge".
    
    You can add "--global" after "git config" to any of these commands to make it
    apply to all git repos (writes to ~/.gitconfig).
    
    Info
    ----
    
    git diff
      show a diff of the changes made since your last commit
    
    git status
      show files added to the index, files with changes, and untracked files
    
    git log
      show recent commits, most recent on top
    
    git show <rev>
      show the changeset (diff) of a commit specified by <rev>, which can be any
      SHA1 commit ID, branch name, or tag
    
    git blame <file>
      show who authored each line in <file>
    
    git blame <file> <rev>
      show who authored each line in <file> as of <rev> (allows blame to go back in
      time)
    
    Adding / Deleting
    -----------------
    
    git add <file1> <file2> ...
      add <file1>, <file2>, etc... to the project
    
    git add <dir>
      add all files under directory <dir> to the project, including subdirectories
    
    git add .
      add all files under the current directory to the project
    
    git rm <file1> <file2> ...
      remove <file1>, <file2>, etc... from the project
    
    Committing
    ----------
    
    git commit <file1> <file2> ... [-m <msg>]
      commit <file1>, <file2>, etc..., optionally using commit message <msg>,
      otherwise opening your editor to let you type a commit message
    
    git commit -a [-m <msg>]
      commit all files changed since your last commit, optionally using commit
      message <msg>
    
    git commit -v [-m <msg>]
      commit vebosely, i.e. includes the diff of the contents being committed in the
      commit message screen
    
    Sharing
    -------
    
    git pull
      update the current branch with changes from the server.  Note: .git/config
      must have a [branch "some_name"] section for the current branch.  Git 1.5.3
      and above adds this automatically.
    
    git push
      update the server with your commits across all branches that are *COMMON*
      between your local copy and the server.  Local branches that were never pushed
      to the server in the first place are not shared.
    
    git push origin <branch>
      update the server with your commits made to <branch> since your last push.
      This is always *required* for new branches that you wish to share.  After the
      first explicity push, "git push" by itself is sufficient.
    
    Branching
    ---------
    
    git branch
      list all local branches
    
    git branch -r
      list all remote branches
    
    git branch -a
      list all local and remote branches
    
    git branch <branch>
      create a new branch named <branch>, referencing the same point in history as
      the current branch
    
    git branch <branch> <start-point>
      create a new branch named <branch>, referencing <start-point>, which may be
      specified any way you like, including using a branch name or a tag name
    
    git branch --track <branch> <remote-branch>
      create a tracking branch. Will push/pull changes to/from another repository.
      Example: git branch --track experimental origin/experimental
    
    git branch -r -d <remote branch>
      delete a "local remote" branch, used to delete a tracking branch.
    Example: git branch -r -d wycats/master
    
    git branch -d <branch>
      delete the branch <branch>; if the branch you are deleting points to a commit
      which is not reachable from the current branch, this command will fail with a
      warning.
    
    git branch -D <branch>
      even if the branch points to a commit not reachable from the current branch,
      you may know that that commit is still reachable from some other branch or
      tag. In that case it is safe to use this command to force git to delete the
      branch.
    
    git checkout <branch>
      make the current branch <branch>, updating the working directory to reflect
      the version referenced by <branch>
    
    git checkout -b <new> <start-point>
      create a new branch <new> referencing <start-point>, and check it out.
    
    git remote add <branch> <remote branch>
      adds a remote branch to your git config. Can be then fetched locally.
    Example: git remote add coreteam git://github.com/wycats/merb-plugins.git
    
    git push <repository> :heads/<branch>
      removes a branch from a remote repository. Example: git push origin
      :refs/old_branch_to_be_deleted
    
    Merging
    -------
    
    git merge <branch>
      merge branch <branch> into the current branch; this command is idempotent and
      can be run as many times as needed to keep the current branch up-to-date with
      changes in <branch>
    
    git merge <branch> --no-commit
      merge branch <branch> into the current branch, but do not autocommit the
      result; allows you to make further tweaks
    
    git merge <branch> -s ours
      merge branch <branch> into the current branch, but in the case of any
      conflicts, the files in the current branch win.
    
    Conflicts
    ---------
    
    If merging resulted in conflicts in file(s) <file1>, <file2>, etc..., resolve
    the conflict(s) manually and then do:
    
      git add <file1> <file2> ...
      git commit -a  
    
    Reverting
    ---------
    
    git revert <rev>
      reverse commit specified by <rev> and commit the result.  This does *not* do
      the same thing as similarly named commands in other VCS's such as "svn revert"
      or "bzr revert", see below
    
    git checkout <file>
      re-checkout <file>, overwriting any local changes
    
    git checkout .
      re-checkout all files, overwriting any local changes.  This is most similar to
      "svn revert" if you're used to Subversion commands
    
    Undo
    ----
    
    git reset --hard
      abandon everything since your last commit; this command can be DANGEROUS.  If
      merging has resulted in conflicts and you'd like to just forget about the
      merge, this command will do that
    
    git reset --hard ORIG_HEAD
      undo your most recent *successful* merge *and* any changes that occurred
      after.  Useful for forgetting about the merge you just did.  If there are
      conflicts (the merge was not successful), use "git reset --hard" (above)
      instead.
    
    git reset --soft HEAD^
      undo your last commit
    
    
    ------------------
    Quoted from  errtheblog.com

    Rails performance – using YSlow

    Posted in Ruby on Rails on March 7, 2008 by linxfo

    YSlow from Yahoo! is a Firefox add-on to analyse web pages and tell you why they’re slow based on rules for high performance web sites. YSlow requires the indispensable Firebug extension.

    The 13 rules YSlow checks your site against are as follows:

    ———————————-

    1. Make Fewer HTTP Requests
    2. Use a Content Delivery Network
    3. Add an Expires Header
    4. Gzip Components
    5. Put CSS at the Top
    6. Move Scripts to the Bottom
    7. Avoid CSS Expressions
    8. Make JavaScript and CSS External
    9. Reduce DNS Lookups
    10. Minify JavaScript
    11. Avoid Redirects
    12. Remove Duplicate Scripts 13. Configure ETags -------------  This post will demonstrate that most of these are easily achievable for
    a Rails website through a combination of plugins and with correct
    configuration of a proxy web server (in front of a mongrel cluster) –
    in this case Nginx. This guide follows experience with improving performance for trawlr.com (an online RSS reader).

    ———————–

    Make Fewer HTTP Requests, Minify JavaScript, Put CSS at the Top, Move Scripts to the Bottom, Remove Duplicate Scripts

    The easiest way to make fewer HTTP requests is to combine all JavaScript and CSS files into one. The asset packager plugin does exactly this, plus it will also compress the source files (in production mode) and correctly handles caching (without query string parameters).

    Moving CSS to the top (within the head section) and moving JavaScript to the bottom of the page are both manual tasks that should be done in the layout templates (such as app/views/layouts/application.rhtml). Remember to use stylesheet_link_merged :base and javascript_include_merged :base rather than the default Rails helpers.

    By using asset packager you can also verify that scripts are only included once – another performance hit otherwise!

    Excluding the Google analytics JavaScript file, trawlr.com now uses a single css and js file (including the entire prototype library). Note: You may need to add a missing semi-colon as per this defect for prototype to work correctly.

    Asset Packager can be included as part of a Capistrano deployment with the following recipe:

    desc "Compress JavaScript and CSS files using asset_packager"
    task :after_update_code, :roles => [:web] do
      run <<-EOF
        cd #{release_path} &&
        rake RAILS_ENV=production asset:packager:build_all
      EOF

    Add an Expires Header

    A first-time visitor to your page may have to make several HTTP requests, but by using the Expires header you make those components cacheable. This avoids unnecessary HTTP requests on subsequent page views. Expires headers are most often used with images, but they should be used on all components including scripts, stylesheets, and Flash components.

    Nginx allows adding arbitrary HTTP headers via the expire and add_header directives. Adding the expires header to static content is done with a regular expression looking for relevant file extensions in the request URL. This example uses the maximum expiry date but could be set to more appropriate values as required (e.g. 24h, 7d, 1M)

    # Add expires header for static content
    location ~* \.(js|css|jpg|jpeg|gif|png)$ {
      if (-f $request_filename) {
            expires      max;
        break;
      }

    Gzip Components

    Nginx can gzip any responses – including those proxied from a mongrel cluster.

    gzip on;
    gzip_min_length  1100;
    gzip_buffers     4 8k;
    gzip_proxied any;
    gzip_types  text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
    }
    end

    Rails searching with Sphinx

    Posted in Ruby on Rails on March 7, 2008 by linxfo
    ------------  $ wget http://www.sphinxsearch.com/downloads/sphinx-0.9.7.tar.gz
    $ tar xvzf sphinx-0.9.7.tar.gz
    $ cd sphinx-0.9.7
    $ ./configure --with-mysql-includes=/opt/local/include/mysql5/mysql/ --with-mysql-libs=/opt/local/lib/mysql5/mysql/
    $ make
    $ sudo make install ------------------  $ rake sphinx:index
    $ rake sphinx:start  $ time rake sphinx:index
    
    using config file 'sphinx.conf'...
    indexing index 'items'...
    collected 1455733 docs, 1255.2 MB
    sorted 182.4 Mhits, 100.0% done
    total 1455733 docs, 1255246639 bytes
    total 438.695 sec, 2861316.50 bytes/sec, 3318.32 docs/sec
    
    real    7m25.307s
    user    4m28.963s
    sys     0m17.578s    ------------------ Searching with acts_as_sphinx via the console (ruby script/console) for the term ‘Google’, sorted by published date.    ------------ >> search = Item.find_with_sphinx 'Google', :sphinx => {:sort_mode => [:attr_desc, 'pub_date'], :page => 1}, :o rder => 'items.pub_date DESC'; 0
    => 0
    >> search.total
    => 1000
    >> search.total_found
    => 73717
    >> search.time
    => "0.000"   -------- Within the Rails controller, search is done via:---  @items = Item.find_with_sphinx(params[:query],
          :sphinx => {:sort_mode => [:attr_desc, 'pub_date'], :limit => 50, :page => (params[:page] || 1)},
          :o rder => 'items.pub_date DESC') -------------

    Updating the Sphinx index

    There’s another rake task for updating the Sphinx index which can be called via a cron job, rather than ‘live’ updates. The rotate command allows the index to be rebuilt whilst the Sphinx daemon is running, forcing a restart once completed.

    $ rake sphinx:rotate   -------------------   Quoted from  slashdotdash.net/

    Slow query Tracing

    Posted in Ruby on Rails on March 5, 2008 by linxfo

    rake query_trace

    QueryTrace is a great Rails plugin (which I learned about from ErrTheBlog) for pinpointing where in your Rails application that slow query is running. Once you have it installed, your logs won’t just tell you that you have a problem, they will pinpoint the exact location of that problem for you. This is invaluable when doing load & performance testing or just trying to understand what the hell ActiveRecord is doing.

    Unfortunately, even though it only logs query traces in DEBUG log mode, it still clutters up your test and development logs, and actually slows things down a bit too. So you don’t want to leave it in your project’s vendor/plugins directory after you’re done using it.

    ————-

    rake query_trace:on                     # Enables the query_trace plugin. Must restart server to take effect.
    rake query_trace:off                    # Disables the query_trace plugin. Must restart server to take effect. ---------  The "on" task actually checks out the plugin from query_trace's
    subversion repository, makes a tarball in vendor/query_cache.tar.gz,
    then leaves the tarball around so it doesn't have to keep going to the
    network all the time. You can even check the tarball in to your project
    and it'll never go to the query_trace repository again. The "off" task
    just removes the whole vendor/plugins/query_cache directory.  ------------------- namespace :query_trace do
      desc "Enables the query_trace plugin. Must restart server to take effect."
      task :o n => :environment do
        unless File.exist?("#{RAILS_ROOT}/vendor/query_trace.tar.gz")
          Dir.chdir("#{RAILS_ROOT}/vendor") do
            url = "https://terralien.devguard.com/svn/projects/plugins/query_trace"
            puts "Loading query_trace from #{url}..."
            system "svn co #{url} query_trace"
            system "tar zcf query_trace.tar.gz --exclude=.svn query_trace"
            FileUtils.rm_rf("query_trace")
          end
        end
        Dir.chdir("#{RAILS_ROOT}/vendor/plugins") do
          system "tar zxf ../query_trace.tar.gz query_trace"
        end
        puts "QueryTrace plugin enabled. Must restart server to take effect."
      end
    
      desc "Disables the query_trace plugin. Must restart server to take effect."
      task :o ff => :environment do
        FileUtils.rm_rf("#{RAILS_ROOT}/vendor/plugins/query_trace")
        puts "QueryTrace plugin disabled. Must restart server to take effect."
      end end  ------------------  Quoted from  pivots.pivotallabs.com

    Mounted State of VPS

    Posted in Virtuozzo on March 2, 2008 by linxfo

    If your VPS is going into Mounted state, please check the following:

    1. Virtuozzo license. If you exceeded maximum amount of running VPSs defined in the license, VPSs over that amount will be stopped in 5 minutes. Please check the following article for more information about Virtuozzo licenses.

    2. Shutdown inside a VPS. If a VPS was shut down by its owner (i.e. using /sbin/halt, /sbin/shutdown -h, etc.), its state will be Mounted. Just start a VPS using
    # vzctl start 101

    or using Virtuozzo Control Center / Power Panel.

    3. A VPS could be created with a wrong license class (class 1, so-called ‘Light VPS’). This class is obsoleted. You should recreate a VPS using license class 2. To check which license class is assigned to VPS 101, use
    # grep CLASSID /etc/sysconfig/vz-scripts/101.conf

    Perform some action for all VPSes on the hardware node

    Posted in Virtuozzo on March 2, 2008 by linxfo

    vzlist -o veid -H

    # for vps in `vzlist -o veid -H` do
    vzctl exec $vps ps ax
    done