Monday, August 1, 2016

Nginx caching reverse proxy with URL rewrite



I am serving large static files from a backend storage server with slow spinning disks. This server is hidden behind fast nginx reverse proxy with local cache on SSD. It works great and fast.



Now I want to change storage backend, and as a result I cannot maintain same location of stored files on a backend. Instead of root of the server, they will have to be served from a subdirectory. How can I modify nginx reverse proxy config so that it proxies all non-cached requests to backend to a subdirectory, and clients are not aware that anything has changed?




I cannot do anything on storage server to maintain old URL scheme, so I have to do it on a frontend. No 301/302 headers are supposed to be passed anywhere.



So currently I have:





What I want to achieve:






I have tried many dozens of configurations, without luck. When I am trying this configuration - instead of silently fetching data from different URL, it ends up in an infinite loop of adding test via regexp.



    location / {
rewrite /(.*) /test/$1 break;
proxy_pass http://f002.backblazeb2.com;
proxy_redirect off;
proxy_set_header Host $host;

proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;

proxy_cache_revalidate on;
proxy_read_timeout 2;
proxy_connect_timeout 3;

proxy_http_version 1.1;
proxy_set_header Connection "";

proxy_cache_valid 200 302 60s;
proxy_cache_valid 404 1m;


limit_conn perip 23;
limit_req zone=dynamic burst=60;

expires 24h;
}

Answer



The following worked:



server {

listen 80;
listen [::]:80;

server_name blablabla.com;
proxy_cache one;

listen 443 ssl http2;
listen [::]:443 ssl http2;

ssl_certificate ....;

ssl_certificate_key ....;

location / {
proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie;

proxy_pass https://f002.backblazeb2.com/file/some-directory/;##$request_uri appended automatically

[...]
}


No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...