【Azure 应用服务】App Service 通过配置web.config来添加请求返回的响应头(Response Header)

简介: 【Azure 应用服务】App Service 通过配置web.config来添加请求返回的响应头(Response Header)

问题描述

在Azure App Service上部署了站点,想要在网站的响应头中加一个字段(Cache-Control),并设置为固定值(Cache-Control:no-store)

效果类似于本地IIS中设置IIS响应标头

 

有时,也会根据不同的安全要求,需要添加Response Header,如下:

#Adding security headers
X-Frame-Options "SAMEORIGIN"
X-Xss-Protection "1; mode=block"
X-Permitted-Cross-Domain-Policies "none"
X-Content-Type-Options "nosniff"
Expect-CT "max-age=86400, enforce"
Referrer-Policy "strict-origin-when-cross-origin"
Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
Content-Security-Policy "block-all-mixed-content; frame-ancestors 'self'; form-action 'self'; object-src 'none'; base-uri 'self';"
Permissions-Policy "accelerometer=(self), camera=(self), geolocation=(self), gyroscope=(self), magnetometer=(self), microphone=(self), payment=(self), usb=(self)"

 

问题解决

在App Service的web.config文件中添加配置Cache-Control为no-store,可以登录到 kudu( https://<your site name>.scm.chinacloudsites.cn/DebugConsole ) 站点查看 wwwroot 下是否存在 web.config 文件,如果没有可以新建一个,web.config配置参考如下:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.webServer>
          <httpProtocol>
      <customHeaders>
        <clear />
        <add name="Cache-Control" value="no-store" />    
      </customHeaders>
    </httpProtocol>
    </system.webServer>
</configuration>

 

同理,如果是需要加上安全相关的Header,追加即可。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.webServer>
          <httpProtocol>
      <customHeaders>
        <clear />
        <add name="Cache-Control" value="no-store" />    
        
        <!-- #Adding security headers -->
        <add name="X-Frame-Options" value="SAMEORIGIN" />   
        <add name="X-Xss-Protection" value="1; mode=block" />   
        <add name="X-Permitted-Cross-Domain-Policies" value="none" />   
        <add name="X-Content-Type-Options" value="nosniff" />   
        <add name="Expect-CT" value="max-age=86400, enforce" />   
        <add name="Referrer-Policy" value="strict-origin-when-cross-origin" />   
        <add name="Strict-Transport-Security" value="max-age=31536000; includeSubDomains; preload" />   
        <add name="Content-Security-Policy" value="block-all-mixed-content; frame-ancestors 'self'; form-action 'self'; object-src 'none'; base-uri 'self';" />   
        <add name="Permissions-Policy" value="accelerometer=(self), camera=(self), geolocation=(self), gyroscope=(self), magnetometer=(self), microphone=(self), payment=(self), usb=(self)" />   
      </customHeaders>
    </httpProtocol>
    </system.webServer>
</configuration>

HTTP 消息头允许客户端和服务器通过 request response传递附加信息。一个请求头由名称(不区分大小写)后跟一个冒号“:”,冒号后跟具体的值(不带换行符)组成。该值前面的引导空白会被忽略。

  • X-Frame-Options : 用来给浏览器 指示允许一个页面 可否在 <frame>, <iframe>, <embed> 或者 <object> 中展现的标记。站点可以通过确保网站没有被嵌入到别人的站点里面,从而避免 clickjacking 攻击。 SAMEORIGIN 表示该页面可以在相同域名页面的 frame 中展示。
  • X-XSS-Protection : 是 Internet Explorer,Chrome 和 Safari 的一个特性,当检测到跨站脚本攻击 (XSS (en-US))时,浏览器将停止加载页面。1;mode=block 启用XSS过滤。 如果检测到攻击,浏览器将不会清除页面,而是阻止页面加载。
  • X-Permitted-Cross-Domain-Policies : 用于允许来自 Flash 和 PDF 文档的跨域请求。none 表示完全阻止通过不同域集成 Flash 和 PDF 文档。
  • X-Content-Type-Options : 相当于一个提示标志,被服务器用来提示客户端一定要遵循在 Content-Type 首部中对  MIME 类型 的设定,而不能对其进行修改。nosniff 只应用于 "script" 和 "style" 两种类型
  • Expect-CT : 允许站点选择性报告和/或执行证书透明度 (Certificate Transparency) 要求,来防止错误签发的网站证书的使用不被察觉。max-age=86400, enforce 指定24小时的证书透明度执行
  • Referrer-Policy : 用来监管哪些访问来源信息——会在 Referer  中发送——应该被包含在生成的请求当中。strict-origin-when-cross-origin 对于同源的请求,会发送完整的URL作为引用地址
  • Strict-Transport-Security : 是一个安全功能,它告诉浏览器只能通过HTTPS访问当前资源,而不是HTTP。
  • Content-Security-Policy : 允许站点管理者控制用户代理能够为指定的页面加载哪些资源。
  • Permissions-Policy : 允许web开发者在浏览器中选择启用、禁用和修改确切特征和 API 的行为

 

问:在App Service for Linux(Node JS 应用) 中是否可以修改Header呢?

答:不可以,App Service for Linux是无法修改服务端配置的,所以无法通过服务端配置添加header的,但是可通过代码方式自行添加Security Header。使用response.setHeader(name, value)方法即可

示例代码如:

const http = require('http');
const server = http.createServer((request, response) => {
    //使用SetHeader添加响应头
    response.setHeader('Content-Type', 'text/html');
    response.setHeader('X-Foo', 'bar');
    response.setHeader("Access-Control-Allow-Origin", "*"); 
 
    response.writeHead(200, {"Content-Type": "text/plain"});
    response.end("Hello World!");
});
const port = process.env.PORT || 1337;
server.listen(port);
console.log("Server running at http://localhost:%d", port);

 

 

参考资料

response.setHeader(name, value):http://nodejs.cn/api/http/response_setheader_name_value.html

Node.js Hello World (App Service): https://github.com/Azure-Samples/nodejs-docs-hello-world

在 Azure 中创建 Node.js Web 应用:https://docs.azure.cn/zh-cn/app-service/quickstart-nodejs?pivots=platform-linux

HTTP headershttps://developer.mozilla.org/en-US/docs/Web/HTTP/Headers#security  OR  https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers

X-PERMITTED-CROSS-DOMAIN-POLICIEShttps://www.scip.ch/en/?labs.20180308

 

【完】

相关文章
|
2月前
|
应用服务中间件 Linux 网络安全
【Azure 应用服务】App Service for Linux 环境中为Tomcat页面修改默认的Azure 404页面
【Azure 应用服务】App Service for Linux 环境中为Tomcat页面修改默认的Azure 404页面
|
2月前
|
Shell PHP Windows
【Azure App Service】Web Job 报错 UNC paths are not supported. Defaulting to Windows directory.
【Azure App Service】Web Job 报错 UNC paths are not supported. Defaulting to Windows directory.
|
2月前
|
Docker 容器
【Azure 应用服务】App Service for Container 无法拉取Docker Hub中的镜像替代方案
【Azure 应用服务】App Service for Container 无法拉取Docker Hub中的镜像替代方案
|
2月前
|
API C++
【Azure 应用服务】Azure Function App在部署时候遇见 503 ServiceUnavailable
【Azure 应用服务】Azure Function App在部署时候遇见 503 ServiceUnavailable
|
2月前
|
网络协议
【Azure 应用服务】Azure Data Factory中调用Function App遇见403 - Forbidden
【Azure 应用服务】Azure Data Factory中调用Function App遇见403 - Forbidden
|
1月前
|
数据库 开发者 Python
web应用开发
【9月更文挑战第1天】web应用开发
38 1
|
20天前
|
数据可视化 图形学 UED
只需四步,轻松开发三维模型Web应用
为了让用户更方便地应用三维模型,阿里云DataV提供了一套完整的三维模型Web模型开发方案,包括三维模型托管、应用开发、交互开发、应用分发等完整功能。只需69.3元/年,就能体验三维模型Web应用开发功能!
41 8
只需四步,轻松开发三维模型Web应用
|
11天前
|
安全 API 开发者
Web 开发新风尚!Python RESTful API 设计与实现,让你的接口更懂开发者心!
在当前的Web开发中,Python因能构建高效简洁的RESTful API而备受青睐,大大提升了开发效率和用户体验。本文将介绍RESTful API的基本原则及其在Python中的实现方法。以Flask为例,演示了如何通过不同的HTTP方法(如GET、POST、PUT、DELETE)来创建、读取、更新和删除用户信息。此示例还包括了基本的路由设置及操作,为开发者提供了清晰的API交互指南。
44 6
|
9天前
|
存储 JSON API
实战派教程!Python Web开发中RESTful API的设计哲学与实现技巧,一网打尽!
在数字化时代,Web API成为连接前后端及构建复杂应用的关键。RESTful API因简洁直观而广受欢迎。本文通过实战案例,介绍Python Web开发中的RESTful API设计哲学与技巧,包括使用Flask框架构建一个图书管理系统的API,涵盖资源定义、请求响应设计及实现示例。通过准确使用HTTP状态码、版本控制、错误处理及文档化等技巧,帮助你深入理解RESTful API的设计与实现。希望本文能助力你的API设计之旅。
33 3
|
11天前
|
JSON API 数据库
从零到英雄?一篇文章带你搞定Python Web开发中的RESTful API实现!
在Python的Web开发领域中,RESTful API是核心技能之一。本教程将从零开始,通过实战案例教你如何使用Flask框架搭建RESTful API。首先确保已安装Python和Flask,接着通过创建一个简单的用户管理系统,逐步实现用户信息的增删改查(CRUD)操作。我们将定义路由并处理HTTP请求,最终构建出功能完整的Web服务。无论是初学者还是有经验的开发者,都能从中受益,迈出成为Web开发高手的重要一步。
32 4
下一篇
无影云桌面