Azure Application Gateway - Prevent redirection - azure

I've a AppService running with an endpoint www.test.com. I've deployed an Application Geteway and set up a backend pool to forward traffic from AG to www.test.com. I've then created DNS records to point www.test.org to the AG IP. When I navigate to www.test.org the browser does a 301 redirect to www.test.com.
I want to prevent the redirection as from the users point of view, however I want the request to forward www.test.com but the browser to remain the same.
Interesting I had previously done this on another site with the exact same config (I think) with the only exception it is using the tier WAF2.

In the HTTPSettings, update the Override Hostname field with "www.test.com". When you do that the actual request will be for "www.test.org" and when the request goes to your backend pool, it will have "www.test.com" as the hostname.
This should fix your issue. You don't need to configure any redirect in your backend server.

Related

Redirect www to non-www on Azure Dns Zone

I have created Azure DNS Zone to manage redirections which use domain from namecheap.
Let's assume that my website is www.johndoe.com and currently website working well for johndoe.com but when I call www.johndoe.com it doesn't work well.
I have CNAME record and in CNAME redirect to redirect but unfortunately it doesn't work.
When I have call www.johndoe.com response message is as following...
Our services aren't available right now
We're working to restore all services as soon as possible. Please check back soon.
Please take into account that DNS does not provide a redirect.
An additional A or CNAME record only provides your IP address to people that navigate to that specific URL. When www.johndoe.com and johndoe.com both resolve to your web server, this means each page on your site has two addresses. This is horrible for things like SEO.
For a true redirect (and a 301 is preferred for SEO), set something up in the service that's handling the traffic coming in.
For instance: if you have an App Service running ASP.NET, setup the redirect in web.config. If you have Front Door (or a CDN) in place, use the rules engine to create a redirect with the correct condition and action.
Here's an example redirect in an Azure Front Door Classic Resource (this one redirects the other way):

How to send all traffic to a certain path with a application gateway (azure)

I'm struggling to find a way of sending all traffic into a app gw to a certain website path.
Say my website is, stackoverflow.devops.com.
I have a App Service backend with the domain stackoverflow.devops.com.
The idea going forwards is to send all traffic to that website to a traffic manager, doing geolocation routing. Currently, if you're in Europe, it routes to an application gateway where the path is fine, and this works.
If you're in the states, it routes to a different application gateway (with the same backend pool) and I want to change the url to stackoverflow.devops.com/us for any traffic going through this app gw.
I've tried URL rewrites but getting a 'too many redirects' error.
Thanks!
It sounds like you are redirecting back to the public DNS name and going back out to Traffic Manager which is creating a loop.
You should be able to specify '/us' as the URL path value on your rewrite rule.
I'm curious about what you are trying to achieve here though if the backend pool is the same for both application gateways. Would you not want to have another App Service in the US region also?

AddRedirectToWww() redirecting to wrong domain

I'm using the AddRedirectToWww rule for the rewriter middleware and I keep getting redirected to the wrong domain.
For context, my website is hosted on azure app service which has a default domain of myapp.azurewebsites.net. I have a custom domain say myapp.com and www.myapp.com.
I setup the rule so traffic from myapp.com gets redirect to www.myapp.com. However, what is happening is upon hitting myapp.com I get a redirect towards www.myapp.azurewebsites.net.
Any reason for why this is so? Does this middleware require specific request headers to determine the right host?
From the scenario and configuration you described, it looks like you have not set CNAME and TXT record correctly. You need to configure this in your Domain Registrar/Provider.
You need to map a subdomain (for example, www.myapp.com) by using a CNAME record.
Map a root domain (for example, myapp.com) by using an A record.
Kindly refer this document for step-step process.

Application Gateway 502 error when configuring it for Azure Web App

