大网数据广告活动
服务器运维问答

Nginx caching reverse proxy with URL rewrite

· 52CE 我爱测

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,…

问题原文

原帖链接 · BarsMonster · CC BY-SA 4.0

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;
    }

已采纳解答

原帖链接 · BarsMonster · CC BY-SA 4.0

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

   [...]
}

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-11-03)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。