# Allow access to only a specific user from a htpasswd file

**URL:** <https://www.ruby-forum.com/t/allow-access-to-only-a-specific-user-from-a-htpasswd-file/210422>\
**Category:** NGINX\
**Created:** [August 8, 2011, 4:39am UTC](https://www.ruby-forum.com/t/allow-access-to-only-a-specific-user-from-a-htpasswd-file/210422 "2011-08-08T04:39:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [August 8, 2011, 4:39am UTC](https://www.ruby-forum.com/t/allow-access-to-only-a-specific-user-from-a-htpasswd-file/210422/1 "2011-08-08T04:39:06Z")

</div>

Hi,

I have a password file that contains several users, but for one  
particular page on my site, I want only one specific user from that file  
to be allowed access. Apache has a “Require user” directive for this,  
and I’m looking for an equivalent in nginx.

I’ve tried testing the value of $remote\_user – this is what I have at  
the moment:

location /foo {  
auth\_basic “Foo”;  
auth\_basic\_user\_file “foo-htpasswd”;

```
     if ($remote_user = "leigh") {
             proxy_pass http://127.0.0.1:19000;
     }

```

}

This mostly works, but if you log in with an account other than “leigh”  
that’s in the password file, the browser returns a 404 and no longer  
prompts for login details. I’d like to keep prompting the user for a  
login until they successfully log in with the “leigh” account.

Thanks  
Leigh

---

<div class="post-metadata">

**Author:** ![Leigh\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ebca7d/32.png) [@Leigh\_D](https://www.ruby-forum.com/u/Leigh_D)\
**Post date:** [August 8, 2011, 6:01am UTC](https://www.ruby-forum.com/t/allow-access-to-only-a-specific-user-from-a-htpasswd-file/210422/2 "2011-08-08T06:01:46Z")

</div>

Leigh D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> I’ve tried testing the value of $remote\_user – this is what I have at  
> the moment:  
> location /foo {  
> auth\_basic “Foo”;  
> auth\_basic\_user\_file “foo-htpasswd”;

> ```
> if ($remote_user = "leigh") {
> proxy_pass http://127.0.0.1:19000;
> }
> 
> ```
> 
> }  
> This mostly works, but if you log in with an account other than “leigh”  
> that’s in the password file, the browser returns a 404 and no longer  
> prompts for login details.

Just a suggestion, and I’m by no means an expert, but I may have begun  
to learn how if blocks really work. Try this:

location /foo {  
auth\_basic “Foo”;  
auth\_basic\_user\_file “foo-htpasswd”;  
proxy\_pass [http://127.0.0.1:19000](http://127.0.0.1:19000);  
if ($remote\_user != “leigh”) {  
return 403;  
}  
}

## and see if that helps.

Dave H. - Consultant - Altadena CA, USA - [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> > > The opinions expressed above are entirely my own \<\<\<

It’s easier to fight for one’s principles than to live up to them.