Scenario:
Configured Azure Application Gateway to route traffic to different Azure Web Apps to leverage the Web Application Firewall (WAF).
Initial configuration worked fine except that the final URL in the browser is the {mysite}.azurewebsites.net. I want this to be the custom domain name {subdomain}.{mysite}.com. This was configured by having the backend pool bound to an app service. Changing it to FQDN is not working. Screen shots below.
CNAME is already pointing to the gateway.
Desired result
When someone hits {subdomain}.{mysite}.com (which is already configured to point to the gateway) I should get {subdomain}.{mysite}.com not {mysite}.azurewebsites.net
Current setup:
The gateway is configured with a multi-site listener with its rule pointing to a backend pool and also bound to the http setting.
Listener is listening on {subdomain}.{mysite}.com
Backend pool config
Probe config
Http setting config
The following links provide some partial answers/configurations but none of them worked for me.
Application Gateway - 502 error
https://learn.microsoft.com/en-us/azure/application-gateway/troubleshoot-app-service-redirection-app-service-url
https://learn.microsoft.com/en-us/azure/application-gateway/create-web-app
UPDATE
Here are a list of steps used to configure an azure app service with a custom domain behind the Azure Application Gateway and WAF (Web Application Firewall) using the azure portal UI.
Configure a Backend pool with target pointing to your App Service.
Configure Health Probe with the following settings
Protocol: HTTP or HTTPS
Host: {appservice}.azurewebsites.net
Path: /
Configure Http setting
Use App Service unchecked.
Use custom probe checked.
Pick host name from backend address unchecked (you might have to check this to configure the rule and then uncheck it).
Configure listener
HostName: {subdomain}.{domain}.com (your domain)
Configure basic rule
Bind it to the appropriate http setting, backend pool and listener
Caveat: If there are no http settings with Pick host name from backend checked, you might need to change it, attach it, and then remove that flag again.
Probably, you get a 502 error because you change to FQDN subdomain.mysite.com in the targets of backend pool. However, In this scenario, you have not changed the custom domains in your web app service, you only could access the web site via the default domain mysite.azurewebsites.net. I suggest changing back to the App service in the targets.
Initial configuration worked fine except that the final URL in the
browser is the {mysite}.azurewebsites.net.
Please check if you have an HTTPS Only setting ON in the Custom domains of web app service. This will redirect all Http to Https. For me, usually, it will cause the final URL in the browser is the mysite.azurewebsites.net.
Update
If you have no HTTPS Only on, this issue may happen due to the following main reasons:
You have redirection configured on your App Service. Redirection can be as simple as adding a trailing slash to the request.
You have Azure AD authentication which causes the redirection.
You have enabled “Pick Host Name from Backend Address” switch in the HTTP settings of Application Gateway.
You don’t have your custom domain registered with your App Service.
This issue can be resolved by not having a redirection on the Application side, however, if that’s not possible, you can follow this solution to resolve it.
Register the domain subdomain.mysite.com to the custom domain list of the App Service. For more information, see Map an existing custom DNS name to Azure App Service.
Once that is done, your App Service is ready to accept the hostname subdomain.mysite.com. Now change your CNAME entry in DNS to point it back to Application Gateway’s FQDN. For example, appgw.eastus.cloudapp.azure.com.
Uncheck the checkbox Pick Hostname from Backend HTTP Settings in probe settings. In the hostname field of the probe, enter your App Service's FQDN mysite.azurewebsites.net as the probe requests sent from Application Gateway will carry this in the host header.
Uncheck the checkbox Pick Hostname from Backend Address in your Application Gateway’s HTTP settings.
Associate the custom probe back to the backend HTTP settings and verify the backend health if it is healthy.
Once this is done, Application Gateway should now forward the same hostname subdomain.mysite.com to the App Service and the redirection will happen on the same hostname.

Application gateway URL path-based routing always redirect to default backend pool

I have configured 4 webapps behind Application gateway (ARM template):
dummyapi1.azurewebsites.de/servicehosts/dummyapi1
dummyapi2.azurewebsites.de/servicehosts/dummyapi2
dummyapi3.azurewebsites.de/servicehosts/dummyapi3
dummyapi4.azurewebsites.de/servicehosts/dummyapi4
web app settings
I can open web apps when I go directly to dummyapiX.azurewebsites.de/servicehosts/dummyapiX.
I want routing work this way: when I go to https://PIPofApplicationGateway/servicehosts/dummyapiX it should redirect to
dummyapiX.azurewebsites.de/servicehosts/dummyapiX
But Application Gateway always redirect to default backend pool, therefore I get 404 error.
I created Custom probes that point to:
/servicehosts/dummyapi1
/servicehosts/dummyapi2
/servicehosts/dummyapi3
/servicehosts/dummyapi4
Backend health show Healthy status for all. Backend pools point to webapps FQDN. PickHostNameFromBackendAddress for httpsSettings and PickHostNameFromBackendHttpSettings for probes are set to true. No other Path related setting are not configured in httpsSettings.
The only path-based rule is:
default backendpool is dummyapi4
"/servicehosts/dummyapi1/*" to backendpooldummy1
"/servicehosts/dummyapi2/*" to backendpooldummy2
"/servicehosts/dummyapi3/*" to backendpooldummy3
Any suggestion? What might be a root cause?
The root cause was trailing "/" in path in rules.
So instead of:
"/servicehosts/dummyapi1/*" to backendpooldummy1
"/servicehosts/dummyapi2/*" to backendpooldummy2
"/servicehosts/dummyapi3/*" to backendpooldummy3
It should be in my case
"/servicehosts/dummyapi1*" to backendpooldummy1
"/servicehosts/dummyapi2*" to backendpooldummy2
"/servicehosts/dummyapi3*" to backendpooldummy3
Now everything works
Firstly, you can confirm if you have created the virtual directory in your web app setting. Make sure the URL-based rule maps to the specific backend pool. Create the basic listener with a listening request port. Create and map a Path-based rule and bind to the listener. Get more details from this tutorial.
Then, you can type ipconfig /flushdns to clear the DNS cache In the command prompt console. Verify it again.
Ref: Error 404 : Web App Not Found – Azure Web App

Resources