0
location / {
    fastcgi_intercept_errors on;
    proxy_pass https://drive.google.com/uc?$query_string;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host drive.google.com;
    proxy_set_header Referer https://drive.google.com;
    error_page 301 302 307 = @handle_redirects;

}
location @handle_redirects {
    set $redirect_location '$upstream_http_location';
    proxy_pass $redirect_location;
    proxy_cache ngx-cache;
    proxy_cache_key $query_string;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

curl -I -A "ua" https://server/dl?id=trhtyjkfktkghjsutrtujyuk return 302 directly rather than go to handle_redirects section,this config works on my another vps with server Tengine,but it doens't work on my another vps with original edition nginx,have no idea where gone wrong.any help would be appreciated.thanks!!

Harris Tailor
  • 281
  • 3
  • 8

2 Answers2

1

I believe you need to add:

  proxy_intercept_errors on;

Stolen from Intercepting backend 301/302 redirects (proxy_pass) and rewriting to another location block possible?

Patrick Tescher
  • 3,387
  • 1
  • 18
  • 31
0

I had a similar issue while trying to proxy the images from https://picsum.photos website. They return a 302 redirect to a HMAC-signed URL when requesting a particular picture.

The working solution for me is:

server {
    listen 80;
    server_name dog.localhost.direct;
    location / {
        proxy_ssl_server_name on;
        proxy_intercept_errors on;
        error_page 301 302 307 = @handle_redirect;
        proxy_pass https://picsum.photos/id/237/536/354;
    }
    location @handle_redirect {
        set $saved_redirect_location '$upstream_http_location';
        resolver 8.8.8.8;
        proxy_pass $saved_redirect_location;
    }
}

Hope this will help somebody.

igops
  • 475
  • 3
  • 7