There is also an internal iframe in DocumentViewer.
Open Network tab in your browser console and examine where request for /resource.ashx/605466187810000000/webviewer/Reader.html is being redirected (you can search for reader.html), for example if it's redirected to myDomain.com/resource.ashx/605466187810000000/webviewer/Reader.html then of course it will not work because it becomes cross domain (because your main page is still e.g. for localhost domain).
> I can see that the instance of docViewer is lost during its execution.
It's not lost but because the iframe becomes cross-domain due to your Rewrite rules, the browser does not allow access of docViewer variable anymore.
Here is a rule that stops rules for GleamTech handlers (this is for Microsoft's UrlRewrite module but the idea is same):
<rule name="Stop Rewriting for GleamTech Handlers" stopProcessing="true">
<match url="(resource|filemanager|fileuploader|documentviewer)\.ashx" />
<action type="None" />
Add this rule to the top of the rules and when the request is .ashx, it will stop processing your other rules so that the .ashx request passes through and works.