iis reverse proxy showing error 404 for services - asp.net

I have Asp.net MVC 4 Application with WCF services. Project is hosted in iis Production Server P125 (10.10.10.125), there are few other sites hosted in same server. Our Web / Front End Server is working as Reverse proxy for production server (Url Rewrite and Application Request Routing installed) As,
185.132.x.x is public ip of Web Server acting as reverse proxy only (Windows Server 2016).
Our application is hosted at port 8899 of Application server ip 10.10.10.125
185.132.x.x:8080 <= reverse proxy => 10.10.10.125:8899
Website is working fine, all the pages/actions are working fine of MVC application, except the services (rest.svc , winact.svc)
whenever we hit 185.132.x.x:8080/rest.svc its showing error :
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /rest.svc
I dont know why ?
all our services are showing same error, while the services are working fine with 10.10.10.125:8899/rest.svc on local network as well as from the WebServer itself.
On remote login to WebServer(185.132.x.x) and browsing/accessing the application and services (.svc) via 10.10.10.125:8899, its working fine.
I am using the Reverse Proxy as :
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="ReverseProxyInboundRule1" stopProcessing="true">
<match url="(.*)" />
<action type="Rewrite" url="http://10.10.10.125:8899/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

After spending 48 hours i have figured out that, configuring serviceHostingEnvironment resolved the issue of services (.svc) and my Web.config on WebServer(185.132.x.x) is like :
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.web>
<customErrors mode="Off"/>
<compilation debug="false" targetFramework="4.5"/>
<httpRuntime targetFramework="4.5" />
<pages>
<namespaces>
<add namespace="System.Web.Helpers"/>
<add namespace="System.Web.Mvc"/>
<add namespace="System.Web.Mvc.Ajax"/>
<add namespace="System.Web.Mvc.Html"/>
<add namespace="System.Web.Optimization"/>
<add namespace="System.Web.Routing"/>
<add namespace="System.Web.WebPages"/>
</namespaces>
</pages>
</system.web>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
<rewrite>
<rules>
<rule name="ReverseProxyInboundRule1" stopProcessing="true">
<match url="(.*)" />
<action type="Rewrite" url="http://10.10.10.125/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Related

web.config redirection ignored on a shared hosting

I have a webpage hosted on a shared Microsoft-IIS/8.5 server.
I don't have the admin privileges on that server, and I don't have access to its configuration files, so I can't really make sure that the URL rewrite module or the HTTP redirect element are correctly installed / set up.
I would like to establish a redirection from a folder named old_folder to a folder named new_folder (both at the root for me, i.e., at http://sharedhosting.com/myusername/).
I placed in the root folder the following web.config file:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<configSections>
<sectionGroup name="system.webServer">
<sectionGroup name="rewrite">
<section name="rules" overrideModeDefault="Allow" />
</sectionGroup>
</sectionGroup>
</configSections>
<system.webServer>
<rewrite>
<rules>
<rule name="attempt" stopProcessing="true" enabled="true">
<match url="^old_folder/$" />
<action type="Redirect" url="new_folder/" redirectType="Permanent"/>
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
But all I can get is a 403 - Forbidden: Access is denied. (if I leave an empty old_folder) or a 404 - File or directory not found. (if I erase it).
I tried using the HTTP Redirect, by placing in old_folder a Web.config file containing
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<httpRedirect enabled="true" destination="http://sharedhosting.com/myusername/new_folder/" />
</system.webServer>
</configuration>
But that didn't worked either.
I know that my Web.config files are read (I can get 500 errors if there is an error in them). I suspect that the URL rewrite is installed, since, for instance, http://sharedhosting.com/myusername/folder is rewritten to http://sharedhosting.com/myusername/folder/ (i.e., with a slash) if the folder folder exists.
Is my rule correct? Does my host prevent me from redirecting? If yes, how can I tell?
I added a <clear /> after the <rules> tag to discard all the other rewriting rules, but it still isn't redirecting anything.
What am I missing?
LAST EDIT BEFORE THE BOUNTY EXPIRES
To clarify, my question is "How to understand why the server isn't correctly interpreting / ignoring my instructions?".
We came to the conclusion that it was probably my server that was weirdly set-up, I'm precisely trying to "revert-engineer" that and to detect what's making the server ignore the redirect / rewrite rules.
You can achieve the same thing by using the HttpHandlers , so i'm presuming that you can access the both Old_folder and New_folder directly in the browser .
Here is a sample httphandlers , you can do in this way :
youname.Handlers
{
public class RedirectHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.Status = "301 Moved Permanently";
string url = context.Request.Url.ToString().Replace("old_folder", "new_folder");
context.Response.AddHeader("Location", url);
}
public bool IsReusable
{
get { return false; }
}
}
}
In web_config add the handler as follows:
system.web>
<httpHandlers>
<add verb="*" path="xxx/*.*" type="yourname.Handlers.RedirectHandler"/>
</httpHandlers>
</system.web>
Using IIS Url_rewriting :
<?xml version="1.0"?>
<configuration>
<system.webServer>
<rewrite>
<rewriteMaps>
<rewriteMap name="yourRedirects">
<add key="/yourroot/old_folder/" value="/otherdir/new_folder" />
</rewriteMap>
</rewriteMaps>
<rules>
<rule name="RedirectRule" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{yourRedirects:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="http://www.yourdomain.com{C:1}" appendQueryString="False" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
I think this can be done with a simple rewrite rule.
<rule name="ToNewFolder" stopProcessing="true">
<match url="^old_folder/(.*)" ignoreCase="true" />
<action type="Rewrite" url="new_folder/{R:1}" />
</rule>
The above rule will send all requests to the new folder, but keeps the old url with old_folder in it. If you want the url to change it's display to new_folder in it, use
<rule name="ToNewFolder" stopProcessing="true">
<match url="^old_folder/(.*)" ignoreCase="true" />
<action type="Redirect" url="new_folder/{R:1}" />
</rule>
Tested it on my local machine with folders, subfolders an requests with a QueryString. I hope this works on shared hosting also.

ASP.NET Core http 500 error

I have created a redirect rule in my web.config in order to redirect my website from http to https. I published my website on my staging and it was working perfectly. After some manipulation in my web.config, i published again my website and now i have a HTTP 500 error. I tried to undo those modification and set my web.config as it was and i still have that error. I tried everything as well as deleting my web.config and create a new one but it seems that the manipulation i did has crushed my staging and i am not able to make it work again.
This is my web.config before redirection:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
</handlers>
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false">
</aspNetCore>
</system.webServer>
</configuration>
This is what i added for redirecting my http to https and it was working perfectly.
<rewrite>
<rules>
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="https://{HTTP_HOST}/{R:1}" />
</rule>
</rules>
</rewrite>
Then i made some manipulation and change the action input {HTTP_POST} to my domain name. While it is working locally, my staging is not working anymore and i tried to undo everything but i still have the HTTP 500. What should i do?

Blocking invalid URL and redirect to homepage in IIS

Currently I am using the web hosting service provided by another company. It uses IIS 7/IIS 8 and allows customers to modify web.config according to their needs.
Suppose I have set up two valid URL:
http://www.example.com
http://dev.example.com
Now, when I try to access http://abc.example.com, HTTP Error 403.14 - Forbidden is returned; and if I then try to access http://www.example.com/abc, HTTP Error 404.0 - Not Found is returned.
How can I configure web.config such that users will be redirected to http://www.example.com when they are trying to access an invalid URL?
I have tried to use the snippet provided by the company but without success:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Canonical Host Name" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example\.com$" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
In system.web in webconfig tag add this tag
<customErrors mode="On" defaultRedirect="~/Home/Index">
</customErrors>
This will redirect you to the home page whenever an error occured
You can be more specific with redirection by adding in customErrors tag
<error statusCode="401" redirect="/error/otherpage"/>
This will indicate the page that you will be redirected to when 401 error occurs

Url Rewrite with Web.config not working with IIS express

I need to use Url Rewriting, so I made a test case, in Web.config, to check if it's working:
Web.config:
<system.webServer>
<rewrite>
<rules>
<rule name="Fail bad requests">
<match url=".*"/>
<action type="AbortRequest" />
</rule>
</rules>
</rewrite>
... other stuff
</system.webServer>
I was expecting any localhost:3285 to abort and fail, but it entered correctly.
I'm using Url Rewrite with IIS Express.
I am guessing your problem is browser caching. I have found that if I open a web page in IE, then add URL rewrite rules (requires stopping IIS express), then open the same page again in IE it still loads the page (and it appears the abort rule is not being applied). But, if I clear the IE browser cache and refresh the page the abort occurs.
Here is a full Web.config example that works for me
<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<system.webServer>
<rewrite>
<rules>
<rule name="Fail bad requests">
<match url=".*"/>
<action type="AbortRequest" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Your example should work because the URL rewrite module is built into the current version of IIS Express:
http://www.iis.net/learn/extensions/introduction-to-iis-express/iis-express-overview

Permanent 301 redirect for ASP.NET IIS7

Im trying to figure out how to setup a 301 permanent redirect for a website that is placed on a Microsoft-IIS/7.0 type server.
So let's say I have domain www.A.com and I want to redirect this to www.B.com I could use something like the following in my web.config file:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to WWW" stopProcessing="true">
<match url="A.com" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.B.com$" />
</conditions>
<action type="Redirect" url="http://www.B.com/{R:0}"
redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
When placing the web.config in the root directory, the server responds:
403 - Forbidden: Access is denied.
You do not have permission to view this directory or page using the credentials that you supplied.
Any suggestion why this 403 error is given?
Thanks in advance.
If you are getting a 403 error with a plain (default) web.config and totally vanilla Default.aspx, then there is a configuration problem with IIS. Most likely the app pool does not have rights to the base folder for the website. Sounds like you're in a hosted situation so contact the system administrator.
This should work and also be much simpler. You don't need URL rewriting, just HTTP redirect.
As for the 403, does the IIS application pool have read access to the folder at the root of your site?
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<httpRedirect enabled="true" destination="www.B.com" httpResponseStatus="Permanent" />
</system.webServer>
</configuration>

Resources