NGINX: How can I extract some value from a URI and use it in the auth_basic_user_file?

I provide several download folders for different clients. Each folder must be password protected with its own .htaccess file.

To easily configure my nginx, I have the following question:

How can I extract the customer name ("customerA") from the URI and use it in the auth_basic_user_file?

This is the configuration I'm using right now:

server {
    root /usr/share/nginx/www;

    server_name localhost;

    autoindex on;

    location /build/customerA {
            index build.json;
            auth_basic "CustomerA Access Only!";
            auth_basic_user_file /usr/share/nginx/www/build/customerA/.htpasswd;
    }

    location /build/customerB {
            index build.json;
            auth_basic "CustomerB Access Only!";
            auth_basic_user_file /usr/share/nginx/www/build/customerB/.htpasswd;
    }
}

Thanks Sebastian

+4
source share
1 answer

auth_basic_user_file, nginx 403 ( 1.7.5). , . , , :

location ~ ^/build/(?<customer>[^/]+) {
    index build.json;
    auth_basic "$customer Access Only!";
    auth_basic_user_file /usr/share/nginx/www/build/$customer/.htpasswd;
}

, . , :

location /build/ {
    location ~ ^/build/(?<customer>[^/]+) {
        index build.json;
        auth_basic "$customer Access Only!";
        auth_basic_user_file /usr/share/nginx/www/build/$customer/.htpasswd;
    }
}

:

map $uri $no_customer {
    default 1;
    ~^/build/(?<customer>[^/]+) 0;
}

:

location /build/ {
    if ($no_customer) { # optionally forbid if no customer specified
        return 403;
    }
    index build.json;
    auth_basic "$customer Access Only!";
    auth_basic_user_file /usr/share/nginx/www/build/$customer/.htpasswd;
}
+2

All Articles