mod_env – Helicon Tech Blog http://www.helicontech.com/articles Web Server Enhancements Fri, 11 Nov 2011 13:12:12 +0000 en-US hourly 1 https://wordpress.org/?v=5.2.4 Сounting downloads from your site http://www.helicontech.com/articles/counting-downloads-from-your-site/ http://www.helicontech.com/articles/counting-downloads-from-your-site/#comments Mon, 21 Feb 2011 13:48:35 +0000 http://www.helicontech.com/articles/?p=456 Continue reading ]]> If you have some downloadable content on your site (installation packages), you’ll probably want to monitor the statistics of downloads. But the popular Google Analytics system cannot count that as it is working only inside html-pages. Another option is analyzing server logs, but that’s something you’ll probably choose last of all… That’s why we are offering rather a simple way of counting downloads with Helicon Ape.

The steps to be accomplished are:

  • create a table in the database to store the download counters for the products;
  • create an .htaccess in Helicon Ape which will make records into the table upon each download with the help of mod_dbd module;
  • create a simple aspx-page to show downloads statictics.

Preconditions

In the root of your site you have a downloads folder containing files for download (e.g., AudioCoder.msi, AudioDecoder.msi, AdditionalCodes.zip).
You have Helicon Ape installed on your Windows Server 2008 (IIS7).
You have SQL Server that will store the DB with downloads statistics.

Table for storing statistics

Run Microsoft SQL Server Management Studio and connect to your SQL server.

SQL Server Management Studio login window

Right click on ‘Databases’, select ‘Create Database’, enter database name (for example ‘DownloadsCounter’) and click ‘OK’.

Database creation

Now we’ll create the table itself:
Unfold the DownloadsCounter in Object Explorer, right click on ‘Tables’ and select ‘New Table…’. Name the table ‘Downloads’ and add 4 fields: id (int), moment (datetime), filename (nvarchar(50)) and ipaddress (nvarchar(50)) as shown on the pic below. You can find sql script to create table in attached DownloadsCounterExample.zip.

Table creation

Done with the table!

mod_dbd configuration

Start Helicon Ape Manager, select your site from the tree and click on ‘downloads’ folder. Now write the following config in .htaccess on the right.

Helicon Ape Manager

The config instructs mod_dbd to connect to DB, catches data from request (filename and client ip-address) and writes them into the DB.

# Helicon Ape version 3.0.0.59

# Connection settings
DBDriver mssql
DBDParams "Data Source=db2003\MSSQLSERVER2008;Initial Catalog=DownloadsCounter;\
           User ID=sa;Password=123123"

# Save Filename (for .msi & .zip files only)
SetEnvIfNoCase REQUEST_URI ^/downloads/(.+\.(?:msi|zip))$ FileNameENV=$1
SetEnvIfNoCase REMOTE_ADDR ^(.*)$ IpAddrENV=$1

# Sql query to save download event
DBDPrepareSQL "INSERT INTO DownloadsCounter.dbo.Downloads\
    (moment, filename, ipaddress)\
    VALUES (\
        GETDATE(),\
        '%{FileNameENV}e',\
        '%{IpAddrENV}e'\
    )\
" InsertDownload

# Execute sql query if request uri is .msi or .zip file
SetEnvIf request_uri \.(?:msi|zip)$ dbd_execute=InsertDownload

# limit access to statistics page to localhost only
<Files stat.aspx>
Order Deny,Allow
Deny from all
Allow from ::1 127.0.0.1 localhost
</Files>

Save your .htaccess and try to download something from the browser. If you now throw a glimpse at the ‘downloads’ table in SQL Server Management Studio, you’ll see the records appearing in it:

Table

How it works?

Helicon Ape module is processing all requests coming to /downloads/ folder. If .zip or .msi file is requested, Ape memorizes the filename (FileNameENV) and client IP (IpAddrEnv), from which the file was requested, and inserts these data into the table by means of SQL query.

Statistics viewer

The archive attached to the article includes stat.aspx which does a very simple task—shows records from ‘downloads’ table.

Of course, that’s not the sort of viewer you need, as it must be capable of showing statistics by days, months, products etc. But designing a real-life solution is beyond the scope of this article, so feel free to advance stat.aspx by yourself.

Resulting archive

Below is the archive DownloadsCounterExample.zip containing all files from this article. To do some testing just unzip it into the root of your site and replace the database password from ‘123123’ to the one you have in both .htaccess and web.config.

DownloadsCounterExample.zip

Why is this option better?

As we already noticed, Google Analytics and like services are based on JavaScript working on the page. They know nothing about static content and other files loaded from the server.

Another option (yet another extreme) is server logs. They are not always accessible, not always enabled, besides, they require writing of special parsers or analyzers. Moreover, server logs do not provide live (current moment) info. Usually log file is created daily so their analysis is only possible at the beginning of the next day.

Summing up

The method explained above is a quick and effortless way to screw downloads counter to your site using Helicon Ape mod_dbd. The example is easily expendable, e.g. you can add more fields to the table to save Referer, User-Agent, etc., or develop a customizable statistics viewer.

We’ve just given you the basement, now it’s time for you to build a house.

Best regards,
Ruslan—Helicon Tech Team

]]>
http://www.helicontech.com/articles/counting-downloads-from-your-site/feed/ 2
Guide: How to enable mod_gzip compression for IIS7 http://www.helicontech.com/articles/how-to-enable-mod_gzip-compression-for-iis7/ http://www.helicontech.com/articles/how-to-enable-mod_gzip-compression-for-iis7/#comments Thu, 12 Mar 2009 15:21:00 +0000 http://localhost:85/blog/?p=32 Continue reading ]]> Hi all! In this article we will tell you how to enable mod_gzip and make it work the way you want. The basics of the module are recounted in Introduction to mod_gzip. We’ll only remind that mod_gzip is used to compress response before it is sent to the client.

