THX
THX

Reputation: 573

nginx: how to setup as caching reverse proxy for global URLs

I am looking for a way to setup nginx to run as a reverse proxy(?) with caching but explicitly taking/rewriting original URLs, where the URL is no local server but any generic address.

E.g., I would like to explicitly cache/proxy URLs. So, for an original URL like https://org.url.baz/user/repo/foo I would like to be able to cache all request through nginx at my.domain.foo with a explicit "cache request" like

http://my.domain.foo/cache/http://org.url.baz/user/repo/foo

and rewrite all subsequent request. I.e., similar to Internet Archive's memento syntax archive.org/save/org.url.baz

Since my idea is no 'true' reverse proxy in any sense, the example in https://www.nginx.com/resources/wiki/start/topics/examples/reverseproxycachingexample/ is not fully applicable but needs heavy extension, I guess.

Upvotes: 0

Views: 264

Answers (1)

Danila Vershinin
Danila Vershinin

Reputation: 9845

You're looking after something like this.

Specifically:

location ~* ^/cache/(?<pschema>https?)/(?<phost>[\w.]+)(?<puri>/.*) {
{
    set $adr $pschema://$phost;
    rewrite .* $puri break;

    proxy_pass $adr;

    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $phost;
    proxy_set_header X-NginX-Proxy true;
    proxy_redirect off;
    proxy_connect_timeout 1;
    proxy_intercept_errors on;
    expires 30;
}

This would proxy your.example.com/cache/https/org.url.baz/user/repo/foo to https://org.url.baz/user/repo/foo

Note that your.example.com/cache/https://org.url.baz/user/repo/foo would probably not be a valid URL, so your URLs should not include :// in protocol specifier of remote websites.

Upvotes: 1

Related Questions