D
D
Damir Makhmutov2015-12-24 13:49:18
Nginx
Damir Makhmutov, 2015-12-24 13:49:18

How to proxy a request to a dynamic url?

Is it possible to proxy a request to a url that is the result of a regular expression in a location block?
For example:
I have a url of the form
/path/to/example/(.*)
Such requests should be proxied for $1
What requests can happen:
/path/to/example/http://google.com
/path/to/example//uploads/boobies.png
Everything after /path/to/example/is a valid url to which the request should be proxied.
I do not understand how this can be arranged, and is it possible at all?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nirvimel, 2015-12-24
@doodoo

http {
    server {
        listen *:80
        resolver 8.8.8.8;
        location ~ /path/to/example/(.*) {
             try_files $1 /path/to/proxy/$1
        }
        location ~ /path/to/proxy/(.*) {
            proxy_pass http://$1;
        }
    }
}

You can check: 127.0.0.1/path/to/example/google.com (but there is a 301 redirect to HTTPS right away).
UPD: Added the ability to return local statics. Haven't tested this myself yet.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question