Nginx: allow access only to referrer that match lo

2019-04-02 03:50发布

问题:

Is there a way, in nginx, to allow access to a "location" only to clients with a referrer that matches the current location name?

This is the scenario:

http://foooooo.com/bar.org/

http://foooooo.com/zeta.net/

etc etc

I want the contents of the bar.org location available only if the referrer is bar.org. The same goes for zeta.net

I know I can do this "statically", but there are a lot of those locations and I need to find a way to do this defining only one "dynamic" location.

Sorry for my bad english.

SOLUTION

I've solved this way:

location ~/([a-zA-Z0-9\.\-]*)/* {
    set $match "$1::$http_referer";
    if ($match !~* ^(.+)::http[s]*://[www]*[.]*\1.*$ ) {
        return 403;
    }
}

回答1:

location ~ ^/([a-zA-Z0-9\.\-]*)/(.*) {
    if ($http_referer !~ "^$1.*$"){
            return 403;
    }
}