nginx下使用HTTP basic auth保护网站目录

10 篇文章 1 订阅
6 篇文章 0 订阅

 nginx下使用HTTP basic auth保护网站目录。

 

最近因远程测试数据问题,安装phpmyadmin数据库管理工具,为了加强安全性,用nginx做了 auth basic 验证。

基于同一个nginx虚机(server块)下,把phpmyadmin改名为data-manager放到nginx虚机的根目录,增加location作用域:

location /data-manager/ {
        index  index.html index.htm index.php;
        auth_basic "Please Login!";
        auth_basic_user_file /usr/local/nginx/conf/auth/nginxpwd;
    }

要注意的是 location  /data-manager/是以/data-manager/开头的,与location / 二选一,所有要增加一个 index 设置。基于https的完整配置代码如下。

 

# HTTPS server

server {
    listen       443 ssl;
    server_name  www.mydomain.com;

    root          /data/server_data/nginx/html/www.mydomain.com;

    ssl_certificate      /usr/local/nginx/conf/ssl/www.mydomain.com/1_www.mydomain.com_bundle.crt;
    ssl_certificate_key  /usr/local/nginx/conf/ssl/www.mydomain.com/2_www.mydomain.com.key;

    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;

    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers  on;

    location / {
        index  index.html index.htm index.php;
        try_files $uri $uri/  /index.php?$args;
    }

    location /data-manager/ {
        index  index.html index.htm index.php;
        auth_basic "Please Login!";
        auth_basic_user_file /usr/local/nginx/conf/auth/nginxpwd;
    }

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

}

 

如果是用 location ^~ 正则模式,因为此模式被匹配后即停止匹配其它location,还需将php的代理放入该块中才能解析php,如下所示:

location ^~ /data-manager/ {
        index  index.html index.htm index.php;
        auth_basic "Please Login!";
        auth_basic_user_file /usr/local/nginx/conf/auth/nginxpwd;
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值