How to "Require User" like Apache in Nginx

Hi all,

I was just wondering, how to do the following in nginx:


<Directory /var/www/vhost/aa/htdocs/>
AuthType Basic
AuthUserFile /var/www/auth/htpasswd
AuthName “pass please”
require valid-user
Allow from all

Require user userX

<LocationMatch “/(favicon.ico|style.css|logo.png)”>
Require valid-user

<Location “/location-1”>
Require user userX userY

<Location “/location-2”>
Require user userX userZ

For what I came up with, but I’m still missing the default user ‘userX’


location / {
auth_basic “Munin”;
auth_basic_user_file /var/www/auth/htpassd;

userX is allow everywhere

below makes site inaccessible

if ( $remote_user != ‘userX’ ) {

return 401;



location ~ ^/location-1/ {
if ( $remote_user !~ ‘^(userX|userY)$’ ) {
return 401;

location ~ ^/location-2/ {
if ( $remote_user !~ ‘^(userX|userZ)$’ ) {
return 401;

Any idea how to solve this?


Frodo L.

Posted at Nginx Forum:,248229,248229#msg-248229

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs