0

I am new to NGINX and need to know how to do this in a special location (folder)

  • remove all query parameters
  • proxy request to another server
  • cache the result locally for x minutes

Config snippet:

location /cache {
    rewrite /cache/([^/\?]) /cache/$1 break;
    proxy_pass http://foo.bar/original/;
    expire 5m;
}

I already saw different ideas with overwriting $args and using ? for arguments removal. But I could not get anything to work like expected.

E.g.

1. Request
---
Request: http://foo.bar/cache/text.css?abc=123
Rewrite: http://foo.bar/cache/text.css
Cache miss
Proxy:   http://fuzzu.buzzi/original/text.css
(store in local cache)
Expire:  http://foo.bar/cache/text.css (after 5min)

2. Request
---
Request: http://foo.bar/cache/text.css?abc=123
Rewrite: http://foo.bar/cache/text.css
Cache hit
5
  • 1
    Can you give a concrete example we can try to implement? I'm having trouble working out what you're trying to achieve.
    – womble
    Aug 28, 2015 at 21:01
  • I added an example =)
    – Möhre
    Aug 28, 2015 at 23:44
  • 1
    Thanks for the example, it makes life a little easier. What's the general rule that translates test.css to text.css?
    – womble
    Aug 28, 2015 at 23:46
  • ouch... that's a late night typo. I fixed it.
    – Möhre
    Aug 29, 2015 at 1:46
  • @womble do you have any idea?
    – Möhre
    Aug 30, 2015 at 21:31

1 Answer 1

0

So finally I found the solution myself:

proxy_cache_path /tmp/nginx levels=1:2 keys_zone=foo_bar:1m inactive=10m;

server {
    listen 80;
    server_name foo.bar;

    # DNS lookup
    resolver 8.8.8.8;
    resolver_timeout 1s;

    # use regex to not forward query string
    location ~ ^/cache/(.*)$ {
        proxy_cache foo_bar;

        # cache key only with host + path but without query string
        proxy_cache_key $host$uri;

        # nginx should ignore cache headers but forward them
        proxy_ignore_headers Cache-Control;

        proxy_cache_valid 5m;
        proxy_pass http://fuzzu.buzzi/original/$1;

        add_header X-Proxy-Cache $upstream_cache_status;
    }

}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .