我已经开发了Django项目,并将其部署到亚马逊的免费层EC2服务中。除了没有返回的错误消息之外,一切都很好。我正在生产模式中使用这个项目。
对上述图像控制台日志的解释
我想得到至少404响应,问题是我没有任何来自服务器的响应。当我在服务器上运行它时,我看到它正在将结果记录到服务器上。
问题:如何在发生问题时返回Django生成的响应。额外信息:这些错误消息和响应是在模板中的djangorestframework's
中生成的。
额外细节:
如果我遗漏了什么就告诉我。
发布于 2020-04-15 02:40:30
当大脑疲劳时,它会做一些有趣的事情。感谢@iklinac。他是对的,我最好能正确地使用django-cors-headers
。它已经安装并在heroku上工作,当我迁移到amazon时,我认为任何事情都与NGINX有关。
记笔记。
INSTALLED_APPS = [
...
'corsheaders',
...
]
MIDDLEWARE = [ # Or MIDDLEWARE_CLASSES on Django < 1.10
...
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
...
]
CORS_ORIGIN_WHITELIST = [
"https://example.com",
"https://sub.example.com",
"http://localhost:8080",
"http://127.0.0.1:9000"
]
然后,几乎没有其他东西可以随意调整和使用。
最后,我将我的nginx.conf更改为
upstream hello_django {
server web:8000;
}
server {
listen 80;
location / {
proxy_pass http://hello_django;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
location /staticfiles/ {
alias /home/app/web/staticfiles/;
}
location /mediafiles/ {
alias /home/app/web/mediafiles/;
}
}
(愉快的编码。)testdriven.io和django-cors-标头的学分
发布于 2020-04-14 05:07:43
您是代理传递请求,它们没有正确地获得add_header,您应该在添加标头之后进行代理传递
location / {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PATCH, PUT, DELETE';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Headers' 'Authorization,Accept,Origin,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PATCH, PUT, DELETE';
#
# Custom headers and headers various browsers *should* be OK with but aren't
#
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range, Authorization';
#
# Tell client that this pre-flight info is valid for 20 days
#
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}
proxy_pass http://app;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
相反,可以将django-cors-标头添加到应用程序中。
https://stackoverflow.com/questions/61207876
复制