4

I'm trying to create a system to serve images and their resized versions from GridFS using MVC3 and IIS URL Rewrite 2. After testing, I've realized that serving images directly from filesystem is 10x faster than serving them using GridFS file streams. Then I've decided to keep the originals in GridFS and create a copy of the original file and resized versions on servers local file system using a combination of Url Rewrite 2 and Asp.Net handlers.

Here are the rewrite rules I use for serving the original and the resized version:

<rule name="Serve Resized Image" stopProcessing="true">
  <match url="images/([a-z]+)/[a-f0-9]+/[a-f0-9]+/[a-f0-9]+/([a-f0-9]+)-([a-f0-9]+)-([a-f0-9]+)-([0-9]+)\.(.+)" />
  <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
  </conditions>
  <action type="Rewrite" url="/Handlers/ImageResizer.ashx?Uri={REQUEST_URI}&amp;Type={R:1}&amp;Id={R:2}&amp;Width={R:3}&amp;Height={R:4}&amp;ResizeType={R:5}&amp;Extension={R:6}" appendQueryString="false" logRewrittenUrl="true" />
</rule>
<rule name="Serve Original Image" stopProcessing="true">
  <match url="images/([a-z]+)/[a-f0-9]+/[a-f0-9]+/[a-f0-9]+/([a-f0-9]+)\.(.+)" />
  <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
  </conditions>
  <action type="Rewrite" url="/Handlers/Images.ashx?Uri={REQUEST_URI}&amp;Type={R:1}&amp;Id={R:2}&amp;Extension={R:3}" appendQueryString="false" logRewrittenUrl="true" />
</rule>

As you can see, rewrite engine checks if the file exist on the file system, and if not. rewrites the url and sends the request to the handler. Handler serves the stream and writes the file to the file system. On the next request, file is served directly from file system. I've seperated the files to folders by splitting their 24 char IDs (MongoDB Object ID as string) to avoid hunderds of thousands images in same folder.

Here is a sample original image request:

http://localhost/images/test/50115c53/1f37e409/4c7ab27d/50115c531f37e4094c7ab27d.jpg

This and the resized versions works without any problems.

Since this URL is too long and has duplicates in it, I've decided to use rewrite engine again to shorten the url to generate folder names automatically. Here is the rule which I put to the top:

<rule name="Short Path for Images">
  <match url="images/([a-z]+)/([a-f0-9]{8})([a-f0-9]{8})([a-f0-9]{8})(.+)" />
  <action type="Rewrite" url="images/{R:1}/{R:2}/{R:3}/{R:4}/{R:2}{R:3}{R:4}{R:5}" appendQueryString="false" logRewrittenUrl="true"></action>
</rule>

When I request an image using this rule for example with the following URL:

http://localhost/images/test/50115c531f37e4094c7ab27d.jpg

it only serves the image if the image is already on filesystem, otherwise I get the following error:

HTTP Error 500.50 - URL Rewrite Module Error. The page cannot be displayed because an internal server error has occurred.

I've checked IIS Log File Entry for the request. It doesn't show any details except:

2012-08-02 14:44:51 127.0.0.1 GET /images/test/50115c531f37e4094c7ab27d.jpg - 80 - 127.0.0.1 Mozilla/5.0+(Windows+NT+6.1;+WOW64)+AppleWebKit/537.1+(KHTML,+like+Gecko)+Chrome/21.0.1180.60+Safari/537.1 500 50 161 37

On the other hand, successfull requests log the rewritten URL's like:

GET /Handlers/ImageResizer.ashx Uri=/images/test/50115c53/1f37e409/4c7ab27d/50115c531f37e4094c7ab27d-1f4-1f4-2.jpg&Type=test&Id=50115c531f37e4094c7ab27d&Width=1f4&Height=1f4&ResizeType=2&Extension=jpg

Elmah and EventLog also doesn't show anything. Added a filesystem logger to the top of my controller method and it doesn't log these particular problematic requests.

Can anyone suggest a workaround to get it work?

Edit: After RuslanY's suggestion about Failed Request Tracing, I've managed to identify the error:

ModuleName: RewriteModule 
Notification: 1 
HttpStatus: 500 
HttpReason: URL Rewrite Module Error. 
HttpSubStatus: 50 
ErrorCode: 2147942561 
ConfigExceptionInfo: 
Notification: BEGIN_REQUEST
ErrorCode: The specified path is invalid. (0x800700a1)

Entire Trace Result can be see here (IE only)

Unfortunately, this is still not taking me to the solution since the second rule (therefore shortening rule) is working when the file exist on the file system.

Community
  • 1
  • 1
M. Mennan Kara
  • 10,072
  • 2
  • 35
  • 39
  • You should look in your IIS logs (`%SystemDrive%\inetpub\logs\LogFiles`) to find the specific error. It could be a problem with your rewrite rules .. or a dud result from the script you are calling to generate the images. – Stennie Aug 02 '12 at 12:24
  • Unfortunately, the logs doesn't show anything. – M. Mennan Kara Aug 02 '12 at 14:51
  • 1
    Have you tried to check if you get more details about the error from IIS Failed Request Tracing log? http://learn.iis.net/page.aspx/467/using-failed-request-tracing-to-trace-rewrite-rules/ – RuslanY Aug 02 '12 at 17:33
  • Thanks. Added some additional information to the question, after getting the trace result. – M. Mennan Kara Aug 02 '12 at 18:04
  • This is just a shot in the dark but I've noticed in your last rewrite rule, 'images/xxxx/xxxx/' is missing the beginning slash. Another thought would be the order of the rewrite rules; IIRC, they are executed in the order they are entered, if you have a rewrite rule that redirects to another, you may have to put it before it. – Master Morality Aug 10 '12 at 14:36
  • Hi, thanks for the comment, the short path rule is on the top and IIS Url Rewrite prefers the rule regexps without the leading slash. (Also I've tried every possible combinations with the slashes) :) – M. Mennan Kara Aug 10 '12 at 14:51
  • Hey - I know this has bit me in the past, but the files being created can be served by IIS right (e.g. NTFS permissions for the user running IIS can read)? Just trying to knock permissions issues out. – Aaron Romine Aug 10 '12 at 15:52
  • Yep, no issues with permissions or rules. My guess is IIS Url Rewrite has a bug while processing subsequent rules with IsFile condition. – M. Mennan Kara Aug 10 '12 at 18:19

1 Answers1

2

As an alternative approach to using UrlRewrite to do this checking, why not using Application Request Routing w/ disk based caching. Dynamic images will be generated, and the caching infrastructure of ARR will save generated images to disk. Less mess, and I've used ARR to great success in production scenarios. Disk cache persist between IIS restarts and can live as long as you say (default is to use cache information from the response, but you can override this to be longer).

Application Request Routing

Aaron Romine
  • 1,539
  • 1
  • 9
  • 8
  • While at it, would you like to take a look at my other question [Selecting custom output cache provider for specific controller actions](http://stackoverflow.com/questions/11818528/selecting-custom-output-cache-provider-for-specific-controller-actions) – M. Mennan Kara Aug 10 '12 at 18:21