简体   繁体   English

Azure 应用服务内部 DNS 解析

[英]Azure App Service internal DNS resolution

I have an Azure Web App that communicates with a 3rd party system thru VPN / VNET integration.我有一个 Azure Web 应用程序,它通过 VPN/VNET 集成与 3rd 方系统进行通信。 Part of that communication relies on being able to resolve a DNS name.该通信的一部分依赖于解析 DNS 名称的能力。

When the app is deployed to a virtual machine, we just update the hosts file with an entry like当应用程序部署到虚拟机时,我们只需使用如下条目更新主机文件

123.45.67.89 RPGMDR500-0R 123.45.67.89 RPGMDR500-0R

and the communication works fine.并且通讯正常。 Because of the way the communication is setup, I cannot just use the IP address in code.由于通信的设置方式,我不能只在代码中使用 IP 地址。 In the communication the DNS name is sent back to us and we have to resolve it to that ip address.在通信中,DNS 名称被发送回给我们,我们必须将其解析为该 IP 地址。

Is there a way to do this with just an App Service?有没有办法只用一个应用服务来做到这一点? Effectively, we want the app to resolve this custom dns name to the ip address.实际上,我们希望应用程序将此自定义 dns 名称解析为 IP 地址。

UPDATE I updated our VNET DNS Servers to use a Virtual Machine setup for DNS.更新我更新了我们的 VNET DNS 服务器以使用虚拟机设置进行 DNS。

使用 DNS 的 VNET

Using nameresolver, I can see that the DNS is reaching the app service, but only if I specify the DNS IP address.使用 nameresolver,我可以看到 DNS 正在访问应用程序服务,但前提是我指定了 DNS IP 地址。 By Default, it is still using a Default Server (which not sure how).默认情况下,它仍在使用默认服务器(不确定如何)。 (sorry for the blackouts, not sure how sensitive those datas are). (对于停电很抱歉,不确定这些数据有多敏感)。

名称解析器

In Azure Web App, you cannot add an entry into the hosts file as it is not allowed.在 Azure Web App 中,您不能将条目添加到主机文件中,因为这是不允许的。

Before you proceed you may want to check if the web app can do the name resolution in the first place.在继续之前,您可能首先要检查 Web 应用程序是否可以进行名称解析。 The Azure Web App has nameresolver.exe built in. You can run the following command: Azure Web 应用程序内置了nameresolver.exe 。您可以运行以下命令:

nameresolver www.facebook.com

Kudu Nameresolver.exe Demo

You can do name resolution in your code.您可以在代码中进行名称解析。 Here is a discussion thread on stackoverflow: How to get the IP address of the server on which my C# application is running on?这是有关 stackoverflow 的讨论线程: 如何获取运行我的 C# 应用程序的服务器的 IP 地址?

It works using System.Net它使用System.Net

string url = "www.facebook.com";
IPAddress[] addressList = Dns.GetHostAddresses(url);

As far as I know, we have no permission to access the disk C in the azure web app.So we couldn't change the hosts file in the azure web app.据我所知,我们在azure web app中没有访问C盘的权限。所以我们无法更改azure web app中的hosts文件。

Image like below:如下图:

在此处输入图片说明

Here is a workaround, if you use http to access the vnet, you could change the request host header to change the dns name in the code by using HttpRequestHeaders class.这是一种解决方法,如果您使用 http 访问 vnet,则可以使用 HttpRequestHeaders 类更改请求主机标头以更改代码中的 dns 名称。

More details, you could refer to below code sample.更多细节,你可以参考下面的代码示例。

HttpClient client = new HttpClient();
client.BaseAddress = new Uri(@"http://111.111.111.70");
client.DefaultRequestHeaders
      .Accept
      .Add(new MediaTypeWithQualityHeaderValue("application/json"));//ACCEPT header

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, @"http://111.111.111.70");
request.Headers.Host = "DNS name";

var re=  client.SendAsync(request).Result;

It is a lot of overhead to set it up but you could set up a DNS server in an Azure Virtual Network and then use the VNet Integration feature with your app.设置它的开销很大,但您可以在 Azure 虚拟网络中设置 DNS 服务器,然后将 VNet 集成功能与您的应用程序结合使用。 Your app would then use the DNS of your VNet which you could configure as you see fit.然后,您的应用将使用 VNet 的 DNS,您可以根据需要对其进行配置。

Here is documentation for the Azure app service that are accessing the VMs in the network. 以下是访问网络中 VM 的 Azure 应用服务的文档 It is said that you will need dedicated DNS server to be set up if app service and VMs are in different networks.据说如果应用服务和虚拟机在不同的网络中,你需要设置专用的DNS服务器。

However I did not tried it and I have heard complains that we don't have this option to sync network using VNET Integration但是我没有尝试过,而且我听到抱怨说我们没有这个选项来使用 VNET 集成同步网络

You can add role DNS server to a Vm in the same vnet the webapp has access to.您可以将角色 DNS 服务器添加到 Web 应用有权访问的同一 vnet 中的 Vm。 Set that DNS server up to just forward all requests to the AAD DNS servers in the other vnet.将该 DNS 服务器设置为仅将所有请求转发到其他 vnet 中的 AAD DNS 服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM