Running Flask via FastCGI in IIS at application level instead of website leve

此生再无相见时 提交于 2021-02-11 12:36:14

问题


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

Well, here it is my trivial sample00.py

from flask import Flask

app = Flask(__name__)

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

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>

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.

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?


回答1:


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

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():

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).

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')

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

from dotenv import load_dotenv
load_dotenv()

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).



来源:https://stackoverflow.com/questions/64334383/running-flask-via-fastcgi-in-iis-at-application-level-instead-of-website-leve

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!