【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 。

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

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

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

问题解决

如何使用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)。 然后就可以*查看静态页面内容。

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

附录:另外也可以通过在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

上一篇:django生产环境中部署


下一篇:MS SQL 事物日志传送能否跨数据库版本吗?