Limit request rate on target server with Nginx

The HttpLimitReqModule provides the capability to limit request rates per session or per client address.  However, it seems this plugin also works for limiting traffic to a specific server.

Here are the configuration in /etc/

limit_req_zone $server_name zone=myzone:10m rate=100r/s;

location / {
proxy_pass http://target_url:8080;
limit_req zone=myzone burst=5000000;
}

 


Running two WordPress installations under one domain

I had the need to maintain two WordPress sites under one domain.  Originally I decided to just have them run in two sub-domains like

  • site1.mysite.com
  • site2.mysite.com

Later it was requested that the two sites to be run under the same domain like

Here are some notes on how that was done.

  1. install two WordPress installations in directories: <home>/www and <home>/www/site2
  2. in the /www/site2/.htaccess, you will change the rewrite rules to

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /site2/
    RewriteRule ^index\.php$ – [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /site2/index.php [L]
    </IfModule>

  3. in addition(assume the old site2.mysite.com is mapped to  <home>/www/oldsite2), add a redirect rule to redirect traffic to old sub-domain.

    RewriteCond %{HTTP_HOST} ^site2\.mysite\.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www\.site2\.mysite\.com$
    RewriteRule ^(.*)$ “http\:\/\/www\.mysite\.com\/site2$1″ [R=301,L]

 

 

 


Visualize module dependencies in a python package

When you are learning a new Python library, it is always helpful to get a graphical view of the dependencies among different modules within the library/package.  Inspired by this post, I created a script to do that.   To use the script,  run

python moddep.py -p <top-level-path-to-package-sources> -o <output-file, default to stdout>

Below is the source code:


Running Node.js on hostmonster

This is a similar post to the previous one that explains how to run tornado on a hostmonster shared server. This time I wanted to try node.js and have to resort to a “proxy” script to redirect the request/response to/from node.js server. It is apparently not acceptable for a real web site, but it works if you just need a playground.

The steps are also similar to setting up tornado.

  1. create a sub-domain via hostmonster’s cpanelThis allows you to map a directory on your host to a separate sub-domain so you don’t have to worry about messing up with your main domain. As an example, I created following mapping:

    nodejs.mydomain.org  –> ~/nodejs
  2. enable the prxoy script in .htaccess  in ~/nodejs

  3. create the proxy PHP file  ~/nodejs/proxy.php

  4. start your node.js on port 3000 and test via http://nodejs.mydomain.org/<anypath&gt;

Running tornado on hostmonster

I own an account on hostmonster.com and it is mainly used for my PHP web site.  In order to run a demo tornado backed up site, I have to resort to FastCGI and tornado‘s WSGI module. Although some of these setups are specific to hostmonster.com, you should be able to apply it to other shared hosting platforms.

Here are the steps to set things up and assume you already have Python and tornado installed in your hostmonster account. It also requires you pre-install flup.

  1. create a sub-domain via hostmonster’s cpanelThis allows you to map a directory on your host to a separate sub-domain so you don’t have to worry about messing up with your main domain. As an example, I created following mapping:tornado.mydomain.org  –> ~/tornado
  2. create a FCGI script called t.fcgi in ~/tornado
  3. enable the FCGI script in .htaccess  in ~/tornado
  4. create the main application module  ~/tornado/myapp/tornadodemo.py

How to enable gzip for Google AdWords in Java client library

Google AdWords API uses SOAP as its messaging format. SOAP can be quite verbose especially when you are dealing with a big account. Fortunately Google provides an option to turn on GZIP to compress the messages.  This is documented here.

If you are using the provided AdWords Java client library, here are the few step you need to do

  1. Set UserAgentto a string that contains “gzip”The Axis client automatically sets the “user agent” to a locale specific resource called “axisUserAgent”. To override this default value, we will create a file “resource_<locale>.properties” and place it in the classpath under “org/apache/axis/i18n/” directory.  Most likely the locale is English and the file name will be “resource_en.properties“.  This file contains the property:axisUserAgent=Axis/1.4 (gzip)

    The important part is that the user agent name contains the value “gzip”.

  2. Enable “client configuration via classpath”Because the client library is built upon Axis, so it can be configured just as a typical Axis application. We now provide a client config file call client-config.wsdd as shown below

  3. Create the custom handler to inject the desired header


Python multiprocessing configuration fix on Centos

The OS I am running is:

2.6.27-chistyakov.1 #1 SMP Tue Dec 29 10:26:29 PST 2009 x86_64 x86_64 x86_64 GNU/Linux

The Python multiprocessing module reported follow errors:

File “/usr/lib/python2.6/multiprocessing/synchronize.py”, line 49, in __init__
sl = self._semlock = _multiprocessing.SemLock(kind, value, maxvalue)
OSError: [Errno 38] Function not implemented

Turned out that we need to enable the SHM on the box. To do that, find the file /etc/fstab, add a new line:

none /dev/shm tmpfs rw,nosuid,nodev,noexec 0 0

Restart the box and the SHM should be enabled now.


Follow

Get every new post delivered to your Inbox.