Having Helicon Ape installed, open httpd.conf file in Ape installation folder using Helicon Ape Manager. To be able to manage the module you should uncomment corresponding LoadModule directive, as shown on the picture:

IIS possesses its own facilities for GZIP compression. These must be disabled if you are going to use mod_gzip for these purposes. Helicon Ape has a lot more means for more flexible and fine configuring. To disable internal compression in IIS please run IIS Manager, in the tree on the left choose server or specific site. Now open ‘Compression’ element and put the ticks as shown below:


Now we are ready to start writing a simple config. Let’s enable compression for all requests first. According to the docs this may be accomplished in three ways. We’ll use environment variables:

SetEnv gzip

The above line does the following: enables mod_gzip, allows compression for all types of requests, applies default compression level of 6.

Easy, isn’t it? “But where’s the flexibility you promised?” – you wonder. Not all files may be compressed to the same extent. It’s well-known for instance that text documents are more “compressable” than images. And for PNG, JPEG, GIF formats the benefit may tend to zero! And everything would be just great if the module’s operation didn’t take time. So why waste time to fruitlessly compress almost uncompressable files if we can simply say mod_gzip not to touch them. Let’s use mod_setenvif module to disable images compression:

SetEnv gzip
SetEnvIf mime image/.* no-gzip

Much better this time, right? Yeah, better but not the best:) mod_gzip can do even more! Let’s play with compression level now. The default value is 6 but maximum is 9. And sometimes the difference is noticable. As we know the text is compressed well, let’s set it’s compression level to 9, we’ll still ignore images and everything else will be compressed by default:

SetEnv gzip
SetEnvIf mime text/.* gzip=9
SetEnvIf mime image/.* no-gzip

Another situation may be that you want to compress only specific file types. Say you want only HTML/CSS/JS to be compressed with maximum compression level:

SetEnvIf (mime text/.*) and (mime application/x-javascript) gzip=9

Note! If you previously were an Apache fan, the above syntax may surprise you. Yes, it’s true – it’s applicable only in Helicon Ape (for your own good). Here you may find more information on extended syntax.

But if for some reason you want your Helicon Ape config to be compatible with Apache, here the equivalent to what we’ve done before:

mod_gzip_on yes
mod_gzip_compression_level 9
mod_gzip_item_include mime text/.*
mod_gzip_item_include mime application/x-javascript

And the most delicious dish in the end;) We’ve made some kind of compilation of the most effective mod_gzip and mod_cache settings that you may use on your server as is. mod_cache is used to speed up the delivery of compressed content to the user. These two modules working together substantially improve server performance. So here’s the piece that’ll make you happy:

# By file extension
SetEnvIfNoCase request_uri \.mdb$ gzip=9
SetEnvIfNoCase request_uri \.bmp$ gzip cache-enable=mem
SetEnvIfNoCase request_uri \.(?:jpg|gif|png|swf|avi|rm)$ no-gzip                        

# By MIME type
SetEnvIfNoCase mime text/.* gzip=9 cache-enable=mem
SetEnvIfNoCase mime audio/wav gzip cache-enable=mem
SetEnvIfNoCase mime image/bmp gzip cache-enable=mem
SetEnvIfNoCase mime message/rfc822 gzip

SetEnvIfNoCase mime application/msword gzip
SetEnvIfNoCase mime application/postscript gzip
SetEnvIfNoCase mime application/vnd.ms-excel gzip
SetEnvIfNoCase mime application/vnd.ms-powerpoint gzip
SetEnvIfNoCase mime application/vnd.ms-works gzip
SetEnvIfNoCase mime application/x-javascript gzip cache-enable=mem
SetEnvIfNoCase mime application/x-msaccess gzip
SetEnvIfNoCase mime application/pdf gzip

# Exceptions for old browsers
#BrowserMatchNoCase \bOpera(?:\s5\.|/5) and \
#    mime application/.* no-gzip vary-agent !cache-enable
#BrowserMatchNoCase \bMozilla/4\.[67] and \
#    (mime application/.* or mime image/.*) no-gzip vary-agent !cache-enable
#BrowserMatchNoCase \bNetscape(?:6/6\.|/) and \
#    mime application/.* no-gzip vary-agent !cache-enable#BrowserMatchNoCase \bFirefox/1 and \
#    mime application/pdf no-gzip vary-agent !cache-enable

SetEnvIfNoCase (mime text/css) or (mime image/jpeg) vary-agent
BrowserMatchNoCase \bMSIE\s[567]\. and \
    (mime text/css or mime image/jpeg) no-gzip vary-agent !cache-enable

# Vary header should be properly set for cachingHeader merge Vary User-Agent env=vary-agent

# Set expiry delta for static content
# Dynamic pages should set expiry delta by oneself
Header merge Cache-Control max-age=86400 env=cache-enable

You can enable the above preset for your server by uncommenting a single line in httpd.conf:

Include smart_gzip_compression.conf

Conclusion
As you can see, not so many rules are needed to adjust mod_gzip for our needs. We also showed you some examples of Helicon Ape extended syntax, but you may as well use standard Apache-compatible one. We are trying to give you a rich set of instruments, so that you can combine them the way you want and deem necessary. But simplicity and convenience are above all.

Always yours,
HeliconTech Team

]]>
http://www.helicontech.com/articles/how-to-enable-mod_gzip-compression-for-iis7/feed/ 1