【问题标题】:Not able to serve Python Flask app from IIS Web Application无法从 IIS Web 应用程序提供 Python Flask 应用程序
【发布时间】:2018-08-17 18:12:42
【问题描述】:

我正在尝试从默认网站下的 IIS Web 应用程序提供 Flask 应用程序,但无法使其正常工作。以下是详细信息:

OS: Windows Server 2016 DataCenter Edition
IIS: Installed with all options including CGI
IIS Rewrite Module: Version 2 installed

我已采取的步骤:

  1. 安装带有所有组件的 IIS(使用 CGI)
  2. 安装 Python 2.7.14
  3. PATH变量中添加Python
  4. pip install wfastcgi
  5. wfastcgi-enable
  6. pip install flask
  7. iisreset
  8. 创建目录:C:\inetpub\wwwroot\flask-demo
  9. 点击 IIS 中的Convert to Application 进入上述目录。
  10. 使用以下内容创建C:\inetpub\wwwroot\flask-demo\myapp.py
from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'

if __name__ == "__main__":
    app.run()
  1. 使用以下内容创建C:\inetpub\wwwroot\flask-demo\web.config
<configuration>
  <system.webServer>
    <handlers>
      <remove name="Python FastCGI" />
      <add name="Python FastCGI"
           path="*"
           verb="*"
           modules="FastCgiModule"
           scriptProcessor="C:\Python27\python.exe|C:\Python27\Lib\site-packages\wfastcgi.pyc"
           resourceType="Unspecified"
           requireAccess="Script" />
    </handlers>
   <rewrite>
 <rules>
 <rule name="asset-url-rewrite" stopProcessing="true">
 <match url="static" />
 <conditions>
 </conditions>
 <action type="Rewrite" url="flask-demo/{R:0}" />
 </rule>
 <rule name="app-url-rewrite" stopProcessing="true">
 <match url="[a-zA-Z]+" />
 <conditions>
 </conditions>
 <action type="Rewrite" url="flask-demo/" />
 </rule>
 </rules>
 </rewrite>
  </system.webServer>
  <appSettings>
    <add key="WSGI_HANDLER" value="myapp.app" />
    <add key="PYTHONPATH" value="C:\inetpub\wwwroot\flask-demo" />
  </appSettings>
</configuration>

问题

当我尝试以 http://localhost/flask-demo 访问页面时,我得到 404。但是如果我将 myapp.py 中的第二行从 @app.route('/') 更改为 @app.route('/flask-demo') 其中flask-demo 是 IIS Web 应用程序的名称,其中 Python文件已放置,它可以工作。

我希望在不写flask-demo/放置 Python 烧瓶应用程序的 IIS Web 应用程序的名称的情况下提供 Python 网页。我做不到。

我尝试使用app.config['APPLICATION_ROOT'] = '/flask-demo',但没有成功。

让这个工作的最佳方法是什么?关注文章https://medium.com/@bilalbayasut/deploying-python-web-app-flask-in-windows-server-iis-using-fastcgi-6c1873ae0ad8,但没有帮助。

【问题讨论】:

  • 您不能将多个服务连接到同一个端口!请检查错误日志。请同时检查网络或环回访问权限。
  • 它是 IIS 托管,从同一个端口为多个 Web 应用程序提供服务应该没有任何问题。它应该可以工作(它适用于 ASP.NET 应用程序)。这是我需要帮助的代码/方法中的问题。
  • flask 广播哪个端口? It's IIS hosting and there should not be any problem for serving multiple web applications 完全错误的想法(没有任何规则),!不要混淆 CGI 和 SERVER(服务)术语。如果ISS服务运行,则无法监听80端口!
  • 没有广播。如果您使用 medium.com 链接 - 它说这应该有效。在我的例子中,IIS 通过 Fast CGI 与 Python 对话。
  • 1- 尝试没有应用程序的 CGI 脚本 2-检查文件权限 3-检查要触发的应用程序(python,什么是 python?) 4- ISS 不能跟随系统路径,它需要特殊授权离开给定的目录。 5-主要广播服务地址转发(ISS处理地址块>> CGI脚本)不知道错过了。

标签: python iis flask


【解决方案1】:

URL 中包含flask-demo,所以 Flask 看到并使用它是正常的。

有一些解决方法,例如:

Add a prefix to all Flask routes(在这个网站上)。

总的来说:你想要什么? URL 比底层技术重要得多(并且 URL 应该是稳定的,在您更改实现时也是如此)。我建议不要在 URL 中包含flask-demo(并且永远不要使用测试和演示,无论如何,演示将永远保留,不幸的是)。所以我会修改 ISS,让它将所有动态页面重定向到flask-demo

【讨论】:

  • 我确实尝试了该解决方案,但它也无法正常工作。在我原来的问题中也提到过。 APPLICATION_ROOT 被忽略。
  • 再次阅读链接。它说APPLICATION_ROOT 处理 cockies 等等。您的 WSGI(和类似的)应该处理 URL,他们应该删除路由前缀(对于 URL)。所以这部分(和配置)在 IIS 端。
  • 显然,我也尝试过。它没有用。刚刚在问题中为我的 web.config 启用了正确的可视化。关于错误的任何指针?
  • 没有任何帮助。尝试了原始扩展,测试并恢复为快速 CGI (wfastcgi)。
【解决方案2】:

经过多次尝试,我已经找到了以下解决方案。

解决方案警告

这假设 Python 应用程序仅托管在 IIS 网站的第一深度。以下解决方案不支持二级部门。

新的 myapp.py

from flask import Flask
import os
app = Flask(__name__)

ROOT_PREFIX = '/' + os.path.dirname(os.path.realpath(__file__)).rsplit('\\', 1)[-1]

@app.route(ROOT_PREFIX  + '/')
def hello_world():
    return 'Hello, World!'

if __name__ == "__main__":
    app.run()

新的 web.config

<configuration>
    <system.webServer>
        <modules>
            <remove name="WebDAVModule" />
        </modules>
        <handlers>
            <remove name="WebDAV" />
            <add name="Python FastCGI"
                        path="*"
                        verb="*"
                        modules="FastCgiModule"
                        scriptProcessor="C:\Python27\python.exe|C:\Python27\Lib\site-packages\wfastcgi.pyc"
                        resourceType="Unspecified"
                        requireAccess="Script" />
        </handlers>
    </system.webServer>
    <appSettings>
        <add key="WSGI_HANDLER" value="myapp.app" />
        <add key="PYTHONPATH" value="C:\inetpub\wwwroot\flask-demo" />
    </appSettings>
</configuration>

【讨论】:

    猜你喜欢
    • 2014-11-29
    • 2016-01-07
    • 1970-01-01
    • 2011-12-15
    • 2017-02-07
    • 2023-03-14
    • 2016-10-30
    • 1970-01-01
    • 2016-02-18
    相关资源
    最近更新 更多