Nginx reverse proxy after redirection

I would like Nginx to return the actual files instead of a response with the Location: redirect header. I use Nginx as a reverse proxy cache: cdn.mydomain.com receives a request and contacts api.mydomain.com/files/ to retrieve image files from there, but api.mydomain.com/files/ returns an empty response with Location: redirect header on AWS S3 instead of the file itself.

Thus, Nginx caches an empty redirect response. How can I get Nginx to retrieve and cache the actual file with S3.

 user www-data; worker_processes 4; pid /var/run/nginx.pid; http { server_names_hash_bucket_size 64; proxy_redirect off; proxy_cache_path /var/cache/nginx levels=2:2:2 keys_zone=my-cache:8m max_size=4G inactive=600m; proxy_temp_path /var/cache/tmp; server { listen 80; server_name cdn.mydomain.com; server_tokens off; location / { proxy_pass http://api.mydomain.com/files/; proxy_cache my-cache; proxy_cache_valid 200 302 30d; expires 30d; add_header Pragma public; add_header Cache-Control "public"; } } 
+5
source share
2 answers

I had the same need and could not find any working solution, so I combined what I learned from the Nginx mailing lists and Nginx Documentation:

 proxy_cache_path /tmp/docker/nginx/cache levels=1:2 keys_zone=DOCKERHUB:10m inactive=24h max_size=8g; server { ... location /v2/ { proxy_pass https://registry-1.docker.io; proxy_cache DOCKERHUB; #proxy_cache_valid 200 1d; #proxy_cache_use_stale error timeout invalid_header updating # http_500 http_502 http_503 http_504; #proxy_cache_lock on; proxy_intercept_errors on; error_page 301 302 307 = @handle_redirect; } location @handle_redirect { set $saved_redirect_location '$upstream_http_location'; proxy_pass $saved_redirect_location; proxy_cache DOCKERHUB; #proxy_cache_valid 200 1d; #proxy_cache_use_stale error timeout invalid_header updating # http_500 http_502 http_503 http_504; #proxy_cache_key $scheme$proxy_host$uri; #proxy_cache_lock on; } } 

PS I commented on the proxy_cache parameters related to my use case, but you can still find them useful.

0
source

All traffic for downloading files will go through nginx.

I would recommend files.mydomain.com up a separate DNS entry, such as files.mydomain.com , and pointing it to the Amazon S3 bucket. This is to be served from your domain, but all traffic went through Amazon.

DNS setup documentation

-1
source

Source: https://habr.com/ru/post/1200705/


All Articles