【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

 

目录
打赏
0
0
0
0
194
分享
相关文章
【Azure App Service】基于Linux创建的App Service是否可以主动升级内置的Nginx版本呢?
基于Linux创建的App Service是否可以主动升级内置的Nginx版本呢?Web App Linux 默认使用的 Nginx 版本是由平台预定义的,无法更改这个版本。
126 77
|
19天前
|
C#
【Azure Function】Function App出现System.IO.FileNotFoundException异常
Exception while executing function: xxxxxxx,The type initializer for 'xxxxxx.Storage.Adls2.StoreDataLakeGen2Reading' threw an exception. Could not load file or assembly 'Microsoft.Extensions.Configuration, Version=9.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the
114 64
【Azure Function】Function App门户上的Test/Run返回错误:Failed to fetch
Running your function in portal requires the app to explicitly accept requests from https://portal.azure.cn. This is known as cross-origin resource sharing (CORS).Configure CORS to add https://portal.azure.cn to allowed origins.
Python3+PyCharm环境的安装及配置
近期碰到有同学入门Python还不会安装并配置Python编程环境的,在这里做一期教程手把手教大家安装与配置使用(以 Python 3.9.9 以及 PyCharm 2021.3.1 为例)
737 0
Python3+PyCharm环境的安装及配置
|
9月前
|
新手向 Python:VsCode环境下Manim配置
该文介绍了如何准备和配置开发环境以使用Manim,主要包括两个步骤:一是准备工作,需要下载并安装VsCode和Anaconda,其中Anaconda需添加到系统PATH环境变量,并通过清华镜像源配置;二是配置环境,VsCode中安装中文插件和Python扩展,激活并配置虚拟环境。最后,安装ffmpeg和manim,通过VsCode运行测试代码验证配置成功。
690 1
python入门(一)conda的使用,创建修改删除虚拟环境,以及常用命令,配置镜像
python入门(一)conda的使用,创建修改删除虚拟环境,以及常用命令,配置镜像
827 0
python+Django+Mysql+Echarts数据可视化实战教程(2):Django环境下web目录的配置
python+Django+Mysql+Echarts数据可视化实战教程(2):Django环境下web目录的配置
367 0
基于Windows下Pycharm和Anaconda的python虚拟环境连接配置及更换项目虚拟环境方法
基于Windows下Pycharm和Anaconda的python虚拟环境连接配置及更换项目虚拟环境方法
531 0
基于Windows下Pycharm和Anaconda的python虚拟环境连接配置及更换项目虚拟环境方法

热门文章

最新文章

推荐镜像

更多
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等