Flask 和 Apache - 日志中没有错误,总是 404

Flask and Apache - No errors in log, Always 404

我正在尝试使用 mod_wsgi 让 Flask 与 Apache 一起工作,我已经为此苦苦思索了 7 个多小时。是时候寻求帮助了。

我运行正在使用 httpd docker 映像安装 apache。

我首先按照说明 here 使 wsgi 'hello world' 应用程序正常工作,而且效果很好。如果我打开浏览器并导航到 172.17.0.2,我会看到位于 /usr/local/apache2/htdocs/

的页面

在我的 httpd.conf 中,我有以下内容来显示这些页面:

DocumentRoot "/usr/local/apache2/htdocs"
<Directory "/usr/local/apache2/htdocs">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiView
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.4/mod/core.html#options
    # for more information.
    #
    Options Indexes FollowSymLinks

    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   AllowOverride FileInfo AuthConfig Limit
    #
    AllowOverride None

    #
    # Controls who can get stuff from this server.
    #
    Require all granted
</Directory>

这都是 docker 图片的默认值。

然后,在 /usr/local/apache2/wsgi-scripts/myapp.wsgi 我在 mod_wsgi 说明页面上找到了 hello world 示例。在我的 httpd.conf 中,我有以下内容

WSGIScriptAlias /myapp /usr/local/apache2/wsgi-scripts/myapp.wsgi

<Directory /usr/local/apache2/wsgi-scripts>
  <IfVersion < 2.4>
      Order allow,deny
      Allow from all
  </IfVersion>
  <IfVersion >= 2.4>
      Require all granted
  </IfVersion>
</Directory>

当我使用浏览器导航到 172.17.0.2/myapp 时,一切都很好。我看到 'hello world' 并且 apache 打印了以下日志:

[Thu Apr 16 02:48:14.678027 2020] [wsgi:info] [pid 1275:tid 140646030583552] mod_wsgi (pid=1275): Create interpreter '172.17.0.2|/myapp'.
[Thu Apr 16 02:48:14.713667 2020] [wsgi:info] [pid 1275:tid 140646030583552] [client 172.17.0.1:38046] mod_wsgi (pid=1275, process='', application='172.17.0.2|/myapp'): Loading Python script file '/usr/local/apache2/wsgi-scripts/myapp.wsgi'.
172.17.0.1 - - [16/Apr/2020:02:48:14 +0000] "GET /myapp HTTP/1.1" 200 12

现在我试着让我的烧瓶应用程序工作。这就是问题所在。我的 wsgi 脚本紧挨着上面的 myapp.wsgi 脚本:

root@423828dd037e:/# ls -l /usr/local/apache2/wsgi-scripts/
total 8
-rw-r--r-- 1 root root 278 Apr 16 02:16 myapp.wsgi
-rw-r--r-- 1 root root  74 Apr 16 02:22 website_api.wsgi

脚本非常简单:

root@423828dd037e:/# cat /usr/local/apache2/wsgi-scripts/website_api.wsgi
from website_rest_api import app as application

我可以 运行 在 python 解释器中执行完全相同的命令,所以我知道导入工作正常。

我有

WSGIScriptAlias /api /usr/local/apache2/wsgi-scripts/website_api.wsgi

在我的 httpd.conf 文件中

当我尝试导航到 172.17.0.2/api 时,我遇到了一个完全沮丧的练习。 Apache 日志如下所示:

[Thu Apr 16 02:52:41.678999 2020] [wsgi:info] [pid 1395:tid 140174394627840] mod_wsgi (pid=1395): Create interpreter '172.17.0.2|/api'.
[Thu Apr 16 02:52:41.683555 2020] [wsgi:info] [pid 1477:tid 140174539728000] mod_wsgi (pid=1477): Python home /usr.
[Thu Apr 16 02:52:41.683649 2020] [wsgi:info] [pid 1477:tid 140174539728000] mod_wsgi (pid=1477): Initializing Python.
[Thu Apr 16 02:52:41.710027 2020] [wsgi:info] [pid 1477:tid 140174539728000] mod_wsgi (pid=1477): Attach interpreter ''.
[Thu Apr 16 02:52:41.714671 2020] [wsgi:info] [pid 1395:tid 140174394627840] [client 172.17.0.1:40228] mod_wsgi (pid=1395, process='', application='172.17.0.2|/api'): Loading Python script file '
/usr/local/apache2/wsgi-scripts/website_api.wsgi'.
[Thu Apr 16 02:52:41.736762 2020] [wsgi:info] [pid 1477:tid 140174539728000] mod_wsgi (pid=1477): Imported 'mod_wsgi'.
172.17.0.1 - - [16/Apr/2020:02:52:41 +0000] "GET /api HTTP/1.1" 404 232

