大网数据广告活动
服务器运维问答

nginx caching work wrong, nginx cache auth system, logout not work (on DJANGO), how to fix?

· 52CE 我爱测

nginx is caching everything, if I login to the system, then I can no longer exit it until the caching expires, since I'm Logout from the account, i need to know how to delete cookies and session! by default, Django itself removes…

问题原文

原帖链接 · InvictusManeoBart · CC BY-SA 3.0

nginx is caching everything, if I login to the system, then I can no longer exit it until the caching expires, since I'm Logout from the account, i need to know how to delete cookies and session!

by default, Django itself removes cookies and sessions when exiting, using the standard method to exit the account from the developers django, I use it, if you disable caching at nginx, then everything works fine!

nginx conf "/etc/nginx/nginx.conf"

##
# Logging Settings
##

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

##
# Gzip Settings
##

gzip on;
gzip_disable "msie6";

gzip_vary on;
gzip_proxied any;
    gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

proxy_connect_timeout 5;
proxy_send_timeout 10;
proxy_read_timeout 10;

proxy_buffering on;
proxy_buffer_size 16k;
proxy_buffers 24 16k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;

proxy_temp_path /tmp/nginx/proxy_temp;
add_header X-Cache-Status $upstream_cache_status;
    proxy_cache_path /tmp/nginx/cache levels=1:2 keys_zone=one:100m;
proxy_cache_path /tmp/nginx/cache2 levels=1:2 keys_zone=two:100m;
proxy_cache one;
proxy_cache_valid any 30d;
proxy_cache_key $scheme$proxy_host$request_uri$cookie_US;

my server conf

upstream theband {
  # fail_timeout=0 means we always retry an upstream even if it failed
  # to return a good HTTP response (in case the Unicorn master nukes a
  # ssingle worker for timing out).

  server unix:/webapps/theband/run/gunicorn.sock fail_timeout=0;
}

server {

    listen   80;
    server_name 207.154.232.99;
    expires 35d;
    client_max_body_size 4G;

    access_log /webapps/theband/logs/nginx-access.log;
    error_log /webapps/theband/logs/nginx-error.log;
    error_log /webapps/theband/logs/nginx-crit-error.log crit;
    error_log /webapps/theband/logs/nginx-debug.log debug; 
    location /static/ {
        alias   /webapps/theband/static/;
    }

    location /media/ {
        alias   /webapps/theband/media/;
    }
    location ~* ^(?!/media).*.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
        alias /tmp/nginx/trash/trash_media;
        expires 35d;
        add_header Pragma public;
        add_header Cache-Control "public, must-revalidate, proxy-revalidate";
        access_log off;
    }    
    location ~* ^(?!/static).*.(?:css|js|html)$ {
    root /tmp/nginx/trash/trash_static;
        expires 35d;
        add_header Pragma public;
        add_header Cache-Control "public, must-revalidate, proxy-revalidate";
        access_log off;
    }     

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_cache one;
        proxy_cache_min_uses 1;
        proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;

        # an HTTP header important enough to have its own Wikipedia entry:
        #   http://en.wikipedia.org/wiki/X-Forwarded-For
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        # enable this if and only if you use HTTPS, this helps Rack
        # set the proper protocol for doing redirects:
        # proxy_set_header X-Forwarded-Proto https;

        # pass the Host: header from the client right along so redirects
        # can be set properly within the Rack application
        proxy_set_header Host $http_host;

        # we don't want nginx trying to do something clever with
        # redirects, we set the Host: header above already.
        proxy_redirect off;

        # set "proxy_buffering off" *only* for Rainbows! when doing
        # Comet/long-poll stuff.  It's also safe to set if you're
        # using only serving fast clients with Unicorn + nginx.
        # Otherwise you _want_ nginx to buffer responses to slow
        # clients, really.
        #proxy_buffering off;

        # Try to serve static files from nginx, no point in making an
        # *application* server like Unicorn/Rainbows! serve static files.
        if (!-f $request_filename) {
            proxy_pass http://theband;
            break;
        }
    }
    error_page 404 /error_404.html;
    location = /error_404.html {
        root /webapps/theband/src/templates;
    }

    # Error pages
    error_page  500 502 503 504 /error_500.html;
    location = /error_500.html {
        root /webapps/theband/src/templates;
    }
}

已采纳解答

原帖链接 · InvictusManeoBart · CC BY-SA 3.0

I'm crying, I've spent a lot of time solving this problem, although I knew what the problem was, and how to solve it roughly, but I just add SIMPLE, SIMPLY 1 line of code .... CARL We had to put this proxy_pass http://theband;

kill me pls(...

well make like this and cache for specific url is gonna be disabled and acceses to page be work!

location /accounts/logout {
    proxy_no_cache 1;
    proxy_cache_bypass 1;
    add_header Last-Modified $date_gmt;
    add_header Cache-Control 'no-cache, must-revalidate, proxy-revalidate, max-age=0';
    if_modified_since off;
    expires -1;
    proxy_pass http://theband;
    etag off;

}

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-12-28)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。