Nginx 中根据 $upstream_status 的值,设置返回给浏览器的响应的头部为啥不生效?或者说怎么根据 upstream 服务器的响应码来设置 Nginx 对浏览器响应的头部?

2017-06-06 11:13:54 +08:00
 noobsheldon

server 配置

server {
    listen		80;
    server_name		insist.com www.insist.com;
    root		/var/www/insist.com;
    index		index index.html index.htm index.nginx-debian.html;

    location = /django {
	return 302 /django/;
    }

    location /django/ {
        proxy_pass http://127.0.0.1:8000/;
	proxy_redirect / /django/;
	set $expire_time "20s";
	if ($upstream_status = 301){ set $expire_time "40s"; }
	expires $expire_time;
    }

搜索了几个小时没找到完美解决方案,请各位帮忙看一看多谢了,多谢了!

5208 次点击
所在节点    NGINX
1 条回复
xuexb
2022-03-17 14:24:23 +08:00
我也遇到这个问题了,使用以下方向解决:

```nginx
# 30x 重定向不缓存
error_page 301 302 307 = @redirect_nocache;
location @redirect_nocache {
add_header Cache-Control 'no-store';
return 302 $upstream_http_location;
}

location @nodejs {
proxy_pass http://nodejs$request_uri;
proxy_intercept_errors on; # 重要
...


# 后台页页不加缓存
if ($uri !~* ^/admin/) {
add_header Cache-Control 'max-age=600, s-maxage=604800';
}
if ($uri ~* ^/admin(/|/.*)?$) {
add_header Cache-Control 'no-store';
}
}
```

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/366292

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX