【问题标题】:Is it possible to get the current machine name in IIS 7.5 Url Rewrite Module Rules?是否可以在 IIS 7.5 Url Rewrite Module Rules 中获取当前机器名称?
【发布时间】:2012-08-02 08:17:24
【问题描述】:

HTTP_HOST 和 SERVER_NAME 服务器变量为我提供来自 HTTP 请求的主机名(可以是 IP 地址或基于请求的负载平衡 DNS)。我找不到可以提供实际机器名称的服务器变量(即 COMPUTERNAME 环境变量的值)。

我正在尝试在服务器场上设置一些重定向规则,并且有一些基于当前机器名称的规则(例如,内部机器的名称中有 int)。我不想为每台机器创建单独的规则,而是希望有一些基于当前机器名称的条件逻辑。我似乎无法找到获取机器名称的方法。

有什么想法吗?

【问题讨论】:

    标签: iis url-rewriting iis-7.5


    【解决方案1】:

    在 IIS7 中,计算机名称不是内置服务器变量之一。但是,一切都不会丢失,通过使用自定义 UrlRewrite 提供程序进行一些工作,您可以在重写规则中显示您的机器名称。

    Scott Forsyth 实际上已经构建了类似的东西,并写了一篇描述其使用的博客文章,并提供了源代码和预构建的二进制 + 安装程序:

    URLRewrite ServerNameVariable Provider

    为了在该文章在这里消失的情况下保留此信息,步骤如下:

    首先在 Visual Studio 中创建一个新的类库项目。您可以在此处找到执行此操作的步骤:

    Developing a Custom Rewrite Provider for URL Rewrite Module (IIS.NET)

    它们本质上是(以防链接失效):

    1. 创建一个类库项目并将其命名为ServerNameProvider您需要确保项目是 .NET 2.0 或 3.5 项目。 IIS7.5 托管代码支持仍然针对 2.0 运行时。

    2. 将默认的 Class1.cs 文件重命名为反映提供者用途的名称,例如:ServerName.cs - 并确保源中的类名也反映这一点。

      李>
    3. 添加对:%ProgramFiles%\Reference Assemblies\Microsoft\IIS\Microsoft.Web.Iis.Rewrite.dll的引用。

    4. 在项目属性中创建一个强名称密钥(在签名选项卡上)

    5. 添加构建后事件以在您的开发 PC GAC 中安装提供程序程序集(仅用于测试):

      CALL "%VS90COMNTOOLS%\vsvars32.bat" > NULL 
      gacutil.exe /if "$(TargetPath)"
      

      注意,如果使用 Visual Studio 2010,则环境变量 %VS90COMNTOOLS% 应更改为 %VS100COMNTOOLS%

    6. 打开ServerName.cs 文件并确保您的类实现了Microsoft.Web.Iis.Rewrite.IRewriteProvider 接口:

      using System.Collections.Generic;
      using Microsoft.Web.Iis.Rewrite;
      
      namespace ServerNameVariable
      {
        public class ServerName : IRewriteProvider
        {
          public void Initialize(IDictionary<string, string> settings, 
                             IRewriteContext rewriteContext)
          {
          }
      
          public string Rewrite(string value)
          {
            return  System.Environment.MachineName;
          }
        }
      }
      
    7. 构建项目。在您的本地 PC 上,程序集将安装到 GAC 中。

    8. 向 IIS 注册提供程序:

    appcmd.exe set config -section:system.webServer/rewrite/providers /+"[name='ServerNameVariable',type='ServerNameVariable.ServerName, ServerNameVariable, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5854ff76fb5c07af']" /提交:应用程序主机

    确保上面命令行中的PublicKeyToken 值与您的程序集的公钥令牌相匹配。您可以通过 sn.exe -T &lt;assemblyfile.dll&gt; 提取该值,例如:

    e:\AppDev\..bin\Debug> sn.exe -T ServerNameVariable.dll Microsoft (R) .NET Framework 强名称实用程序版本 4.0.30319.1 版权所有 (c) 微软公司。版权所有。 公钥令牌是 5854ff76fb5c07af

    接下来要做的是在重写规则中使用您的新提供程序,例如,这是 Scott 给出的出站规则示例:

    <outboundRules>
        <rule name="Set Custom Header" enabled="true">
            <match serverVariable="RESPONSE_X_Machine_Name" pattern=".*" />
             <action type="Rewrite" value="{ServerNameVariable:}" />
        </rule>
    </outboundRules>
    

    这是一个简单的入站规则,如果机器名称是BOB,它会重定向到 google.com:

    <rewrite>
        <rules>
            <rule name="TestServerVariableProvider" 
                  enabled="true" 
                  stopProcessing="true">
                <match url=".*" />
                <conditions>
                    <add input="{ServerNameVariable:}" pattern="BOB" />
                </conditions>
                <action type="Redirect" 
                        url="http://google.com" 
                        appendQueryString="false" 
                        redirectType="Found" />
            </rule>
        </rules>
    </rewrite>
    

    【讨论】:

      猜你喜欢
      • 2017-07-09
      • 2011-03-24
      • 2022-10-19
      • 2013-03-01
      • 1970-01-01
      • 2019-09-14
      • 1970-01-01
      • 2017-07-07
      • 2018-03-12
      相关资源
      最近更新 更多