No registered users in community Developer XoWIKI
in last 10 minutes

V.1.4 Using Nginx as an accelerating proxy

Assume you have the (pretty normal) situation on a busy site that you have a lot of files lying around in a resources directory. These static files usually clutter your webservers connections and let it slow down considerably sometimes. Also, any access statistics will get seriously out of whack.

There are two methods which you can use. One is doing proxy caching and is a tiny bit slower, but less work to setup. The other thing to do here is to use the location assertion and let it access all static files locally from within nginx.

In some circumstances this results in CSS files not being used by Firefox...

Proxy caching 

Proxy caching only works after version 0.5.32 and relies on the proxy_store directive being present. Proxy_store is a mirror on demand in that it will try to find the file locally and if present serve it from there. If it is not present locally though it will query the backend server and ask for the file, save it locally and then serve it. From now on every request to the file will be send from within NGINX and not result in a request to the backend server.

The best way to learn about proxy_store is the wiki at http://wiki.codemongers.com/NginxHttpProxyModule#proxy_store. Translated into OpenACS this looks as follows:

       location /resources/ {
           root                 /var/lib/aolserver/resources/SERVER0;
           error_page           404 = /fetch$uri;
       }

        location /fetch {
            internal;

            proxy_pass           http://127.0.0.1:80;
            proxy_store          on;
            proxy_store_access   user:rw  group:rw  all:r;
            proxy_temp_path      /var/lib/aolserver/resources/SERVER0/cache;

            alias                 /var/lib/aolserver/resources/SERVER0;
        }
This will (as in the example below) store the mirrored file in /var/lib/aolserver/resources/SERVER0/resources (as the root is the location and /resources get's appended to it). Important for fetch is that the alias matches the root.

Location assertion 

First thing to do is link the files from the package_key/www/resources directory to a separate directory, lets say /web/resources/SERVICE0/resources (it is mandatory that the directory is called "resources", otherwise the location rule wont work).

As this can become very tiring the following script should speed up the process. This is based upon work from Hector at UNED:

#!/bin/sh
if [ $# -lt 1 ]; then
        echo "* Usage: `basename $0` instance name (SERVICE0)"
        exit 1
fi

# Translate into OpenACS standard usage of SERVICE0
SERVICE0="$1"
AOLDIR="/var/lib/aolserver/$SERVICE0"

# Directory for the resources
RESOURCES="/var/lib/aolserver/resources/$SERVICE0/resources"
mkdir -p $RESOURCES

for i in $(ls $AOLDIR/packages/); do
  if [ -e $AOLDIR/packages/$i/www/resources ]; then
   echo -n "* $i"
   # Create the symbolic link to the resources directory
   ln -svfT "$AOLDIR/packages/$i/www/resources" "$RESOURCES/$i";
  fi
done
exit 0
Once this is done the following rule will allow you to access the resources cached from within nginx.

# serve static files for acs-subsite and acs-templating in packages

location ~* /resources/  {
root   /var/lib/aolserver/resources/SERVICE0/resources;
access_log   off;
}

The access_log off allows you to turn of the access log for these requests, after all you do not need it for css files and others.

Sadly, this does not take the special resources locations of /www and /packages/acs-subsite into account. Therefore we need to copy them separately:

cp -pr /web/SERVICE0/www/resources/* /web/resources/SERVICE0/resources
cp -pr /web/SERVICE0/packages/acs-subsite/www/resources/* /web/resources/SERVICE0/resources

Additionally you would want to do the same for the robots.txt. First link it up properly:

ln -s /web/SERVICE0/www/robots.txt /web/resources/SERVICE0/robots.txt 

Then edit the correct location in NGINX
location ~* /robots.txt  {
root   /var/lib/aolserver/resources/SERVICE0/robots.txt;
access_log   off;
}

Not to forget the favicon.ico which is called upon on every single request...:

ln -s /web/SERVICE0/www/favicon.ico /web/resources/SERVICE0/favicon.ico 

Then edit the correct location in NGINX

¬≠location ~* /favicon.ico  {
root   /var/lib/aolserver/resources/SERVICE0/favicon.ico;
expires max;
access_log   off;
}­

 Last but not least do this for the images directory as well

mkdir -p /web/resources/$SERVIC0/shared
ln -s /web/SERVICE0/packages/acs-subsite/shared/images /web/resources/stage/shared/images

Then edit the correct location in NGINX

location ~* /shared/images  {
root   /var/lib/aolserver/resources/SERVICE0/;
access_log   off;
}

Make sure that the location is correct

-- Other random stuff
  -- If want to run behind a reverse proxy, e.g. apache, and 
you're running on something other than port 80, older 
versions of openacs (and newer?), util_current_location 
needs a patch to support the "apparent port" which may be 
different from the port aolserver is listening on:

--- utilities-procs.tcl 6 Nov 2004 05:04:47 -0000       1.1.1.1
+++ utilities-procs.tcl 25 Feb 2008 08:00:03 -0000
@@ -2727,7 +2727,7 @@
     switch [ad_conn driver] {
         nssock {
             set proto http
-            set port [ns_config -int "ns/server/[ns_info server]/module/nssock" Port]
+            set port [ns_config -int "ns/server/[ns_info server]/module/nssock" ApparentPort [ns_config -int "ns/server/[ns_info server]/module/nssock" Port]]
         }
         nsunix {
             set proto http