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 · Jun 6, 2017 · 6156 views
    This topic created in 3298 days ago, the information mentioned may be changed or developed.

    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 replies    2022-03-17 14:24:23 +08:00
    xuexb
        1
    xuexb  
       Mar 17, 2022
    我也遇到这个问题了,使用以下方向解决:

    ```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';
    }
    }
    ```
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3134 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 50ms · UTC 12:16 · PVG 20:16 · LAX 05:16 · JFK 08:16
    ♥ Do have faith in what you're doing.