在 Azure 应用服务 IIS 上使用 responseMode executeURL 使用 httpErrors 自定义 404 错误时需要保留 HTTP 状态代码 [英] Need to preserve HTTP status code when using httpErrors custom 404 error using responseMode executeURL on Azure App Service IIS

查看:33
本文介绍了在 Azure 应用服务 IIS 上使用 responseMode executeURL 使用 httpErrors 自定义 404 错误时需要保留 HTTP 状态代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要所有缺失的内容/坏"的内容URL 重定向到我们的自定义 404.html 错误页面.这对于在 Google Analytics 中准确记录 404 错误非常重要.

问题在于,当设置了 responseMode=ExecuteURL 标志时,自定义错误不会保留 404 状态代码,而是始终显示 200 代码.我可以将其更改为 responseMode=Redirect,但这会在重定向到自定义 404.html 页面之前显示 302 状态代码.

所有这些确实与文件"一起工作.在 httpError 上设置的标志……只是没有使用ExecuteURL";我们的服务器端 Perl 所需的标志包括用于呈现页眉/页脚页面元素.

理想情况下,我们应该能够使用 Azure App Service IIS web.config 将自定义错误设置为:

  • 始终在地址栏(和开发工具)中保留/显示所请求(缺失)的 URL 请求
  • 始终保留/展示真实的";开发工具中的 HTTP 状态代码 (404)
  • 允许使用服务器端包含来使用我们当前的 Perl 设置更新页眉/页脚元素

以下代码用于在地址栏中保留请求的 URL,正确显示带有服务器端页眉/页脚内容的自定义 404.html 页面,但是在开发工具中丢失了 404 状态代码(和谷歌分析)...

<httpErrors><删除状态代码=404";subStatusCode="-1";/><错误状态代码=404"responseMode="ExecuteURL";路径=/404.html"/></httpErrors>

更改为 responseMode=重定向";仅在重定向到自定义 404.html 之前将状态码更改为 302...

如果我改为使用 responseMode="File";这一切都很好,但是我失去了使用 Perl 服务器端包含处理的自定义服务器端页眉和页脚......

需要明确的是,自定义 404 页面都是 HTML 和 Javascript,但 利用一些 非常 旧的 Perl 服务器端包含向页面添加自定义页眉和页脚元素.我们没有使用任何 .NET 框架或 .NET 核心页面...

这种安排应该是可能的,但也许只能使用不同的 Web 服务器,而不是 IIS?也许是 nginx?

最终更新:不是一个完整的答案,但我们近期的解决方案是使用 nginx 代理配置(已经存在并且可以在 nginx.conf 中更改)来保留 404 错误代码并显示正确的自定义 404.html 静态文件.

我也可以用 Docker 和 nginx 做到这一点,所以我知道 Web 服务器可以处理这种情况......

我已经确定 AFAICT 没有办法让 IIS web.config 不使用 Jason Pan 建议的服务器端代码来处理这个问题.因此,尽管他可能是正确的,但该答案对我们的需求没有帮助.

解决方案

更新

当您在 web.config 中使用 httpErrors 时,您的项目中必须有代码来处理服务器端的 400500.

由于您的项目只是静态 Web 应用程序,没有服务器端代码.所以我建议你可以在 httpErrors 中使用硬代码.

喜欢,

.

httpErrors的标签用于iis等服务器.你想要的 404500 错误不能直接在浏览器中显示.因为使用了httpErrors标签,所以服务器会处理一切,返回给浏览器,所以你得到的HttpStaus总是200.

私人

我可能知道你的问题.你的程序是.net framework还是.net core,暂时不清楚.但是我在 web.config 文件中看到了配置标签.

原则上错误的请求到达IIS等服务器,代码层面已经处理了404等错误,所以返回HttpStatus 值必须是 200.没办法改,但是当出现404或者500错误时,我们可以在Application_Error中处理记录也可以达到目的你想分析.

所以我测试了一下,基于.net框架,你可以

