一款nginx中给目录增加密码保护实现程序,可以有效的保护一些目录不被访问,有需要的朋友可参考一下。

了防止一些可能出现存在漏洞的后台脚本暴露,使用验证的方式保护这些文件所在的目录

使用apache的htpasswd工具生成密码

yingouqlj@yingouqlj-laptop:~$ htpasswd -b -c filename username passwd
Adding password for user ******

nginx可以为网站或目录甚至特定的文件设置密码认证。密码必须是crypt加密的。可以用apache的htpasswd来创建密码。

格式为:htpasswd -b -c site_pass username password

site_pass为密码文件。放在同nginx配置文件同一目录下,当然你也可以放在其它目录下,那在nginx的配置文件中就要写明绝对地址或相对当前目录的地址。

如果你输入htpasswd命令提示没有找到命令时,你需要安装httpd.如centos是yum install httpd

如果是为了给网站加上认证,可以直接将认证语句写在nginx的配置server段中。

如果是为了给目录加上认证,就需要写成目录形式了。同时,还要在目录中加上php的执行,否则php就会被下载而不执行了。
例如:基于整个网站的认证,auth_basic在php解释之前。

 代码如下 复制代码
server { 
    listen       80; 
    server_name www.akii.org akii.org; 
    root  /www/akii; 
    index index.html index.htm index.php; 
  
    auth_basic "input you user name and  password"; 
    auth_basic_user_file /usr/local/nginx/conf/vhost/nginx_passwd; 
  
    location ~ .php$ { 
        fastcgi_pass  127.0.0.1:9000; 
        fastcgi_index index.php; 
        include fastcgi_params; 
    } 
    location ~ /.ht { 
         deny  all; 
    } 
    access_log /logs/akii.org_access.log main; 

针对目录的认证,在一个单独的location中,并且在该location中嵌套一个解释php的location,否则php文件不会执行并且会被下载。auth_basic在嵌套的location之后。

 代码如下 复制代码

1server { 
    listen       80; 
    server_name www.akii.org akii.org; 
    root  /www/akii; 
    index index.html index.htm index.php; 
  
    location ~ ^/admin/.* { 
        location ~ .php$ { 
            fastcgi_pass  127.0.0.1:9000; 
            fastcgi_index index.php; 
            include fastcgi_params; 
        } 
  
        auth_basic "auth"; 
        auth_basic_user_file /usr/local/nginx/conf/vhost/auth/admin.pass; 
    } 
  
    location ~ .php$ { 
        fastcgi_pass  127.0.0.1:9000; 
        fastcgi_index index.php; 
        include fastcgi_params; 
    } 
  
    location ~ /.ht { 
         deny  all; 
    } 
    access_log /logs/akii.org_access.log main; 
}

这里有一个细节,就是location ~ ^/admin/.* {…} 保护admin目录下的所有文件。如果你只设了/admin/ 那么直接输入/admin/index.php还是可以访问并且运行的。 ^/admin/.* 意为保护该目录下所有文件。当然,只需要一次认证。并不会每次请求或每请求一个文件都要认证一下。

附一个可用的bash脚本 用于创建密码

 代码如下 复制代码
   #!/bin/bash
   PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
   export PATH
 
   #set UserName
 
           username=""
           read -p "Please input UserName:" username
           if [ "$username" = "" ]; then
                   echo "Error:UserName can't be NULL!"
                   exit 1
           fi
           echo "==========================="
           echo "UserName was: $username"
           echo "==========================="
 
   #set password
 
           unpassword=""
           read -p "Please input the Password:" unpassword
           if [ "$unpassword" = "" ]; then
                   echo "Error:Password can't be NULL!"
                   exit 1
           fi
           echo "==========================="
           echo "Password was: $unpassword"
           echo "==========================="
   password=$(perl -e 'print crypt($ARGV[0], "pwdsalt")' $unpassword)
 
   #set htpasswd file
 
           htfile=""
           read -p "Please input Auth filename:" htfile
           if [ "$htfile" = "" ]; then
                   echo "Error:Auth filename can't be NULL!"
                   exit 1
           fi
           echo "==========================="
           echo "Auth File:$htfile"
           echo "==========================="
 
           get_char()
           {
           SAVEDSTTY=`stty -g`
           stty -echo
           stty cbreak
           dd if=/dev/tty bs=1 count=1 2> /dev/null
           stty -raw
           stty echo
           stty $SAVEDSTTY
           }
           echo ""
           echo "Press any key to Creat...or Press Ctrl+c to cancel"
           char=`get_char`
   if [ ! -f $htfile ]; then
     echo "Create Auth file......"
cat >$htfile<<eof
$username:$password
eof
   echo "Create Auth file successful,auth file path:$htfile"
   else
           echo "File already exists,please run this script again."
           exit 1
   fi

命令参数注释:

Usage:

htpasswd [-cmdpsD] passwordfile username
htpasswd -b[cmdpsD] passwordfile username password

htpasswd -n[mdps] username
htpasswd -nb[mdps] username password
-c Create a new file.
-n Don’t update file; display results on stdout.
-m Force MD5 encryption of the password (default).
-d Force CRYPT encryption of the password.
-p Do not encrypt the password (plaintext).
-s Force SHA encryption of the password.
-b Use the password from the command line rather than prompting for it.
-D Delete the specified user.

-b 使用命令行处理

nginx中给目录增加密码保护实现程序相关推荐

  1. CentOS6.4 安装OpenResty和Redis 并在Nginx中利用lua简单读取Redis数据

    1.下载OpenResty和Redis OpenResty下载地址:wget http://openresty.org/download/ngx_openresty-1.4.3.6.tar.gz Re ...

  2. 在应用程序级别之外使用注册为 allowDefinition='MachineToApplication' 的节是错误的。如果在 IIS 中没有将虚拟目录配置为应用程序,则可能导致此错误。...

    调试ASP.NET程序时发生错误:在应用程序级别之外使用注册为 allowDefinition='MachineToApplication' 的节是错误的.如果在 IIS 中没有将虚拟目录配置为应用程 ...

  3. 目录遍历漏洞:入侵检测php程序中的目录遍历漏洞,目录浏览(目录遍历)漏洞

    目录浏览(目录遍历)漏洞 来源:信管网 2019年08月26日 [所有评论] 目录浏览漏洞属于目录遍历漏洞的一种 目录浏览(目录遍历)漏洞 目录浏览漏洞是由于网站存在配置缺陷,导致网站目录可以被任意浏 ...

  4. nginx程序访问linux任意目录,通过nginx访问linux目录

    http { ...... autoindex on; autoindex_exact_size off; autoindex_localtime on; server { listen 80; .. ...

  5. Web集群案例实战 -- Nginx 反向代理根据URL中的目录地址实现代理转发 -- 案例实战

    Nginx 反向代理根据URL中的目录地址实现代理转发 -- 案例实战 前言 一.需求背景 前言 本环境是基于 Centos 7.8 系统构建Nginx学习环境 具体构建,请参考 Nginx-1.18 ...

  6. linux nginx root目录,详解nginx.conf 中 root 目录设置问题

    在配置 nginx.conf 总会遇到一些问题,下面列举一些常见的问题并说明如何解决 1.相对路径的问题 例如配置文件中 location 设置 location ~ .php${ root html ...

  7. http响应Last-Modified和ETag以及Apache和Nginx中的配置

    基础知识 1) 什么是"Last-Modified"? 在浏览器第一次请求某一个URL时,服务器端的返回状态会是200,内容是你请求的资源,同时有一个Last-Modified的属 ...

  8. Nginx中server_name 参数详解

    Nginx中的server_name指令主要用于配置基于名称的虚拟主机,server_name指令在接到请求后的匹配顺序分别为: 1.准确的server_name匹配,例如: server {list ...

  9. nginx中的502错误

    遇到这种情况,首先看一下慢日志 [17-Aug-2015 13:13:43] WARNING: [pool www] child 27780, script '/data/s.com/index.ph ...

最新文章

  1. BestCoder Round #80 1002
  2. 简单介绍Kubernetes
  3. android进程间对象传递,Android Fragment 间对象传递
  4. sql server中的varchar和Nvarchar有什么区别?
  5. 高并发面试 - 如何设计一个高并发系统?
  6. mysql workbench企业_甲骨文发布MySQL Workbench 6.0版本
  7. ASP.NET Core 异常处理与日志记录
  8. C/C++编程(尾积相乘)
  9. 风变编程Python9 函数的学习
  10. RPC(RemoteProcedureCallProtocol)
  11. linux设备驱动归纳总结(三):1.字符型设备之设备申请【转】
  12. tdd:spock工作原理_TDD与 数学形式主义:敌还是友?
  13. Android手机端脚本录制
  14. 疫情期间谁干谁赚钱的三个小副业,普通人也能干,基本不用你投资
  15. 关于计算机论文摘要和引言,论文摘要和背景的区别_论文前言与摘要的区别_引言和摘要有什么区别...
  16. 漫画算法:什么是跳跃表?
  17. HDFS集群管理与运维+distcp工具的使用
  18. 面向对象开发期末复习概述(四)
  19. JetBrains全家桶软件
  20. 【Audio】Unity音频模块:加载、转换、剪切、混音、合并

热门文章

  1. Drupal 修复远程代码执行漏洞
  2. jQuery图片水平滑动延迟加载动画
  3. 查看linux服务器内存使用情况,不够时创建Swap、手动 cached
  4. 用Java实现HTTP文件队列下载
  5. [CentOs7]iptables防火墙安装与设置
  6. Chronos首页、文档和下载 - 作业调度器 - 开源中国社区
  7. IIS6上配置CGI有两个要点(转)
  8. 批量修改文本文件编码GB18030为UTF-8
  9. 用Python解压tgz文件
  10. post和php input,PHP“php:/Input”vs$POST