从这些日志中,我知道 apache 知道这是一件 mod_wsgi 事情。它似乎从正确的位置加载了脚本,但随后我得到了 404。我的所有烧瓶 URL 都得到了这个。我应该可以点击 /api、/api/login、/api/logout、/api/projects、/api/tests,但没有。一切都是 404,我看不到其他有助于调试的错误。

我知道 from website_rest_api import app as application 导入了正确的东西,因为我可以自己进入 python shell 并查看 URL 地图:

>>> from website_rest_api import app as application
>>> application.url_map
Map([<Rule '/api/projects' (OPTIONS, HEAD, GET) -> projects>,
 <Rule '/api/logout' (POST, OPTIONS) -> logout>,
 <Rule '/api/login' (POST, OPTIONS) -> login>,
 <Rule '/api/tests' (OPTIONS, HEAD, GET) -> tests>,
 <Rule '/api' (OPTIONS, HEAD, GET) -> hello>,
 <Rule '/static/<filename>' (OPTIONS, HEAD, GET) -> static>])

我也可以打印这些,以便它们显示在 apache 日志中:

[Thu Apr 16 04:38:57.162667 2020] [wsgi:info] [pid 2953:tid 139925743146112] mod_wsgi (pid=2953): Imported 'mod_wsgi'.
[Thu Apr 16 04:38:57.396592 2020] [wsgi:error] [pid 2864:tid 139925713024768] Map([<Rule '/api/projects' (GET, OPTIONS, HEAD) -> projects>,
[Thu Apr 16 04:38:57.396634 2020] [wsgi:error] [pid 2864:tid 139925713024768]  <Rule '/api/logout' (OPTIONS, POST) -> logout>,
[Thu Apr 16 04:38:57.396642 2020] [wsgi:error] [pid 2864:tid 139925713024768]  <Rule '/api/login' (OPTIONS, POST) -> login>,
[Thu Apr 16 04:38:57.396649 2020] [wsgi:error] [pid 2864:tid 139925713024768]  <Rule '/api/tests' (GET, OPTIONS, HEAD) -> tests>,
[Thu Apr 16 04:38:57.396657 2020] [wsgi:error] [pid 2864:tid 139925713024768]  <Rule '/api' (GET, OPTIONS, HEAD) -> hello>,
[Thu Apr 16 04:38:57.396666 2020] [wsgi:error] [pid 2864:tid 139925713024768]  <Rule '/static/<filename>' (GET, OPTIONS, HEAD) -> static>])

我也可以故意搞乱 website_api.wsgi 中的导入,我看到 Apache 吐出一条错误消息,所以我有理由相信如果导入错误它会告诉我:

[Thu Apr 16 04:34:27.634313 2020] [wsgi:info] [pid 2743:tid 139796746721024] [remote 172.17.0.1:60664] mod_wsgi (pid=2743, process='website_api', application=''): Loading Python script file '/usr
/local/apache2/wsgi-scripts/website_api.wsgi'.
[Thu Apr 16 04:34:27.634956 2020] [wsgi:error] [pid 2743:tid 139796746721024] [remote 172.17.0.1:60664] mod_wsgi (pid=2743): Failed to exec Python script file '/usr/local/apache2/wsgi-scripts/web
site_api.wsgi'.
[Thu Apr 16 04:34:27.635044 2020] [wsgi:error] [pid 2743:tid 139796746721024] [remote 172.17.0.1:60664] mod_wsgi (pid=2743): Exception occurred processing WSGI script '/usr/local/apache2/wsgi-scr
ipts/website_api.wsgi'.
[Thu Apr 16 04:34:27.644848 2020] [wsgi:error] [pid 2743:tid 139796746721024] [remote 172.17.0.1:60664] Traceback (most recent call last):
[Thu Apr 16 04:34:27.644905 2020] [wsgi:error] [pid 2743:tid 139796746721024] [remote 172.17.0.1:60664]   File "/usr/local/apache2/wsgi-scripts/website_api.wsgi", line 1, in <module>
[Thu Apr 16 04:34:27.644917 2020] [wsgi:error] [pid 2743:tid 139796746721024] [remote 172.17.0.1:60664]     import zarzlefrz
[Thu Apr 16 04:34:27.644943 2020] [wsgi:error] [pid 2743:tid 139796746721024] [remote 172.17.0.1:60664] ModuleNotFoundError: No module named 'zarzlefrz'
172.17.0.1 - - [16/Apr/2020:04:34:27 +0000] "GET /api HTTP/1.1" 500 528

