【问题标题】:password protect /backoffice folder in nginxnginx 中的密码保护/backoffice 文件夹
【发布时间】:2015-08-20 18:14:25
【问题描述】:

我正在尝试使用密码保护名为 backoffice 的文件夹。 我想用密码保护文件夹及其下面的所有内容(包括 PHP 文件)。

我似乎无法让它在 nginx 中工作。

我目前的配置是这样的:

server {
    listen   80;
    server_name  www.example.com;
    access_log  /var/log/nginx/localhost.access.log;
    access_log off;
    client_max_body_size 50m;

    ## Default location
    location / {
        root   /var/www/clients/client3/web21/web;
        index  index.php;
    }

    ## Images and static content is treated different
    location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
      access_log        off;
      expires           30d;
      root /var/www/clients/client3/web21/web;
    }

    ## Parse all .php file in the /var/www directory
    location ~ .php$ {
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        fastcgi_pass   unix:/dev/shm/fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/clients/client3/web21/web$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_param  QUERY_STRING     $query_string;
        fastcgi_param  REQUEST_METHOD   $request_method;
        fastcgi_param  CONTENT_TYPE     $content_type;
        fastcgi_param  CONTENT_LENGTH   $content_length;
        fastcgi_intercept_errors        on;
        fastcgi_ignore_client_abort     off;
        fastcgi_connect_timeout 30;
        fastcgi_send_timeout 30;
        fastcgi_read_timeout 30;
    }

    ## Disable viewing .htaccess & .htpassword
    location ~ /\.ht {
        deny all;
    }

    # Password Protect important Directories
    location ^~ /backoffice {
        root /var/www/clients/client3/web21/web;
        auth_basic            "Restricted";
        auth_basic_user_file   /var/www/clients/client3/web21/htpass;
    }
}

【问题讨论】:

    标签: nginx passwords


    【解决方案1】:

    您需要将 php 位置块嵌套在受保护的目录块中。像这样:

    location ^~ /protected {
        auth_basic              "Restricted";
        auth_basic_user_file    /usr/local/nginx/conf/password;
    
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            include        fastcgi.conf;
        }
    }
    

    【讨论】:

    • 而且我们仍然必须将 .php 配置保留在受保护的设置之外吗?
    猜你喜欢
    • 2014-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-18
    • 1970-01-01
    • 2011-07-26
    相关资源
    最近更新 更多