Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Hello Serverfault members.

I want my NginX to use an optional .htpasswd file. The reason is, that my config dynamically serves the webcontent. Based on $http_host.

So I've tried the following:

if (-f "/foo/$http_host/.htpasswd") {
    auth_basic "Restricted";
    auth_basic_user_file "/foo/$http_host/.htpasswd";

But this does not work because auth_basic is not allowed inside an if. If I only use the two auth_basic lines it shows me a 403 for webcontent without .htpasswd.

Is there a possibility to achieve this without using a seperate config file for the protected contents?

share|improve this question
up vote 0 down vote accepted

It's impossible to do on nginx side, because nginx auth_basic_user_file doesn't allow to use variables.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.