【问题标题】:how to send a static file when nginx proxy 302 redirectnginx代理302重定向时如何发送静态文件
【发布时间】:2018-03-05 16:14:39
【问题描述】:

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

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

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;

我该怎么办,有人可以帮忙吗?

【问题讨论】:

    标签: nginx http-status-code-302


    【解决方案1】:

    你可以这样做:

    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; 
    

    【讨论】:

    • 它不起作用,当用户请求a.com/a/b/c.jpg时,它会尝试返回/tmp/logo/a/b/c.jpg而不是tmp/logo/logo.png
    • 你是否设置了try_files $uri /logo.png =404; 注意到/logo.png 我刚刚测试并工作
    • 我只是尝试使用try_files /tmp/log/logo.png,现在可以了。
    • nice :-) 你用的是什么版本的 Nginx?
    • 我的版本是1.10.2
    猜你喜欢
    • 2017-09-09
    • 1970-01-01
    • 2012-11-16
    • 1970-01-01
    • 2016-08-19
    • 2019-04-17
    • 2023-04-09
    • 1970-01-01
    • 2017-07-10
    相关资源
    最近更新 更多