zoukankan      html  css  js  c++  java
  • 【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%homesitewwwroot" />
        <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

     

     

    当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!

  • 相关阅读:
    java 8
    内存溢出VS内存泄漏
    dubbo zk 分布式服务项目搭建与配置
    转发 VS 重定向
    过滤器
    Synchronized
    java 泛型
    spring 整合 mongo
    泛型
    反虚拟机
  • 原文地址:https://www.cnblogs.com/lulight/p/15220297.html
Copyright © 2011-2022 走看看