IIS 页面 URL 重写给出 HTTP 错误 400,参数字符串长度超过 260 个字符

IIS page URL rewrite gives a HTTP Error 400 with parameter string longer than 260 characters long

我的环境如下:

IIS 版本为 10.0.17763.1 运行 Windows Server 2019 数据中心

在远程服务器上,我在端口 8041 上有一个休息服务器(作为 windows 服务)运行,它接受 http 调用。我创建了一个在端口 9010 上运行的 IIS 网站,该网站通过重定向到 8041 端口上的服务 运行 的 https 域重写调用,反之亦然。我可以分别访问这两个服务,它们在以下情况下都可以正常工作:

8041 端口上的服务 运行:

http://:8041/rest/AccountModule/GetUserSettings/<大于260个字符的参数字符串>

http://:8041/rest/AccountModule/GetUserSettings/<小于260个字符的参数串 https://:9010/rest/AccountModule/GetUserSettings/<小于260个字符的参数串>

网站 运行 在端口 9010 上:

https://:9010/rest/AccountModule/GetUserSettings/<长度小于260个字符的参数串>

但是以下情况不起作用:

**https://:9010/rest/AccountModule/GetUserSettings/<大于260个字符的参数串 https://:9010/rest/AccountModule/GetUserSettings/<小于260个字符的参数串字符长>

**

调用出现以下错误:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Bad Request</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Bad Request - Invalid URL</h2>
<hr><p>HTTP Error 400. The request URL is invalid.</p>
</BODY></HTML>```

I have tried many things such as configuration editor changes in IIS increase the limits maxRequestLength, maxUrlLength and maxQueryStringLength and adding new DWORD values to the registry files (Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\HTTP\Parameters). They are as follows and none of them seems to work. 

[IIS-Configuration-Editor-Changes][1]
[Registry_Changes_Http][2]


my web config file content is as follows:


    <?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="ReverseProxyInboundRule1" stopProcessing="true">
                    <match url="(.*)" />
                    <action type="Rewrite" url="http://localhost:8041/{R:1}" logRewrittenUrl="true" />
                </rule>
            </rules>
        </rewrite>
    <security>
      <requestFiltering allowDoubleEscaping="true">
        <requestLimits maxAllowedContentLength="50000000" maxUrl="6000000" maxQueryString="6000000">
                    <headerLimits>
                    </headerLimits>
                </requestLimits>
      </requestFiltering>
    </security>
    </system.webServer>
    <appSettings>
        <add key="httpRuntime" value="maxUrlLength" />
    </appSettings>
    <system.web>
        <httpRuntime maxRequestLength="50000" useFullyQualifiedRedirectUrl="true" maxUrlLength="50000" maxQueryStringLength="50000" />
    </system.web>
</configuration>

Help on fixing this long URL parameters (longer than 260 characters) is much appreciated.

Thanks
Dan


  [1]: https://i.stack.imgur.com/q82p6.png
  [2]: https://i.stack.imgur.com/xIoRq.png

这些配置修改完成后,需要重启http服务才能生效:

  1. 运行 搜索栏中的命令提示符
  2. 在命令提示符下,键入 net stop http,然后按 Enter
  3. 在命令提示符下,键入 net start http,然后按 Enter

我注意到你的一些参数的值没有设置为最大值,你可以试试下面的配置参数:

  1. 修改http运行time节点增加maxQueryStringLength,maxRequestLength配置:

    <httpRuntime requestPathInvalidCharacters="" requestValidationMode="2.0" maxQueryStringLength="2097151" maxUrlLength="2097151" maxRequestLength="2097151" relaxedUrlToFileSystemMapping="true" executionTimeout="36000" delayNotificationTimeout="36000" />
    
  2. 修改system.webServer节点:

    <security>
      <requestFiltering allowDoubleEscaping="true" allowHighBitCharacters="true"  >
        <requestLimits maxAllowedContentLength="2097151" maxQueryString="2097151" maxUrl="2097151" />
      </requestFiltering>
    </security>
    

根据Microsoft的link,您可以修改注册表中的以下两个值:

UrlSegmentMaxCount的设置同上,值也是Dword 2048。