简体   繁体   English

使用共享IP地址从IIS站点进行出站Web请求失败

[英]outbound web requests fail when made from IIS sites using shared IP address

  • IIS 7.5 / Windows Server 2008 R2 IIS 7.5 / Windows Server 2008 R2
  • Multiple IIS sites bound to the same IP address, using host names. 多个IIS站点使用主机名绑定到同一IP地址。
  • Inbound traffic to sites working fine. 工作正常的网站的入站流量。
  • Outbound web requests made by the back-end site code fail. 后端站点代码发出的出站Web请求失败。 Remote site returns 404 (NotFound). 远程站点返回404(NotFound)。
  • Verified via a network trace that the traffic is making it to the remove server. 通过网络跟踪验证流量是否已移至删除服务器。
  • Same requests work fine if done from a site using a dedicated IP address (ie not shared w/ any other sites). 如果从使用专用IP地址的站点(即不与任何其他站点共享)完成相同的请求,则工作正常。

Anyone have any ideas on how to make this work or what could be going wrong? 任何人都有任何关于如何使这项工作或可能出错的想法?

Network trace on hosting server: 托管服务器上的网络跟踪:

Successful request from site w/ non-shared IP address: 来自非共享IP地址的站点的成功请求:

No.     Time            Source                Destination           Protocol Info
   6366 15:54:35.590463 192.168.1.76          173.194.77.121        HTTP     GET /key/value/one/two HTTP/1.1 
   6369 15:54:35.599879 173.194.77.121        192.168.1.76          TCP      http > 55407 [ACK] Seq=1 Ack=110 Win=344 Len=0
   6370 15:54:35.621587 173.194.77.121        192.168.1.76          HTTP     HTTP/1.1 200 OK  (application/json)
   6608 15:54:35.815774 192.168.1.76          173.194.77.121        TCP      55407 > http [ACK] Seq=110 Ack=357 Win=509 Len=0

Failed request from site using a shared IP address: 来自使用共享IP地址的站点的请求失败:

No.     Time            Source                Destination           Protocol Info
   9720 15:54:39.244192 192.168.1.80          173.194.77.121        HTTP     GET /key/value/one/two HTTP/1.1 
   9760 15:54:39.256958 173.194.77.121        192.168.1.80          TCP      [TCP segment of a reassembled PDU]
   9761 15:54:39.256962 173.194.77.121        192.168.1.80          HTTP     HTTP/1.1 404 Not Found  (text/html)
   9762 15:54:39.257027 192.168.1.80          173.194.77.121        TCP      55438 > http [ACK] Seq=212 Ack=1676 Win=512 Len=0

Code: 码:

public static HttpWebRequest CreateWebRequest(string url, string method = "GET", string referer = null, string contentType = null, int timeout = 100000, string authentication = null, string bindToIpAddress = null, string host = null)
{
    var request = (HttpWebRequest)WebRequest.Create(url);

    if (!string.IsNullOrWhiteSpace(bindToIpAddress))
    {
        IPAddress bindIp;
        if (!IPAddress.TryParse(bindToIpAddress, out bindIp))
        {
            throw new ArgumentException("bindToIpAddress");
        }

        request.ServicePoint.BindIPEndPointDelegate = ((sp, rep, rc) =>
        {
            return new IPEndPoint(bindIp, 0);
        });
    }

    request.Accept = "*/*";
    request.ContentType = contentType;
    request.Referer = referer;
    request.Method = method;
    request.Timeout = timeout;

    if (!string.IsNullOrWhiteSpace(host))
    {
        request.Host = host;
    }

    return request;
}

string GetData()
{
    try
    {
        string result;

        var request = CreateWebRequest("http://jsonplaceholder.typicode.com/posts/1", 
                                       "GET", 
                                       "somedomain.com", 
                                       timeout: (10 * 1000), 
                                       bindToIpAddress: "192.168.27.133" /*site IP*/);

        request.Accept = "application/json";

        using (var response = request.GetResponse())
        {
            using (var sr = new StreamReader(response.GetResponseStream()))
            {
                result = sr.ReadToEnd();
            }
        }

        return result;
    }
    catch (Exception ex)
    {
        return null;
    }
}

The 404 response comes back from the remote site, so the remote site is failing to process your request, and it has nothing to do with what's going on on your local server. 404响应从远程站点返回,因此远程站点无法处理您的请求,并且它与本地服务器上发生的事情无关。

The only difference from the remote site's point of view is the sender's IP address, so it must be configured to accept requests only from certain IP addresses. 与远程站点的唯一区别是发件人的IP地址,因此必须将其配置为仅接受来自某些IP地址的请求。 This restriction could be in the remote server, or any firewall, router or proxy in between. 此限制可能位于远程服务器或其间的任何防火墙,路由器或代理中。

This turned out to be a bug in our application. 这是我们的应用程序中的一个错误。 In some cases, the host header (Host property on the request) was being set improperly (to the hosting/source site's host name). 在某些情况下,主机头(请求中的主机属性)设置不正确(到主机/源站点的主机名)。 The stripped down code sample in the question doesn't show it. 问题中的精简代码示例未显示。 It was fine for web services that ignored the header and was an issue (404 response) for others that did not ignore the header. 忽略标头的Web服务很好,对于没有忽略标头的其他人来说是一个问题(404响应)。 The issue had nothing to do w/ IIS or shared IP address. 该问题与IIS或共享IP地址无关。 Thanks for all of the responses. 感谢所有的回复。

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

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