How to configure a reverse proxy for multiple IIS sites and a single IP? - asp.net-mvc

I've inherited some internal sites from a previous employee and my constraints are the following, it is written in MVC and I need to change how it is hosted and no direct control over DNS. Security won't allow me to use subdomains within DNS so I'd like to use a subdomain within IIS or file path extension. manage.mgmt.domain.td or mgmt.domain.td/manage as an example.
How can I configure an IIS binding and an inbound proxy rule so that mgmt.domain.td directs to a general menu page, but manage1.mgmt.domain.td directs to a separate page and manage2.mgmt.domain.td to another page and have them fully functional. I've been able to configure the inbound reverse proxy rule to use an IP such as 10.0.0.1:801, however I cannot configure it to working using either friendly format listed above.
Can a vdir, appdir, work with an mvc project or is a reverse proxy better? It's iis10 .
https://computingforgeeks.com/configure-virtual-directory-on-windows-iis-server/
[Edit adding IIS binding and reverse proxy rule image]
Current

ASP.NET applications run in application pool. When you add vdir, you cannot choose application pool. So add application is more suitable than vdir.
I can type in the ip 10.0.0.1/manage and it points to my site, I can also type 10.0.0.1 and it will load the same content (which I dont' want)
10.0.0.1/manage is the correct url to access MVC application. If 10.0.0.1 also show same content, consider that client cache or url redirect. The correct content it shows should be index page of the main site.
however it is not loading content when I use the dns friendly name?
I cannot understand what the dns friendly name you mean. If you have a public domain name, just bind the domain to the server in server provider. and when you add site, set the host name to it.

An easier way to do this is to add a route to your application, for example on global.asax, to get the incoming server address from there and direct it to the correct destination. For Application_Begin_Request in Mvc, you can refer to this article here.

Related

How do I change the url of my IIS hosted site?

Currently, my .NET MVC application hosted on IIS 7 can be accessed remotely with the following url:
http://172.21.1.83:8080/Mysite/Index
I want to be able to use the url
http://example.com/Mysite/Index instead.
How do I achieve this? Basically I just want to get rid of the ip address and port number in the url, and replace it with a custom domain name (like example.com).
Sounds like you need a reverse proxy. That way the user doesn't have to see or use the port. Check out this article.
http://www.myconnectionserver.com/support/tutorials/v90/iisProxy/index.html
In your IIS web application click on Bidings from the right side menu bar, and add a new biding by entering your IP address and Port number, and your Host Name as the domain name which in your example is http://example.com/Mysite/Index.

Route 53 - Special domain for a single page on existing server

I have a complex web app at example-app.com, hosting fully on AWS using ELB and Route 53 for DNS. It's a Rails app.
I'm running an experiment that I'm using in the rails app, at example-app.com/test. I want to set up new-domain-app.com, to point at example-app.com/test, and have the URL cloacked to always be new-domain-app.com. It's a single page site, so it shouldn't require any navigation.
I'm having a lot of trouble figuring out how to set up my DNS on Route 53 to accomplish this. Does anyone have good ideas on what this Route 53 configuration should look like?
AWS offers a very simple way to implement this -- with CloudFront. Forget about the fact that it's marketed as a CDN. It's also a reverse proxy that can prepend a fixed value onto the path, and send a different hostname to the back-end server than the one typed into the browser, which sounds like what you need.
Create a CloudFront web distribution.
Configure the new domain name as an alternate domain name for the distribution.
For the origin server, put your existing hostname.
For the origin path, put /test -- or whatever string you want prefixed onto the path sent by the browser.
Configure the cache behavior as needed -- enable forwarding of the query string or cookies if needed and any headers your app wants to see, but not Host.
Point your new domain name at CloudFront... But before you do that, note that your CloudFront distribution has a dxxxexample.cloudfront.net hostname. After the distribution finishes setting up (the "In Progress" status goes away, usually in 5 to 20 minutes) your site should be accessible at the cloudfront.net hostname.
How this works: When you type http://example.com into the browser, CloudFront will add the origin path onto the path the browser sends, so GET / HTTP/1.1 becomes GET /test/ HTTP/1.1. This configuration just prefixes every request's path with the string you specified as the origin path, and sends it on to the server. The browser address bar does not change, because this is not a redirect. The host header sent by the browser is replaced with the hostname of the origin server when the request is sent to the origin.
What you are trying to do is not possible. Route53 is a DNS system, and you can not configure a hostname (e.g. new-domain-app.com) to point to URL (e.g. http://example-app.com/test) using DNS.
However, you are probably using a wrong tool for the job. If example-app.com/test is indeed a simple, static, single page site, then you do not need to host it inside Rails app. Instead, you can host it on AWS S3 bucket, and then you can point new-domain-app.com to that bucket using Route53.
See the following for details:
http://docs.aws.amazon.com/AmazonS3/latest/dev/WebsiteHosting.html
http://docs.aws.amazon.com/Route53/latest/DeveloperGuide/RoutingToS3Bucket.html
DNS knows about Domains, not url's. DNS simply converts names to IP addresses.
You can't do what you are asking for just using DNS and ELB, however, what you can do is have a seperate VHOST for new-domain-app.com that points to your example-app.com site and accomplishes what you want using some sort of redirection rule that only fires for new-domain-app.com.
I'm not sure that this qualifies as an SO question, and more likely is a serverfault question. Specifics about your webserver and OS platform would be helpful in getting more specific advice.
So here's some details:
You already have example-app.com setup and working
You create a CNAME entry pointing new-domain-app.com to example-app.com or you can make an A record pointing to the same IP. If you already have example-app.com pointing to a different IP address, then use a subdomain (test.example-app.com) to isolate it.
Setup a new vhost on your server that basically duplicates the existing vhost for new-domain-app.com. The only thing you need to change is the server name configuration.
Why does this work? Because HTTP 1.1 included the HOST header that browsers send along, and web servers use in vhosting to determine which virtual host to route an incoming request to. When it sees that the client browser wanted "example-app.com" it routes the request to the appropriate vhost.
Rather than having to do some fancy proxying, which certainly can be used to get to a similar result, you can just add a redirection rule that looks for requests for the host example-app.com and redirects those to example-app.com. In apache that uses mod_rewrite which people often utilize by putting rules in the ubiquitous .htacess file, but can also be done in nginx and other common web servers. The specifics are slightly different for each.

IIS URL Rewrite domain to one of several VMs

I have a server running several Virtual Machines. Each VM runs IIS 8.0 and hosts a variety of different sites. The base server is also running IIS 8.0 and I'm intending that it directs requests to one of of the VMs depending on the HTTP_HOST.
I'm trying to define the inbound and outbound rules to achieve the following:
www.mydomain1.com requests are directed to VM 192.168.1.10/mydomain1
www.mydomain2.com requests are directed to VM 192.168.1.10/mydomain2
www.mydomain3.com requests are directed to VM 192.168.1.11/mydomain3
www.mydomain4.com requests are directed to VM 192.168.1.11/mydomain4
What I want is for the user to see www.mydomainx.com all the time while the internal VM serves up the pages seamlessly without changing the URL. I've spent hours trying every combination I can think of and read dozens of posts but none seem to cover this particular scenario.
I've got subdomain to folder working (mysub.mydomain.com -> 192.168.1.10/mysub but not full domain name.
Any suggestions gratefully received.
After a lot of searching and testing I've finally found the solution. Instructions below relate to IIS 8.5.
Install Application Request Routing Extension for IIS
Install URL Rewrite 2.0 for IIS
Restart IIS Manager and you should see a new node called Server Farms
For every Virtual Machine that hosts websites, create a Server Farm, then add the IP address of the VM as a server in that farm. In my set up I have one VM per Server Farm.
Once you have all your VM's set up in their own Server Farm, select the root server node in IIS and double-click URL Rewrite
For each VM create rules for all the domains being handled.
Add a new blank rule
Add an appropriate name and set the Pattern to .* - regex for all matches
In the Conditions section, click Add
Set Condition input to {SERVER_NAME}
Make sure input string dropdown is set to Matches the Pattern
Set the pattern to ^(www.)?yourdomain.com$ - set the domain name appropriately and click ok
Add conditions for every domain name that will be handled by the VM
In the Action section, set Action Type to Route to Server Farm
In the Action properties, select the server farm that will handle these domains
Click Apply
Now all you need to do is set up your router to port forward requests on port 80 to your base IIS server and it will then route traffic based on the domain names to the appropriate Virtual Machine.
I have four Virtual Machines set up this way the routing is seamless and works really well.

asp.net mvc site with multiple subdomains in iis

I want to do capture subdomain as a parameter and show the appropriate information based on subdomain.
for example user1.domain.com and user2.domai.com each one get appropriate info.
possible duplicate: Is it possible to make an ASP.NET MVC route based on a subdomain?
what i have done so far:
I have a dns server with following ip address: 192.168.1.11 and a web server which host bunch of websites with ip 192.168.1.12
The site the should capture the subdomain is named pakdomain.dev
I've tried two scenarios:
after defining pakdomain.dev zone on iis and creating and "A" record with www as host and ip 192.168.1.12 on dns server
fist i have created another "A" record with "*" as host and wildcarding it to ip webserver
second i have created another "CNAME" record with "*" as host and wildcarding it to www.pakdomain.dev
in iss i have a binding with www.pakdomain.dev and pakdomain.dev to a website.
none of the above scenarios have worked for me when i goto www.pakdomain.dev or pakdomain.dev everything is fine but when i try to go to address like user1.pakdomain.dev or user2.pakdomain.dev the it goes to default website on iis.
what should id ? and how should i capture subdomain on my app?
Try this
var domain = HttpContext.Current.Request.ServerVariables["HTTP_HOST"];
That should give you the host header information and then use the value to route to wherever it is you want to go.

Account based lookup in ASP.NET

I'm looking at using ASP.NET for a new SaaS service, but for the love of me I can't seem to figure out how to do account lookups based on subdomains like most SaaS applications (e.g. 37Signals) do.
For example, if I offer yourname.mysite.com, then how would I use ASP.NET (MVC specifically) to extract the subdomain so I can load the right template (displaying your company's name and the like)? Can it be done with regular routing?
This seems to be a common thing in SaaS so there has to be an easy way to do it in ASP.NET; I know there are plugins that do it for other frameworks like Ruby on Rails.
This works for me:
//--------------------------------------------------------------------------------------------------------------------------
public string GetSubDomain()
{
string SubDomain = "";
if (Request.Url.HostNameType == UriHostNameType.Dns)
SubDomain = Regex.Replace(Request.Url.Host, "((.*)(\\..*){2})|(.*)", "$2");
if (SubDomain.Length == 0)
SubDomain = "www";
return SubDomain;
}
I'm assuming that you would like to handle multiple accounts within the same web application rather than building separate sites using the tools in IIS. In our work, we started out creating a new web site for each subdomain but have found that this approach doesn't scale well - especially when you release an update and then have to modify dozens of sites! Thus, I do recommend this approach rather than the server-oriented techniques suggested above based on several years worth of experience doing exactly what you propose.
The code above just makes sure that this is a fully formed URL (rather, say, than an IP address) and returns the subdomain. It has worked well for us in a fairly high-volume environment.
You should be able to pick this up from the ServerVariables collection, but first you need to configure IIS and DNS to work correctly. So you know 37Signals probably use Apache or another open source, unix web server. On Apache this is referred to as VirtualHosting.
To do this with IIS you would need to create a new DNS entry (create a CNAME yourname.mysite.com to application.mysite.com) for each domain that points to your application in IIS (application.mysite.com).
You then create a host header entry in the IIS application (application.mysite.com) that will accept the header yourname.mysite.com. Users will actually hit application.mysite,com but the address is the custom subdomain. You then access the ServerVariables collection to get the value to decide on how to customize the site.
Note: there are several alternative implementations you could follow depending on requirements.
Handle the host header processing at a hardware load balancer (more likely 37Signals do this, than rely on the web server), and create a custom HTTP header to pass to the web application.
Create a new web application and host header for each individual application. This is probably an inefficient implementation for a large number of users, but could offer better isolation and security for some people.
You need to configure your DNS to support wildcard subdomains. It can be done by adding an A record pointing to your IP address, like this:
* A 1.2.3.4
Once its done, whatever you type before your domain will be sent to your root domain, where you can get by splitting the HTTP_HOST server variable, like the user buggs said above:
string user = HttpContext.Request.ServerVariables["HTTP_HOST"].Split(".")
//use the user variable to query the database for specific data
PS. If you are using a shared hosting you're probably going to have to by a Unique IP addon from them, since it's mandatory for the wildcard domains to work. If you're using a dedicated hosting you already have your own IP.
The way I have done it is with HttpContext.Request.ServerVariables["HTTP_HOST"].Split(".").
Let me know if you need more help.

Resources