<强>2.根据Application_Error方法,添加一个ErrorController.

3.执行测试,结果显示在屏幕截图中.

解码后的消息.内容是

找不到路径/a/b"的控制器或未实现 IController..

我们可以在 Application_Error 函数中自定义错误消息.我们可以附加 HttpCode 和其他信息.

I want to have all missing content/"bad" URLs redirect to our custom 404.html error page. This is important for accurately recording 404 errors in Google Analytics.

The issue is that when the responseMode=ExecuteURL flag is set, then the custom error does not preserve the 404 status code, but always shows a 200 code. I can change this to responseMode=Redirect, but this then shows a 302 status code, before redirecting to the custom 404.html page.

All of this DOES work with a "File" flag set on the httpError… just not with the "ExecuteURL" flag which is required for our server-side Perl includes used to present Header/Footer page elements.

Ideally, we should be able to use the Azure App Service IIS web.config to set a custom error to:

  • always preserve/show the requested (missing) URL request in address bar (and dev tools)
  • always preserve/show the "real" HTTP status code (404) in the dev tools
  • allow the use of server-side includes to update header/footer elements using our current Perl setup

The below code works to preserve the requested URL in address bar, correctly shows the custom 404.html page with server-side header/footer content, BUT loses the 404 status code in dev tools (and Google Analytics)...

<httpErrors>
      <remove statusCode="404" subStatusCode="-1" />
      <error statusCode="404" 
          responseMode="ExecuteURL" 
          path="/404.html" />
</httpErrors>

Changing to responseMode="Redirect" only changes the status code to 302 before redirecting to custom 404.html...

If I change to use responseMode="File" this all works fine, but I then lose the custom server-side header and footers which are handled with Perl server-side includes...

EDIT: To be clear, the custom 404 page is all HTML and Javascript, but also leverages some very old Perl server-side includes to add custom header and footer elements to the page. We are not using any .NET framework or .NET core pages...

This arrangement should be possible, but perhaps only with a different web server, not IIS? nginx, perhaps?

FINAL UPDATE: Not a full answer, but our near-term resolution was to use nginx proxy configuration (which was already present and could be altered in nginx.conf) to preserve 404 error codes and present the proper custom 404.html static file.

I was also able to do this with Docker and nginx, so I know it is possible for a web server to deal with this situation...

I've determined that AFAICT there is no way for IIS web.config to handle this without using server-side code as Jason Pan suggested. So while he may be correct, that answer was not helpful for our needs.

解决方案

UPDATE

When you use httpErrors in web.config, it must have code in your project to handle 400 and 500 in server side.

Due to your project just static web app, and no sever side code. So I suggest you can use hard code in httpErrors.

Like,

<error statusCode="404" responseMode="ExecuteURL" path="/404.html?httpcode=404" />.

The tag of httpErrors is used in servers such as iis. The 404 and 500 errors you want can’t be displayed directly in the browser. Because the httpErrors tag is used, the server will process everything and return it to the browser, so the HttpStaus you get is always 200.

PRIVIOUS

I probably know your question. Your program is .net framework or .net core, it is not clear for the time being. But I see the configuration tags in the web.config file.

In principle, the wrong request arrives at IIS and other servers, and the code level has processed 404 and other errors, so the returned HttpStatus value must be 200. There is no way to change it, but when a 404 or 500 error occurs, we can Processing and recording in Application_Error can also achieve the purpose you want to analyze.

So I tested it and based on the .net framework, you can download my sample code on github and give the following suggestions.

1. Add the Application_Error method to the Global.asax.cs file.

2. According to the Application_Error method, add an Error Controller.

3. Perform the test and the result is shown in the screenshot.

After decode the message. The content is

The controller for path '/a/b' was not found or does not implement IController..

We can custom error msg in Application_Error function. We can append HttpCode and other info.

这篇关于在 Azure 应用服务 IIS 上使用 responseMode executeURL 使用 httpErrors 自定义 404 错误时需要保留 HTTP 状态代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