douzao9845 2015-09-19 14:47 采纳率: 0%
浏览 40

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.

展开全部

  • 写回答

1条回答 默认 最新

  • dongxing7318 2015-12-05 13:23
    关注

    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.

    评论
    编辑
    预览

    报告相同问题?

    手机看
    程序员都在用的中文IT技术交流社区

    程序员都在用的中文IT技术交流社区

    专业的中文 IT 技术社区,与千万技术人共成长

    专业的中文 IT 技术社区,与千万技术人共成长

    关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

    关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

    客服 返回
    顶部