gpt4 book ai didi

django.request : Forbidden (Referer checking failed - no Referer. )

转载 作者:行者123 更新时间:2023-12-01 04:57:24 27 4
gpt4 key购买 nike

我正在使用 AWS 和 Django Rest Framework 开发 Web 应用程序。(Django:v1.8,DRF:v3)
我一直在为 POST 多部分表单请求获取 django.request: Forbidden (Referer checking failed - no Referer.)

我在我的 ec2(在 autoscailing 组中)和 Gunicorn 上使用 AWS ELB(弹性负载均衡器)、NGINX。

AWS ELB 监听器设置如下(仅限 HTTPS):

elb https only listener setting

NGINX 设置如下:

user  nginx;
worker_processes auto;

error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

events {
worker_connections 1024;
}

http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main;

sendfile on;
tcp_nopush on;
tcp_nodelay on;

keepalive_timeout 65;
types_hash_max_size 2048;

include /etc/nginx/conf.d/*.conf;

index index.html index.htm;

upstream my_server {
server localhost:8000;
}

server {

listen 80;
server_name <server name>;
access_log /etc/nginx/log/local-wc.access.log;
error_log /etc/nginx/log/local-wc.error.log;
root /usr/share/nginx/html;

location /api/v1 {
proxy_pass http://my_server/api/v1;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Protocol $scheme;
}
}
}
<server name> 是指向 elb DNS 名称的 CNAME。

换句话说, <server name> => xxxx-123456789.us-west-2.elb.amazonaws.com(A 记录)。
每个 API 调用都由 https://<server name>/api/v1/* 进行

最后 Gunicorn 正在运行: gunicorn my_django_app.wsgi:application -w 1 -b 127.0.0.1:8000 -t 300 --max-requests=100
和 Django 设置是:
ALLOWED_HOSTS = ['*']
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.security.SecurityMiddleware',
)

查看功能如下(CSRF豁免):
class UserViewSet(CsrfExemptMixin, mixins.CreateModelMixin,
mixins.ListModelMixin,
mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
viewsets.GenericViewSet):

# already tried @csrf_exempt
def create(self, request, *args, **kwargs):
self.parser_classes = (FormParser, MultiPartParser, )
.........

问题又来了:

当我发送
curl -i -k -X POST -H "Accept: application/json" \
-F "email=myemail@email.com" \
-F "profile_img=@profile.jpg" \
https://<server name>/api/v1/users/

在我的 Django 日志中:
[WARNING] django.request: Forbidden (Referer checking failed - no Referer.): /api/v1/users/

它适用于 HTTP 上的 POST 或 HTTPS 上的 GET 方法。

不知道是ELB配置错了还是Nginx配置错了referer...
如果有人帮助我解决这个问题,我将不胜感激..

最佳答案

我认为 DRF 忽略 csrf_exempt 装饰器,我不确定 CsrfExemptMixin 在哪里定义。您可以做的最简单的事情是添加 Referrer: yourhost到您的 curl 标题。

关于django.request : Forbidden (Referer checking failed - no Referer. ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36080089/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com