nginx密码保护适用于所有域的目录代码

I am new to nginx.

I have followed the tutorial at http://kbeezie.com/protecting-folders-with-nginx/ to password protect a directory using nginx.

I have many domains in the nginx configuration file and all those have 'admin' directory like /home/domain.com/public_html/admin

I have this code in each of the domain server block.

server {
        server_name domain.com www.domain.com;
        listen xxx.xx.xx.xxx;
        root /home/domain.com/public_html;

    location ~/admin {
        auth_basic "Admin Login";
        auth_basic_user_file /home/domain.com/public_html/admin/.htpasswd;
    }

    # This will deny access to any hidden file (beginning with a .period)
    location ~ /\. { deny  all; }

    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        include        fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
    }


    }

Is it possible to keep this code in the top server block so that it applies to all the admin directories for all domains ? (without keeping this code in each of the domain server blocks ?)

   server {
        listen       80;
        server_name  localhost;
        root         /usr/share/nginx/html;

# code to password protect all admin directories in all domains
}

I don't mind using the same password for all admin directories.

Please suggest.

You can separate the config you want to reuse into a new file and then use the

include [file];

pattern to inject the settings where ever you want them.