Apache 抛出错误 500:从 localhost/internal 网络获取时出现内部服务器错误

Apache throws ERROR 500: Internal Server Error when GET from localhost/internal network

我有一个使用 mod_wsgi[=54= 安装了 apachedjango 的生产服务器].

django 应用程序有一个 REST API,它在发送 GET 请求时提供一些信息。

这在开发服务器上一直运行良好,我们 运行 django 在屏幕中使用 manage.py。现在我们用 apache 运行 django 创建了一个生产服务器但是这个 API returns Error 500 when 运行 wget 来自 localhost 或同一网络中的其他机器(使用 192.168.X.X IP).

这是 wget 的输出:

~$ wget localhost:80/someinfo
--2020-04-02 16:26:59--  http://localhost/someinfo
Resolving localhost (localhost)... 127.0.0.1
Connecting to localhost (localhost)|127.0.0.1|:80... connected.
HTTP request sent, awaiting response... 500 Internal Server Error
2020-04-02 16:26:59 ERROR 500: Internal Server Error.

看来连接成功了,估计不是apache的问题。错误来自 API 响应。

apache 中的错误 error.log 看起来像这样:

127.0.0.1 - - [02/Apr/2020:14:24:36 +0000] "GET /someinfo HTTP/1.1" 500 799 "-" "Wget/1.19.4 (linux-gnu)"

question: what is the number after 500? Sometimes is 799 and other times is 803.

但是如果请求是从外部(即从浏览器)使用服务器的 public IP 完成的,API 工作正常并且我看到了正确的信息。

我已经检查了 django 允许的主机,它正在接受本地主机和另一台机器的 192.168.X.X IP。最后我把django的settings.py这样留下了:

#ALLOWED_HOSTS = ['localhost', '127.0.0.1', '192.168.1.101']
ALLOWED_HOSTS = ['*']

Note: 192.168.1.101 is the machine that tries to make the GET request. The final goal of all this is to be able to make a GET request from a python script running in that machine (which already works if django runs via manage.py).

我的apache.conf:


    <VirtualHost *:80>

            ServerAdmin webmaster@localhost
            #DocumentRoot /var/www/html

            Alias /static /home/myuser/myproject/django/static_root
        <Directory /home/myuser/myproject/django/static_root>
            Require all granted
        </Directory>

        <Directory /home/myuser/myproject/django/myproject_django>
            <Files wsgi.py>
                Require all granted
            </Files>
        </Directory>

        WSGIDaemonProcess myproject python-home=/home/myuser/env python-path=/home/myuser/myproject/django
        WSGIProcessGroup myproject
        WSGIScriptAlias / /home/myuser/myproject/django/myproject_django/wsgi.py
    </VirtualHost>

I tried running django via manage.py and the wget from localhost works just fine. The problem only appears when django is ran by apache.

我也尝试了this post中给出的解决方案,但是更改行并没有修复错误。

我对这个错误有一些疑问:

  1. how does apache run django?
  2. does restarting apache2 service also restart django? (thus, reading again the settings.py)
  3. Is there any other django settings file rather than the one I'm editing?
  4. how can I see django logs? I don't have the console now so I can't see real time prints.

非常感谢任何帮助。

我终于自己解决了。

事实证明,wsgi 将来自本地主机或外部 IP 的请求作为不同的实例组处理。所以我所要做的就是把

WSGIApplicationGroup %{GLOBAL}

/etc/apache2/sites-available/000-default.conf