当nginx代理302重定向时如何发送静态文件

时间:2017-09-24 16:57:53

标签: nginx http-status-code-302

我有服务器实现302重定向服务器的一些内容。

例如,当用户请求http://a.com/images/a.jpg时,我的服务器会向server B: http://b.com/images/a.jpg发出请求,但server B可能会返回302.所以我想要的是server B何时返回302,我的服务器将向用户发送a.logo

server {
    listen 80;
    server_name a.com;
     location / {
      proxy_pass http://www.girls-av.com;
       proxy_set_header X-Real-IP $remote_addr;
       proxy_cache nginx-cache;
       proxy_cache_valid 200 302 304 1440m;
       proxy_intercept_errors on;
      error_page 301 302 = @handler;
   }
    location @handler {
       proxy_pass /home/git/a/images/logo.png;   
   }
}

// but I got an error "invalid URL prefix"

然后我尝试

location @handler {
 proxy_pass http://a.com/images/logo.png;   
}

// but I got this error ""proxy_pass" cannot have URI part in location given by regular expression, or inside named location"

我也尝试

location @handler {
  alias /home/git/a/images/logo.png;   
}
// but I got another error the "alias" directive cannot be used inside the named location;

我做了什么sholud,任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

你能做的是这样的事情:

server {             
    listen 80;
    server_name a.com; 
    # ...

    error_page 301 302 = @handler;          
    location @handler {                   
       root /tmp/logo;                    
       try_files /logo.png /dev/null =404;
       error_log /tmp/nginx-error.log debug;                                         
    }                
} 

在这种情况下,文件logo.png位于/tmp/logo/logo.png

仅供测试,请注意:

error_log /tmp/nginx-error.log debug;