【问题标题】:Django behind uwsgi+nginx not able to set cookiesuwsgi+nginx 后面的 Django 无法设置 cookie
【发布时间】:2012-07-21 05:14:03
【问题描述】:

我正在为一个辅助项目膳食计划工具构建一个 PoC。我正在使用在 AWS 上运行的 django 和 nginx 和 uwsgi。该站点正在运行并加载页面,但没有将 CSRF cookie 传递给浏览器。

CSRF 和 Messages 中间件都已启用,django 调试输出列出了“CSRF_COOKIE”的值,并且我的浏览器启用了 cookie,所以我怀疑 Django 试图设置一个被 nginx 删除的 cookie或 uwsgi。

配置信息如下:

uwsgi.ini

[uwsgi]
chdir=/opt/django/mealplanner/src/mealplanner/
module=mealplanner.wsgi:application
master=True
autoload=True
pidfile=/opt/run/mealplanner.pid
vacuum=True
max-requests=5000
socket=/opt/run/mealplanner.sock
chmod-socket=True
harakiri=120
processes=1
home=/opt/django/mealplanner/src
daemonize=/opt/log/uwsgi/mealplanner.log

nginx.conf

user www-data;
worker_processes 1;
pid /opt/run/nginx.pid;

events {
        worker_connections 768;
        # multi_accept on;                                                                                 
}

http {

        ##                                                                                                 
        # Basic Settings                                                                                   
        ##                                                                                                 

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;                                                                               

        # server_names_hash_bucket_size 64;                                                                
        # server_name_in_redirect off;                                                                     

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

        ##                                                                                                 
        # Logging Settings                                                                                 
        ##                                                                                                 

        access_log /opt/log/nginx/access.log;
        error_log /opt/log/nginx/error.log;

        ##                                                                                                 
        # Gzip Settings                                                                                    
        ##                                                                                                 

        gzip on;
        gzip_disable "msie6";

        # gzip_vary on;                                                                                    
        gzip_proxied any;
        gzip_comp_level 2;
        # gzip_buffers 16 8k;                                                                              
        # gzip_http_version 1.1;                                                                           
        gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml \
application/xml+rss text/javascript;

        server {
               listen 80;
               # I've also tried the dns name I access the site with as the server name.
               server_name ec2-xx-xx-xx-xx.us-west-2.compute.amazonaws.com xx.xx.xx.xx;
               client_max_body_size 50M;
               set $home /opt/django/mealplanner;
               root $home;
               location / {
                        include uwsgi_params;
                        uwsgi_pass unix://opt/run/mealplanner.sock;
                        root $home;
               }
        }

        ##                                                                                                 
        # Virtual Host Configs                                                                             
        ##                                                                                                 

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}

【问题讨论】:

  • 当您说您在 AWS 上运行它时,您是指单个 EC2 实例吗?
  • 是的,它使用的是 EC2 micro。我只是偶然发现我认为我做错了什么。

标签: python django nginx uwsgi


【解决方案1】:

看起来问题可能出在 nginx.conf 中的 server_name 上。它突然开始工作,我唯一改变的是使 server_name 与站点的域名匹配。另一方面,将它改回来似乎并没有重新引入问题,所以要么我没有正确重新启动 nginx,要么这根本不是问题。

该网站现在正在运行,但这是我对原因和解决方案没有令人满意的理解的情况之一,所以如果有人可以证实这个假设或指出其他内容,我会欢迎输入。

【讨论】:

    猜你喜欢
    • 2014-02-01
    • 2011-03-12
    • 1970-01-01
    • 2019-03-03
    • 2018-03-19
    • 1970-01-01
    • 2010-12-12
    • 2014-05-31
    • 2012-01-06
    相关资源
    最近更新 更多