Using HttpContext.RewritePath() with static files - asp.net

We use HttpContext.RewritePath() to rewrite the path to static files. We do this to be able to virtualize the location of the static files on the server.
When we do so, IIS seems to behave strangely by sometimes not compressing the content of the file, but by still putting gzip encoding in the http response header. When this happens, browsers fail to properly parse the static file since they try to uncompress cleartext data.
We have been looking around and found a few threads talking about this, but we could not find a satisfying answer as to how to avoid this problem, or why should rewriting paths of static files not be allowed.
Shouldn't it be possible to rewrite the path of a static file?
Here a few pages I found about the issue:
GZip compression in IIS7 not working, but content-encoding header is set to gzip
HttpContext.RewritePath breakes buildin IIS gzip

Related

NGINX Rewrite Rule without access to the configuration file

In apache, the rewrite rule can be written in the configuration file or in .htaccess file. How about in nginx? Can I use url rewriting without access to the configuration file?
Unfortunately, you can't. This is one of the reasons shared hostings typically use apache or litespeed, not nginx or lighttpd.
A (very ugly) workaround would be to handle all requests with a script which would contain the rewrite rules and would serve the file/script according to the request URI (and which could be modified by a user without having root privileges). However you'd have a bad performance serving static files and you'd need to handle all the request headers by this script, which is not very practical.

Gzip static files previously instead of on the fly

If instead of using mod_deflate or mod_gzip I would manually gzip a css or js file obtaining something like:
base.css.gz
And I rename it as:
base.css
And then load it from an HTML page, would it work?
This could be useful on environments with very limited memory and resources, like for examples wireless access points in mesh networks.
I also wanted to ask if it would have sense to do it on normal websites to save resources of the server's cpu? At the moment I use mod_deflate, I think with this method the contents gets gzipped at every request on the fly, is it so? Isn't that a bit of waste of resources?
I answer to myself because no one addressed my question.
It is possible to serve manually gzipped files, for example css, but they need to be served by the web-server with the correct content-type and compression headers, just loading a gzipped file from a <link> or <javscript> tag won't work.
Here's an example with php:
<?php
header("Content-Encoding: gzip");
header("Content-Type: text/css");
echo file_get_contents("base.min.css.gz");
exit(0);
// no closing tag to prevent occasionally printing any chars
HTTP servers if set to use compression on static files cache the compressed file for you - so don't worry about it.
IIS documentations here
I'm not too up on Apache, but mod_deflate and mod_cache work together https://serverfault.com/a/220418/7869

How to Use GZIP files In Asp.net?

I want to use GZIP files to reduce the page load time.
I have Converted All my JS files in GZIP files.
Now I want to know How Do I set IIS (I am using IIS 7) that this can be work.
Second thing How do i call GZIP file in My Asp.net Pages as well?
Thanks in Advance
I have Converted All my JS files in GZIP files.
Not necessary. Just enable GZIP compression in IIS settings. This article gives details on how to do so. This SO discussion talks about how to validate that the settings were successfully applied.
Second thing How do i call GZIP file in My Asp.net Pages as well
Compression can be enabled for static and/or dynamic files. ASPX pages are considered a dynamic type; enabling dynamic content compression will cause them to be GZIP'd before they are served to a compatible user agent.
To validate, you can use a tool like http://gtmetrix.com/ which will warn about uncompressed resources.

Static gzip compression with IIS 7 when using mod_jk

I have a web application running on JBoss and I am using IIS 7 for load balancing JBoss instances. Static files (ex: CSS, JS) are served from IIS. I am using mod_jk ISAPI filter to bridge IIS and JBoss.
I have enabled static compression in IIS. However, the CSS files served from IIS were not getting gzip compressed (I have checked this by examining the response header and it doesnt have content-encoding: gzip header).
Post this, I enabled dynamic compression in IIS and then the CSS files were compressed with gzip. I checked my uriworkermap.properties file and it is not routing CSS file request to JBoss. I am puzzled as to why IIS wouldnt compress CSS files with static compression enabled and only compresses when dynamic compression is enabled.
Thanks,
Kishor
This is probably a result of IIS deciding not to compress the content as it's not considered "frequently hit". If you request the file twice within 10 seconds (make sure you're not hitting a cache, ctrl-F5), does it then compress it?
If so, setting the frequentHitThreshold attribute to 1 on the system.webServer/serverRuntime node in the applicationHost.config file should do the trick, as documented at http://www.iis.net/ConfigReference/system.webServer/serverRuntime.
You can do this by executing the following command as an administrator:
%windir%\system32\inetsrv\appcmd set config /section:serverRuntime /frequentHitThreshold:1 /commit:apphost
A word of warning - the "frequent hit" concept does not seem specific to compression. I have no idea whether there are other consequences as a result of setting this!

Only Serve Static Files From a Specific Directory via Code

I am looking for a code based solution to only serving static files from a specified directory using ASP.NET routing rather than specify a HttpHandler in the Web.config.
For example:
I have the following partial directory layout
/
/public
/public/hello.txt
/public/css
/public/css/base.css
/readme.txt
If any files are requested from the /public directory they should be served as-is. If any other static files are requested for example /readme.txt it should be served as a 404. The directory which allows static files to be served from should be easily specified as a string (var publicDir = "public";).
I am looking to do this with little to no configuration required in the Web.config file, and most of it configured in code.
I don't think it would be possible to achieve a solution using a class library. In IIS 6 environment files with .TXT or .JPG extension are served without going through .NET Framework. The only way is changing IIS configuration.
In IIS 7 things are handled differently but I have no idea if it is possible to find a solution that works in all environments or not.
I'll be pleased to hear what others say about this and correct me if i'm worng.

Resources