Error when reprocessing a rewritten url in IIS URL Rewrite 2

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.

asp.net
asp.net-mvc-3
iis-7
url-rewriting
asked on Stack Overflow Aug 2, 2012 by M. Mennan Kara • edited May 23, 2017 by Community

1 Answer

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

answered on Stack Overflow Aug 10, 2012 by Aaron Romine

User contributions licensed under CC BY-SA 3.0