django - Apache 抛出错误 500 : Internal Server Error when GET from localhost/internal network

标签 django apache rest mod-wsgi internal-server-error

我有一个使用 mod_wsgi 安装了 apachedjango 的生产服务器。

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

如果我们在屏幕中使用manage.py运行django,这在开发服务器上总是工作得很好。现在我们创建了一个运行 django 的 apache 生产服务器,但是当从 localhost 运行 wget 时,此 API 返回 错误 500 或同一网络中的其他机器(使用 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 允许的主机,它正在接受 localhost 和另一台机器的 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

关于django - Apache 抛出错误 500 : Internal Server Error when GET from localhost/internal network,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60997112/

相关文章:

django - 覆盖查询集的更新方法 - Django

java - 使用简单的 spring-data-rest 项目获取 404 错误

python - URLconf 不适用于类似的 URL

Apache httpd.conf 用于将 ip 重定向到主机名 - SSL

apache - .htaccess 在没有 www 的情况下重写并重定向到子目录

php - htaccess 中的 Hash(#) 标签重定向

rest - 如何在 Springfox 的 Swagger 2 中为 JSONObject 请求正文自定义示例值?

wordpress - 禁用 WP REST API 的默认路由

python - 从帖子中检索元数据,然后按 django 中的元数据对帖子进行排序

django - 如何在 Django 中指定 login_required 重定向 url?