V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
noobsheldon
V2EX  ›  NGINX

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

  •  
  •   noobsheldon · 2017-06-06 11:13:54 +08:00 · 5194 次点击
    这是一个创建于 2508 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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;
        }
    

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

    1 条回复    2022-03-17 14:24:23 +08:00
    xuexb
        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';
    }
    }
    ```
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5670 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 02:06 · PVG 10:06 · LAX 19:06 · JFK 22:06
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.