简体   繁体   English

Django Bad Request(400)使用Apache / NginX部署时出错

[英]Django Bad Request(400) Error in Deployment with Apache/NginX

I'm trying to lunch my app on VPS in Debug=False mode. 我正试图在Debug=False模式下在VPS上午餐。 Debug=True works fine but when I change it to false I got this error. Debug=True工作正常,但当我将其更改为false时,我收到此错误。 I'm using Apache for rendering python pages and Nginx to serve my static files. 我正在使用Apache来渲染python页面,使用Nginx来提供我的静态文件。 I tried using this [answer]: Debugging Apache/Django/WSGI Bad Request (400) Error but it's not working at least for me. 我尝试使用这个[回答]: 调试Apache / Django / WSGI错误请求(400)错误,但它至少对我不起作用。 And this is my wsgi config: 这是我的wsgi配置:

#wsgi.py
import os
import sys

os.environ['DJANGO_SETTINGS_MODULE'] = 'example.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
path = '/var/www/example'
if path not in sys.path:
    sys.path.append(path)

And also I've added below code to my settings file: 我还在下面的代码中添加了以下代码:

ALLOWED_HOSTS = [
    '.example.com', # Allow domain and subdomains
    '.example.com.', # Also allow FQDN and subdomains
]

To discover your problem, first in settings.py set ALLOWED_HOSTS temporarily to: 要发现您的问题,首先在settings.py中将ALLOWED_HOSTS暂时settings.py为:

ALLOWED_HOSTS = '*'

And then in somewhere in your view , try to print out and see output of this command: 然后在view某个位置,尝试打印并查看此命令的输出:

print(request.META['HTTP_HOST']) # or print(request.get_host())

Then according to output, set that (just domain of it as an list) to your ALLOWED_HOSTS . 然后根据输出,将它(只是它的域作为列表)设置为ALLOWED_HOSTS

Notes: 笔记:

  • Use ALLOWED_HOSTS = '*' may have security issue for you, read about that here . 使用ALLOWED_HOSTS = '*'可能有安全问题,请在此处阅读。

  • After every change you need to restart your service(apache/nginx). 每次更改后,您都需要重新启动服务(apache / nginx)。

I've had the same problem with my cyrillic domain. 我的西里尔字母域也遇到了同样的问题。

I've added punicode representation of my domain to ALLOWED_HOSTS, and that solved the problem. 我已经将我的域的punicode表示添加到ALLOWED_HOSTS,这解决了问题。

Maybe this should be sent as a bug to Django, but I didn't dig much into that (it could be an issue of my hosting configuration), just ab (and of course browser). 也许这应该作为一个bug发送到Django,但我没有深入研究(这可能是我的托管配置的问题),只是ab(当然还有浏览器)。 The problem was also intermittent for me (usually worse than what is shown below with ab). 这个问题对我来说也是断断续续的(通常比下面用ab显示的更糟)。

Without punicode 没有punicode

Complete requests: 20 Failed requests: 3 完成请求:20个请求失败:3

while with punicode 与punicode

Complete requests: 20 Failed requests: 0 完成请求:20个请求失败:0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM