通过IIS在应用程序级别而不是网站级别通过FastCGI运行Flask [英] Running Flask via FastCGI in IIS at application level instead of website leve

查看:48
本文介绍了通过IIS在应用程序级别而不是网站级别通过FastCGI运行Flask的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在VENV中配置了基本的Flask Web示例,现在可以通过IIS通过FastCGI正确发布它了.

I've configured the basic Flask web sample in a VENV and now it is correctly published through IIS via FastCGI.

好吧,这是我的琐碎sample00.py

Well, here it is my trivial sample00.py

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello, World, from Flask!"

和web.config,涉及更多:

and the web.config, a little more involved:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
      <handlers>
        <add name="PythonHandler" path="*" verb="*" modules="FastCgiModule" 
         scriptProcessor="F:\Appl\flask\venv\Scripts\python.exe|F:\Appl\flask\venv\Lib\site-packages\wfastcgi.py" 
         resourceType="Unspecified" requireAccess="Script" />
      </handlers>
  </system.webServer>
    <appSettings>
      <add key="PYTHONPATH" value="F:\Appl\flask" />
      <!-- Flask apps only: change the project name to match your app -->
      <add key="WSGI_HANDLER" value="sample00.app" />
      <add key="WSGI_LOG" value="F:\Appl\flask\wfastcgi.log" />
    </appSettings>
</configuration>

经过一番尝试和错误,我发现我可以在IIS管理器中输入>处理程序映射>选择我的"PythonHandler",编辑可执行文件(可选)",然后检查路径是否正确并要求我激活处理程序,然后我可以回答是".最终一切都按预期工作.

After some trials and errors, I've discovered that I can enter in IIS Manager > Handler Mappings > select my "PythonHandler", edit the "Executable (optional)", and it checks if the path is correct and asks me to activate the handler, then I can answer "yes" and eventually it all works as expected.

到目前为止,一切都很好.现在,我不想在IIS中将其设置为单独的网站,而是将其设置为另一个现有网站下的新应用程序.甚至有可能还是有文件说不允许这样做?我们可以关闭该问题或将其标记为重复,但是直到现在我仍无法找到该特定问题的先前答案.无论如何,我都尝试这样做,但是我收到了 404 页面错误...就像如果请求未正确转发到cgi处理程序,因此我怀疑在管理中可能存在一些问题IIS应用程序级别的Flask应用程序,是这种情况还是我做错了什么?

So far so good. Now, I would like to setup it in IIS not as a separate website, but as a new application under another, existing, website. Is it even possible or is there a documentation saying that it is not permitted? We can close this question or mark it as duplicated, but till now I was not able to find a previous answer to this specific point. Anyway, I've tried to do that, but I receive a 404 page error... like if the request is not correctly forwarded to the cgi handler, hence I suspect there could be some problem in managing a Flask app at IIS application level, is it the case or what else am I doing wrong?

推荐答案

重点仅在于,在应用程序级别查看IIS中的基本设置",请参见下面的图像示例,

The point is simply that, looking at the Basic Settings in IIS at the application level, see an image sample below,

虚拟路径(在上述情况下为/flask )从IIS传递到FastCGI处理程序,因此必须在Flask应用程序中对其进行管理,例如您可以将路线定义为:

the virtual path (/flask in the above case) is passed from IIS to the FastCGI handler, hence it must be managed in the Flask app, e.g. you can define the route as:

@app.route("/flask")
def hello():

,它将正确显示在您的浏览器中(请注意,虽然最终的/会代表不同的网址,所以这样的错字也可能会导致404错误).

and it will be correctly shown in your browser (notice that a final / would represent a different url though, so such a typo could also cause a 404 error).

困惑是,您绝对可以在应用程序级别(不仅是在IIS中的网站级别)运行Flask FastCGI.

The conculsion is that you absolutely can run the Flask FastCGI also at application level and not only at website level in IIS.

您还需要在静态文件夹之前添加虚拟路径:

You also need to add the virtual path before the static file folder:

app = Flask(__name__, static_url_path='/flask/static')

最后一个建议:如果您有类似的配置

Finally a suggestion: if you have a configuration like

app.config.from_object(Config())
if environ.get('FLASK_FREIGHT_SETTINGS') is not None:
 app.config.from_envvar('FLASK_FREIGHT_SETTINGS')

然后 iisreset/restart 使新的环境变量对IIS可见,或者-甚至更好-

then iisreset /restart to make a new env var visible to IIS or - even better -

from dotenv import load_dotenv
load_dotenv()

如上所述,使用 python-dotenv模块文件,但我还要在服务器级别启用IIS的 fastcgi设置中的监视文件更改的标志,以便您的烧瓶当您更新app_settings.cfg(我在其中定义版本号参数)时,将重新加载应用程序的python句柄.

use the python-dotenv module as above to read the environmental variables from an .env file, but I'd also enable the flag to monitor changes to file in fastcgi settings of IIS at server level, so that your flask application's python handle is reloaded when you update your app_settings.cfg (where I define a version number parameter).

这篇关于通过IIS在应用程序级别而不是网站级别通过FastCGI运行Flask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