douzao9845 2015-09-19 22: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 21: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.

    评论

报告相同问题?

悬赏问题

  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM