【发布时间】:2019-01-18 23:15:23
【问题描述】:
我已经安装了 nginx、gunicorn 和 Django,在 docker 容器上运行。 Nginx 用作反向代理。
一切正常,但对于 POST 方法,我收到错误消息 Forbidden (CSRF cookie not set.)。相同的配置在本地运行良好,但在生产环境(aws 上的 ec2)上,这不起作用。
这里是nginx配置:
upstream web {
ip_hash;
server web:8000;
}
server {
location / {
alias /src/frontend/dist/;
try_files $uri $uri/ /index.html;
}
location /static {
alias /src/static/;
try_files $uri =404;
}
# works locally, but doesn't work in prodcution on ec2
location /api/ {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://web; # pass to gunicorn
# what to serve if upstream is not available or crashes
# error_page 500 502 503 504 405 /error.html;
}
location @rewrites {
rewrite ^(.+)$ /index.html last;
}
listen 80;
server_name www.example.com;
}
我已经按照 SO 中的建议尝试了所有方法。我在 Django 设置的中间件中设置了CsrfViewMiddleware。 CSRF_COOKIE_SECURE & SESSION_COOKIE_SECURE 设置为 False。我已经没有想法和尝试的东西了。请给我一个解决方案的建议。
谢谢
版本:
Django==1.9.5
gunicorn==19.6.0
MySQL-python==1.2.5
nltk==3.3
pandas==0.23.3
django-pandas==0.5.1
NGINX=1.15.1
【问题讨论】:
-
作为一个提示:为了检查 nginx 是否丢弃或修改任何东西,我通常在服务器上执行“curl -I
”来比较响应头来自外部的请求。
标签: django docker nginx gunicorn