tags:

views:

227

answers:

5

Yslow and google are recommending gzip for webpages. but I am not able to find how to make web pages with gzip.

Can anyone direct me to any tutorials or explain how to do it?

Thanks in advance.

A: 

The documentation you seek for Apache web server can be found here.

David Parunakian
+7  A: 

You don't make a page with gzip, you have your web server compress it on the fly before sending it to the client, thus saving bandwidth. You're very scarce on details on your setup, but instructions for apache HTTP are very straightforward.

Please note that compression trades bandwidth for CPU time, enabling it on a very busy server might bring it down...

Luca Tettamanti
+1  A: 

You'd normally enable the relevant plugin/module on your web server. No coding necessary.

If you are using Java and if you are serving your pages directly from the servlet container, you can write a servlet filter to do the same.

Example modules on Apache are mod_gzip or mod_deflate. For IIS, you should visit the TechNet pages like http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/25d2170b-09c0-45fd-8da4-898cf9a7d568.mspx?mfr=true to see how to set this up.

Hope this helps.

Cheers.

Ryan Fernandes
+1  A: 

In addition to Ryan's Link, below is some code (i don't remember from where I got it) you can put in the Global.aspx page for ASP.NET. That will compress the files at server side & decompress it at client:

void Application_BeginRequest(object sender, EventArgs e)
        {
            HttpCompress((HttpApplication)sender);
        }

        private void HttpCompress(HttpApplication app)
        {
            try
            {
                string accept = app.Request.Headers["Accept-Encoding"];
                if (accept != null && accept.Length > 0)
                {
                    if (CompressScript(Request.ServerVariables["SCRIPT_NAME"]))
                    {
                        Stream stream = app.Response.Filter;
                        accept = accept.ToLower();
                        if (accept.Contains("gzip"))
                        {
                            app.Response.Filter = new GZipStream(stream, CompressionMode.Compress);
                            app.Response.AppendHeader("Content-Encoding", "gzip");
                        }
                        else if (accept.Contains("deflate"))
                        {
                            app.Response.Filter = new DeflateStream(stream, CompressionMode.Compress);
                            app.Response.AppendHeader("Content-Encoding", "deflate");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //handle the exception
            }
        }

        private bool CompressScript(string scriptName)
        {
            if (scriptName.ToLower().Contains(".aspx")) return true;
            if (scriptName.ToLower().Contains(".axd")) return false;
            if (scriptName.ToLower().Contains(".js")) return false;
            if (scriptName.ToLower().Contains(".ashx")) return false;
            return true;
        }
Ganesh R.
I have used similar code in my site, but noticed the other day that a Server.Transfer on a page will kill the response. Firefox says that the page encoding is bad. I now have moved the code to Application_PreRequestHandlerExecute(), but I am wondering if there are other gotchas with that. Anyone every run into this?
slolife
A: 

Hi Ganesh.. I have used you approch in my application it works fine.. but one problem i am facing is in case of failures i mean if any pagemethod throws exception i am not able to get exact message for the failure on client side for eg. //Server Side [webMethod] public static MyMethod() {

throw new exception("My exception"); } //Client Side PageMethods.MyMethod(function(result){;}, function failure(exc) { alert(exc.get_message()); }

... In case of alert I am getting Service Method "MyMethod" fails. and not string as "My exception".. What should be the solution for this case.. For succesfull your code works perfect.