conditional auth_basic

Cedric Jeanneret cedric.jeanneret at
Tue Oct 5 09:33:40 MSD 2010


I have a special setup involving a wildcard :
server {
  server_name         *
  access_log          /var/log/nginx/itnux_acces.log main;
  error_log           /var/log/nginx/itnux_error.log info;
  add_header X-Devel  "$host - production site";

  location / { 
    proxy_pass        http://localhost:9095;
    proxy_set_header  Host $host;

I'd like to add some auth_basic, only if $host is NOT "".

I thought I could make it like that:
  location / { 
    proxy_pass        http://localhost:9095;
    proxy_set_header  Host $host;
    if ($host != '') {
      auth_basic  "Not for everyone ;)";
      auth_basic_user_file  /var/www/vhosts/;

But it seems that nginx doesn't like it:
nginx -t
[emerg]: "auth_basic" directive is not allowed here in /etc/nginx/vhosts/
configuration file /etc/nginx/nginx.conf test failed

How can I do this ? Do I have to declare "", and then the wildcard in two distinct servers ?

Thank you in advance.

Best regards,


Cédric Jeanneret                 |  System Administrator
021 619 10 32                    |  Camptocamp SA
cedric.jeanneret at  |  PSE-A / EPFL
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: not available
URL: <>

More information about the nginx mailing list