使用特定子域进行身份验证

Mac*_*Mac 4 nginx authentication subdomain

基本上,我试图在访问子域的特定部分时启用身份验证模块,它们是dev.domain.compma.domain.com,它们都必须加载身份验证模块。我似乎无法弄清楚为什么我的 nginx 配置文件不起作用。

在第二个服务器块中,您可以看到带有身份验证模块的pmadev,当我访问pma.domain.com或 时dev.domain.com,我没有看到浏览器中显示的身份验证模块,也没有存储任何错误日志。

无论如何,我只需要修复以启用对它们两个子域的身份验证,而不是完全重写我的 nginx 配置文件。

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

任何人?

ghl*_*ogh 6

但是,您需要在单独的服务器块中拆分这两个域,并为其他域保留通配符替换,或者尝试使用下面的解决方法。

1.“位置”指令仅适用于 URI,不适用于主机头

2.如果你尝试做类似的事情

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

然后你会得到一个

错误 nginx: [emerg] "auth_basic" 指令在这里是不允许的......

因为 auth_basic 指令是无条件的

解决方法未经充分测试):

    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;
    }
Run Code Online (Sandbox Code Playgroud)


Tom*_*wik 6

一种更简单的解决方案是使用地图

map $http_host $auth_type {
    default "off";
    example.domain.tld "Restricted";
}

server {
    auth_basic $auth_type;
}
Run Code Online (Sandbox Code Playgroud)

或者

nginx 1.5.4+auth 模块开始:

http://nginx.org/en/docs/http/ngx_http_auth_request_module.html