我有一个使用 mod_wsgi 安装了 apache 和 django 的生产服务器。
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 中给出的解决方案,但更改行并不能修复错误。
我对这个错误有一些疑问:
- how does apache run django?
- does restarting apache2 service also restart django? (thus, reading again the settings.py)
- Is there any other django settings file rather than the one I'm editing?
- 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/