[英]Error 500 while running a Python Flask application on IIS. How to get the actual error
I try to get a Flask application running on an IIS on windows.我尝试在 Windows 上的 IIS 上运行 Flask 应用程序。
The reason is to get single-sign-on with windows authentication done using the IIS mechanisms.原因是使用 IIS 机制完成 Windows 身份验证的单点登录。
I got it running on my development machine.我让它在我的开发机器上运行。 But it doesn't work in the actual production environment.
但是在实际生产环境中是行不通的。 I get a error 500 for some reasons, but I don't see the actual Flask error message.
由于某些原因,我收到错误 500,但我没有看到实际的 Flask 错误消息。
I saw the python error message ones during the setup, before I set the rights to write to the log file.在设置写入日志文件的权限之前,我在设置过程中看到了 python 错误消息。 It told me about the missing rights.
它告诉我缺少的权利。 That should mean FastCGI is configured the right way, I guess.
我猜这应该意味着 FastCGI 以正确的方式配置。
Now after I set the write access, I get a IIS error 500 page that tells me that something went wrong with the FastCGI.现在,在我设置写访问权限后,我收到一个 IIS 错误 500 页面,告诉我 FastCGI 出现问题。 But I don't get any log entry, even if I set the rights to write them.
但是我没有得到任何日志条目,即使我设置了写入它们的权限。 No log files and no entries in the windows event logs.
Windows 事件日志中没有日志文件和条目。 Nothing.
没有。
Do you know a way to get the actual error message?您知道获取实际错误消息的方法吗?
[Update] After enabling the failed request trace, I get the following error: [更新] 启用失败的请求跟踪后,出现以下错误:
<RenderingInfo Culture="en-US">
<Opcode>FASTCGI_UNKNOWN_ERROR</Opcode>
<Keywords>
<Keyword>FastCGI</Keyword>
</Keywords>
<freb:Description Data="ErrorCode">The directory name is invalid.
(0x8007010b)</freb:Description>
</RenderingInfo>
The web.config looks like this: web.config 看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="PYTHONPATH" value="C:\inetpub\wwwroot\app_name" />
<!-- The handler here is specific to Bottle; see the next section. -->
<add key="WSGI_HANDLER" value="main.app" />
<add key="WSGI_LOG" value="C:\inetpub\wwwroot\app_name\LogFiles\wfastcgi.log" />
</appSettings>
<system.web>
<customErrors mode="Off" />
</system.web>
<system.webServer>
<handlers>
<clear />
<add name="Python FastCGI" path="*" verb="*" modules="FastCgiModule"
scriptProcessor=""c:\program files\python37\python.exe"|"c:\program files\python37\lib\site-packages\wfastcgi.py""
resourceType="Unspecified" requireAccess="Script" />
</handlers>
<security>
<authentication>
<anonymousAuthentication enabled="false" />
<windowsAuthentication enabled="true" />
</authentication>
</security>
</system.webServer>
</configuration>
If I would guess, the path to the "program files" folder with the blank inside would cause the error.如果我猜想,内部空白的“程序文件”文件夹的路径会导致错误。
To configure python flask app in iis you could follow the below steps:要在 iis 中配置 python flask 应用程序,您可以按照以下步骤操作:
https://www.python.org/downloads/ https://www.python.org/downloads/
note: if possible please use python version above 3.6.注意:如果可能,请使用 3.6 以上的 python 版本。
after installing python install the wfastcgi.run the command prompt as administrator and run below command:安装 python 后安装 wfastcgi.run 以管理员身份运行命令提示符并运行以下命令:
pip install wfastcgi
wfastcgi-enable
below is my flask example:下面是我的烧瓶示例:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello from FastCGI via IIS!"
if __name__ == "__main__":
app.run()
after creating an application to run it use below command:创建应用程序以运行它后,请使用以下命令:
python app.py
now enable the cgi feature of iis:现在启用 iis 的 cgi 功能:
executable path value:可执行路径值:
C:\\Python37-32\\python.exe|C:\\Python37-32\\Lib\\site-packages\\wfastcgi.py C:\\Python37-32\\python.exe|C:\\Python37-32\\Lib\\site-packages\\wfastcgi.py
All IIS sites have a web.config file in the root which holds all the settings.所有 IIS 站点的根目录中都有一个 web.config 文件,其中包含所有设置。 You can edit this manually with a text editor (it's just XML) or you can use the IIS GUI.
您可以使用文本编辑器(它只是 XML)手动编辑它,也可以使用 IIS GUI。
It sounds to me like the default <customErrors>
setting is On
or RemoteOnly
which means it shows actual errors on your local dev machine, but hides them with "friendly" (but unhelpful) error pages on production when you view the site remotely.在我看来,默认的
<customErrors>
设置是On
或RemoteOnly
,这意味着它会在您的本地开发机器上显示实际错误,但是当您远程查看站点时,它会在生产环境中使用“友好”(但无用)错误页面隐藏它们。
Look in your web.config for <customErrors>
inside the system.web
element (see below).在您的 web.config 中查找
system.web
元素中的<customErrors>
(见下文)。 Set it to mode="Off"
, this should hopefully mean you can see the actual error message.将其设置为
mode="Off"
,这应该意味着您可以看到实际的错误消息。
<configuration>
<system.web>
<customErrors mode="Off">
</customErrors>
</system.web>
</configuration>
I got the same error and have solved it by add access rights to the folder of python.我遇到了同样的错误,并通过向 python 文件夹添加访问权限来解决它。 In your case, the python folder path is
c:\\program files\\python37
, so try to add IIS_IUSRS
with read and execution access rights .在您的情况下,python 文件夹路径是
c:\\program files\\python37
,因此请尝试添加IIS_IUSRS
具有读取和执行访问权限。
The other option is that changing AppPool identity of the web application to LocalSystem
as IIS manager.另一种选择是将 Web 应用程序的AppPool标识更改为
LocalSystem
作为 IIS 管理器。
If your file permissions are correct, the problem might lie in your Application Pools settings.如果您的文件权限正确,则问题可能出在您的应用程序池设置中。
Application Pools
tabApplication Pools
选项卡Set Application Pool Defaults
...Set Application Pool Defaults
...Identity
under Process Model
to LocalSystemProcess Model
下的Identity
设置为LocalSystem
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.