Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Basically I'm trying to enable the authentication module when accessing to a specific part of a subdomain, which are dev.domain.com and pma.domain.com, they both must have the authentication module loaded. I can't seem to figure why my nginx configuration file isn't working.

In the second server block you can see the pma and dev with the authentication module, when I access to pma.domain.com or dev.domain.com, I don't see the authentication module showing from my browser, nor any error logs stored.

Anyway, I just need a fix to enable the authentication on them two subdomains, not an entire rewrite of my nginx configuration file.

server {

    server_name domain.com;

    root            /var/www/domain.com/www;
    index           index.php index.htm index.html;
    error_page      404 /404.html;
    error_page      500 502 503 504  /50x.html;

    access_log      /var/www/domain.com/logs/access.log;
    error_log       /var/www/domain.com/logs/errors.log;

    error_page 404  /index.php;

    location ~ \.php$ 
    {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /var/www/domain.com/www$fastcgi_script_name;
        include fastcgi_params;
    }
}

server {

    server_name ~^(.+)\.domain\.com$;

    set $file_path $1;

    root            /var/www/domain.com/www/$file_path;
    index           index.html index.php;

    access_log      /var/www/domain.com/logs/access.log;
    error_log       /var/www/domain.com/logs/errors.log;

    location /
    {
        try_files $uri /$uri /index.php?$args;
    }

    location ~ pma
    {
        auth_basic            "Website development";
        auth_basic_user_file  /var/www/domain.com/www/dev/authfile;
    }

    location ~ dev
    {
        auth_basic            "Website development";
        auth_basic_user_file  /var/www/domain.com/www/dev/authfile;
    }

    location ~ \.php$ 
    {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /var/www/domain.com/www$fastcgi_script_name;
        include fastcgi_params;
    }
}

Anyone?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

However, you need to split this two domains in separate server blocks and leave wildcard substitution for others, or try to use workaround below.

1. "location" directive works with URI only, not with host header

2. And if you try to do something like

if ($host ~ "(dev|pma).example.com" ) {
        auth_basic            "Website development";
        auth_basic_user_file  /var/www/domain.com/www/dev/authfile;
}

then you will get a

error nginx: [emerg] "auth_basic" directive is not allowed here in.....

because auth_basic directive is unconditional

workaround (not well tested):

    if ($host ~ "(dev|pma).example.com" ) {
        return 555;
    }

    error_page 555 = @auth;

    location @auth {
        auth_basic            "Website development";
        auth_basic_user_file  /var/www/domain.com/www/dev/authfile;
        try_files $uri /$uri /index.php?$args;
    }
share|improve this answer
    
I get this error when restarting nginx Restarting nginx: nginx: [emerg] unknown directive "if($host" –  MacMac Dec 20 '11 at 1:20
    
add a space symbol between "if" and "($host" –  ghloogh Dec 20 '11 at 2:25
    
Here's a weird thing, when I access the subdomains, the page constantly loads for a long time then shows a blank page after that. Any reason why? –  MacMac Dec 20 '11 at 3:13
    
Well... anything...? –  MacMac Dec 20 '11 at 13:45
    
At least give me a response. –  MacMac Dec 21 '11 at 9:43

Your Answer

 
discard

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.