在Azure App Service 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

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

问题描述

我想让所有缺少的内容/不好"的内容URL重定向到我们的自定义404.html错误页面.这对于在Google Analytics(分析)中准确记录404错误非常重要.

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

所有 DOES 都可与文件"一起使用,在httpError上设置的标志…只是不与"ExecuteURL"一起使用;服务器端Perl所必需的标记,用于显示页眉/页脚页面元素.

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

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

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

 < httpErrors><删除statusCode ="404";subStatusCode =-1"./><错误statusCode ="404";responseMode ="ExecuteURL"path ="/404.html"/></httpErrors> 

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

如果我更改为使用responseMode ="File",一切正常,但随后我失去了使用Perl服务器端包含处理的自定义服务器端标头和页脚...

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

这种安排应该是可能的,但也许仅适用于其他Web服务器,而不是IIS?也许是nginx?

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

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

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

解决方案

更新

在web.config中使用 httpErrors 时,它必须在项目中包含代码,才能在服务器端处理 400 500 ./p>

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

就像

<错误statusCode ="404"responseMode ="ExecuteURL"path ="/404.html?httpcode = 404";/> .

httpErrors 的标记在诸如 iis 之类的服务器中使用.您想要的 404 500 错误无法直接在浏览器中显示.因为使用了 httpErrors 标记,服务器将处理所有内容并将其返回到浏览器,因此您获得的HttpStaus始终为 200 .

重要

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

原则上,错误的请求到达 IIS 和其他服务器,并且代码级别已处理 404 和其他错误,因此返回的 HttpStatus 值必须为 200 .无法更改它,但是当发生 404 500 错误时,我们可以在 Application_Error 中进行处理和记录也可以达到目的您要分析.

因此我进行了测试,并基于 .net框架,您可以

2.根据 Application_Error 方法,添加一个 Error 控制器.

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 App Service IIS上使用responseMode executeURL使用httpErrors自定义404错误时需要保留HTTP状态代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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