【Azure 应用服务】App Service For Windows 环境中部署Python站点后,如何继续访问静态资源文件呢(Serving Static Files)?

简介: 【Azure 应用服务】App Service For Windows 环境中部署Python站点后,如何继续访问静态资源文件呢(Serving Static Files)?

问题描述

当创建一个App Service 后,运行时环境和版本选择Windows 和 Python 3.6. 登录Kudu 站点查看,默认的文件有 web.config, hostingstart-python.py, hostingstart-python.html,  在配置文件中,通过pythonpath来指定启动目录,而 WSGI_HANDLER 则指定启动的py文件为 hostingstart-python.py.

web.config

<configuration>
  <appSettings>
    <add key="pythonpath" value="%SystemDrive%\home\site\wwwroot" />
    <add key="WSGI_HANDLER" value="hostingstart-python.application" />
  </appSettings>
</configuration>

hostingstart-python.py 文件中定义了应用的返回内容为hostingstart-python.html中的内容

import sys
import platform
def application(environ, start_response):
    start_response(b'200 OK', [(b'Content-Type', b'text/html')])
    with open ("hostingstart-python.html", "r") as hostingstart_file:
        hosting = hostingstart_file.read()
        yield hosting.encode('utf8').replace(b'PYTHON_VERSION', platform.python_version().encode('utf8'))

hostingstart-python.html

<html>

<body>test from other root folder start Python project from wwwroot....</body>

</html>

当访问站点时候,就会把 hostingstart-python.html 中的内容显示到首页。但是当站点中也需要部署一些静态html文件时,发现不管如何修改URL,始终返回的内容都是hostingstart-python.html 。

 

由于Python应用的启动文件中强制返回的内容为hostingstart-python.html,而且没有配置route,所以不管是什么URL访问到此站点,永远输出都是同样内容,因为处理请求的进程是Python.exe, 而非w3wp.exe

 

问题解决

如何使用IIS来处理静态页面的请求呢?实现Python 站点也能通过URL访问到正确的静态文件(Serving Static Files): 有的。在静态文件夹中添加 web.config 文件,并添加以下内容:

<?xml version="1.0"?>  
<configuration>  
    <system.webServer>
        <handlers>
           <clear />
            <add 
                name="StaticFile"
                path="*" verb="*" modules="StaticFileModule,DefaultDocumentModule,DirectoryListingModule" 
                resourceType="Either" 
                requireAccess="Read" />
        </handlers>
    </system.webServer>
</configuration>
  • 它告诉IIS这是一个静态资源文件,只需要StaticFileModule 等就可以解析,不需要使用Python wfastcgi模块

 

注意:添加web.config文件后,需要重启站点(App Service)。 然后就可以自由查看静态页面内容。

 

 

附录:另外也可以通过在wwwroot目录中的web.config中配置URL重写的规则,来实现对静态文件的访问

添加如下的Rewrite规则:

<system.webServer>
    <rewrite>
      <rules>
        <rule name="Static Files" stopProcessing="true">
          <conditions>
            <add input="true" pattern="false" />
          </conditions>
        </rule>
        <rule name="Configure Python" stopProcessing="true">
          <match url="(.*)" ignoreCase="false" />
          <conditions>
            <add input="{REQUEST_URI}" pattern="^/static/.*" ignoreCase="true" negate="true" />
          </conditions>
          <action type="Rewrite" url="handler.fcgi/{R:1}" appendQueryString="true" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>

 

参考资料

Django app with HttpPlatformHandler in Azure App Services (Windows) (Serving Static Files)https://azureossd.github.io/2017/09/01/django-app-with-httpplatformhandler-in-azure-app-services-windows/

 

如何在 Azure 应用服务 (Windows) 上设置 Python 环境https://docs.microsoft.com/zh-cn/visualstudio/python/managing-python-on-azure-app-service?view=vs-2019

 

相关文章
|
2月前
|
存储 安全 数据安全/隐私保护
【Azure Function App】在Function App中使用System Managed Identity访问Storage Account
本文介绍了如何在Azure Function中使用托管身份(Managed Identity)替代AzureWebJobsStorage连接函数应用到存储账户,以提高安全性并减少Access Key的使用。具体步骤包括:1) 启用系统分配的身份;2) 为函数应用授予存储访问权限,添加必要角色(如Storage Blob Data Contributor);3) 配置`AzureWebJobsStorage__blobServiceUri`参数指定Blob Service Uri。完成后删除旧配置,即可通过Managed Identity访问Storage Account。
107 20
|
30天前
|
数据采集 Web App开发 JavaScript
基于Selenium的Python爬虫抓取动态App图片
基于Selenium的Python爬虫抓取动态App图片
205 68
|
29天前
|
数据采集 存储 Web App开发
逆向音乐APP:Python爬虫获取音乐榜单
逆向音乐APP:Python爬虫获取音乐榜单
187 58
|
22天前
|
安全 Linux 开发工具
【Azure Function】分享把Function App从.NET 6.0升级到.NET 8.0 Isolated的步骤
本文介绍了将Azure Function App从.NET 6.0升级到.NET 8.0 Isolated的步骤。.NET 6.0作为长期支持版本,生命周期至2024年11月结束。为确保持续支持,建议升级至更新版本。升级步骤包括安装.NET 8 SDK、更新Azure Functions Core Tools、修改项目文件目标框架为net8.0、更新兼容的NuGet包、本地测试以及重新发布到Azure。更多详细信息可参考官方文档。
|
1月前
|
API Go 网络架构
【Azure Logic App】特殊的方法来停止正常步骤无法停止的Workflow Job
本文介绍了一种特殊方法,用于解决标准版Logic App在异常情况下无法正常停止的问题。当点击Cancel按钮报错“WorkflowRunCanNotBeCancelled”时,可通过以下步骤解决:进入Logic App的Kudu页面,定位到`C:\home\site\wwwroot`目录下的`host.json`文件,添加`Jobs.SuspendedJobPartition`和`Jobs.CleanupJobPartition`参数,并以大写格式设置值为`&quot;&lt;WORKFLOWID&gt;:2D&lt;RUNID&gt;&quot;`。调整后可成功停止异常Job
77 18
|
2月前
【Logic App】获取Azure Logic App在执行的标识值 Identifier
本文介绍了如何在Logic App中记录执行标识符以方便问题排查。通过添加一个变量(如RequestTrackingID),将其值设为`workflow()[&#39;run&#39;][&#39;name&#39;]`,可实现将Logic App执行的Identifier与业务数据关联。这样,在排查问题时,可通过日志中的关键信息快速定位对应的Logic App执行记录,提升效率。文中还提供了操作步骤及参考资料,帮助用户更好地理解和实现该方法。
73 10
|
5月前
|
存储 数据采集 数据处理
如何在Python中高效地读写大型文件?
大家好,我是V哥。上一篇介绍了Python文件读写操作,今天聊聊如何高效处理大型文件。主要方法包括:逐行读取、分块读取、内存映射(mmap)、pandas分块处理CSV、numpy处理二进制文件、itertools迭代处理及linecache逐行读取。这些方法能有效节省内存,提升效率。关注威哥爱编程,学习更多Python技巧。
145 8
|
10月前
|
SQL JSON 关系型数据库
n种方式教你用python读写excel等数据文件
n种方式教你用python读写excel等数据文件
143 1
|
存储 Python 内存技术
python WAV音频文件处理—— (1)读写WAV文件
python WAV音频文件处理—— (1)读写WAV文件
261 14
|
Python
python读写execle文件数据
python读写execle文件数据
110 0

推荐镜像

更多