甚至调试级别的日志似乎也没有告诉我任何关于正在发生的事情:

[Thu Apr 16 03:53:52.766115 2020] [core:debug] [pid 2075:tid 140389386400896] log.c(1571): AH02639: Using SO_REUSEPORT: yes (1)
[Thu Apr 16 03:53:52.766422 2020] [wsgi:info] [pid 2079:tid 140389386400896] mod_wsgi (pid=2079): Python home /usr.
[Thu Apr 16 03:53:52.766546 2020] [wsgi:info] [pid 2079:tid 140389386400896] mod_wsgi (pid=2079): Initializing Python.
[Thu Apr 16 03:53:52.769139 2020] [wsgi:info] [pid 2078:tid 140389386400896] mod_wsgi (pid=2078): Python home /usr.
[Thu Apr 16 03:53:52.769300 2020] [wsgi:info] [pid 2078:tid 140389386400896] mod_wsgi (pid=2078): Initializing Python.
[Thu Apr 16 03:53:52.805797 2020] [wsgi:info] [pid 2077:tid 140389386400896] mod_wsgi (pid=2077): Attach interpreter ''.
[Thu Apr 16 03:53:52.806676 2020] [wsgi:info] [pid 2079:tid 140389386400896] mod_wsgi (pid=2079): Attach interpreter ''.
[Thu Apr 16 03:53:52.813314 2020] [wsgi:info] [pid 2078:tid 140389386400896] mod_wsgi (pid=2078): Attach interpreter ''.
[Thu Apr 16 03:53:52.845456 2020] [wsgi:info] [pid 2079:tid 140389386400896] mod_wsgi (pid=2079): Imported 'mod_wsgi'.
[Thu Apr 16 03:53:52.847250 2020] [mpm_event:debug] [pid 2079:tid 140389373064960] event.c(2314): AH02471: start_threads: Using epoll (wakeable)
[Thu Apr 16 03:53:52.859225 2020] [wsgi:info] [pid 2077:tid 140389386400896] mod_wsgi (pid=2077): Imported 'mod_wsgi'.
[Thu Apr 16 03:53:52.860438 2020] [mpm_event:debug] [pid 2077:tid 140389373064960] event.c(2314): AH02471: start_threads: Using epoll (wakeable)
[Thu Apr 16 03:53:52.862762 2020] [wsgi:info] [pid 2078:tid 140389386400896] mod_wsgi (pid=2078): Imported 'mod_wsgi'.
[Thu Apr 16 03:53:52.863600 2020] [mpm_event:debug] [pid 2078:tid 140389373064960] event.c(2314): AH02471: start_threads: Using epoll (wakeable)
[Thu Apr 16 03:54:03.057717 2020] [authz_core:debug] [pid 2079:tid 140389235279616] mod_authz_core.c(818): [client 172.17.0.1:41350] AH01626: authorization result of Require all granted: granted
[Thu Apr 16 03:54:03.057771 2020] [authz_core:debug] [pid 2079:tid 140389235279616] mod_authz_core.c(818): [client 172.17.0.1:41350] AH01626: authorization result of <RequireAny>: granted
[Thu Apr 16 03:54:03.086984 2020] [wsgi:info] [pid 2079:tid 140389235279616] mod_wsgi (pid=2079): Create interpreter '172.17.0.2|/api'.
[Thu Apr 16 03:54:03.128584 2020] [wsgi:info] [pid 2079:tid 140389235279616] [client 172.17.0.1:41350] mod_wsgi (pid=2079, process='', application='172.17.0.2|/api'): Loading Python script file '
/usr/local/apache2/wsgi-scripts/website_api.wsgi'.
172.17.0.1 - - [16/Apr/2020:03:54:03 +0000] "GET /api HTTP/1.1" 404 232

我的问题:

我到底应该如何进一步调试呢?有没有什么地方在喷出我看不到的秘密烧瓶错误?

好吧,我想通了 - 秘密调试技巧是离开一个小时然后回来。我希望我在六个小时前尝试过。

flask 应用有这样的路由:

@app.route('/api', methods=['GET'])
def hello():                       
    return "Hello World"           

Apache 配置如下所示:

WSGIScriptAlias /api /usr/local/apache2/wsgi-scripts/website_api.wsgi

这意味着要查看烧瓶打印 "Hello World",我需要转到 172.17.0.2/api/api 而不是像我之前尝试的那样 172.17.0.2/api

当我使用 flask 的内置服务器与生产进行调试时,我需要做一些额外的工作来使路由匹配,但这是一个不同的堆栈溢出问题