gpt4 book ai didi

Varnish cookies 问题

转载 作者:行者123 更新时间:2023-12-03 17:47:16 24 4
gpt4 key购买 nike

首先,抱歉我的英语不好,这不是我的自然语言。

我尝试使用Cookie配置 Varnish ,以供管理用户后端使用,并且我在登录和进行其他检查时遇到一些问题。

我的recv,fetch和hash的配置:

backend default {
.host = "127.0.0.1";
.port = "8080";
}

sub vcl_recv {

remove req.http.X-Forwarded-For;
set req.http.X-Forwarded-For = client.ip;

if (req.request == "POST"){
return (pass);
}

# Grace mode
if (! req.backend.healthy) {
set req.grace = 30m;
} else {
set req.grace = 15s;
}

if(req.url ~ "^localhost$"){
set req.http.host = "www.micasa.com";
}

# Acces to system URL's is protected
if ((req.url ~ "^/server_status") || (req.url ~ "^/discover/varnish_server")) {
error 403 "Go away, please";
}

# Delete all cookies except from user

if ( !(req.url ~ "^/logout") &&
!(req.url ~ "^/profile") &&
!(req.url ~ "^/playlists") &&
!(req.url ~ "^/users") &&
!(req.url ~ "^/signup") &&
!(req.url ~ "^/comments") &&
!(req.url ~ "^/login") &&
!(req.url ~ "^/remind"))
{
unset req.http.cookie;
}

sub vcl_fetch {

# Grace mode
# https://www.varnish-cache.org/docs/trunk/tutorial/handling_misbehaving_servers.html#grace-mode
set beresp.grace = 30m;

# Saint mode
# https://www.varnish-cache.org/docs/trunk/tutorial/handling_misbehaving_servers.html#saint-mode
if (beresp.status == 500) {
set beresp.saintmode = 10s;
return (restart);
}
if ( !(req.url ~ "^/login") && (req.request == "GET")){
unset beresp.http.set-cookie; # To avoid caching of cookies
}

# Process ESIs if X-RUN-ESI is set. This will be stripped before being sent down to client.
if ( beresp.http.X-RUN-ESI ) {
set beresp.do_esi = true;
remove beresp.http.X-RUN-ESI;
}

# cache 404s and 301s for 5 minute
if (beresp.status == 404 || beresp.status == 301 || beresp.status == 500) {
set beresp.ttl = 15m;
return (deliver);
}

# cache images and static assets during 15m
if ( req.url ~ "\.(png|gif|jpg|css|js|ico)" ) {
set beresp.ttl = 15m;
return (deliver);
}


# If X-VARNISH-TTL is set, use this header's value as the TTL for the varnish cache.
# Expires, cache-control, etc. will be passed directly through to the client
# Cribbed from http://www.lovelysystems.com/configuring-varnish-to-use-custom-http-headers/
if (beresp.http.X-VARNISH-TTL) {
C{
char *ttl;
/* first char in third param is length of header plus colon in octal */
ttl = VRT_GetHdr(sp, HDR_BERESP, "\016X-VARNISH-TTL:");
VRT_l_beresp_ttl(sp, atoi(ttl));
}C
remove beresp.http.X-VARNISH-TTL;
return (deliver);
}
sub vcl_deliver {
unset resp.http.x-url; # Optional
if ( req.url ~ "\.(png|gif|jpg|css|js|ico|woff)" ) {
set resp.http.expires = "3600";
}

#mikel
#remove resp.http.X-Powered-By;
remove resp.http.Server;
#remove resp.http.X-Varnish;
#remove resp.http.Via;
#remove resp.http.Age;

}

sub vcl_hash {
if (req.http.Cookie ~ "_micasa_session") {
hash_data(req.url);
hash_data(req.http.Cookie);
return (hash);
}
}

当我尝试与用户登录时可以,但是如果在此之后刷新同一页面,则会丢失Cookie并立即注销,也许问题出在sub vcl_recv中?

感谢您的帮助。

最佳答案

您取消设置除已定义页面之外的所有 cookie。您的站点登录信息几乎肯定保存在 cookie( session cookie?)中。简单的方法是通过检查是否设置了一些标识登录用户的 cookie 来禁用登录用户的缓存。最好的方法是使用 ESI,以便缓存所有用户都相同的部分。

关于 Varnish cookies 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15843122/

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