How to set up? Nginx directory password authentication /usr/local/nginx/conf/vhost/xxx.xxx.com.conf site configuration files.
I want to in this website xxx.xxx.com/123 folders in password access protection, similar to the apache.But I don't know how to make nginx?
Heard: a folder password protection to set format the location?

        listen       80;
        server_name xxx.xxx.com; #server_name end
        index index.html index.htm index.php; #index end

        set $subdomain '';
        root  /home/wwwroot/xxx.xxx.com/web$subdomain;
        include rewrite/amh.conf; #rewrite end

        error_page 400 /ErrorPages/400.html;
        error_page 403 /ErrorPages/403.html;
        error_page 404 /ErrorPages/404.html;
        error_page 502 /ErrorPages/502.html;
        location ~ /ErrorPages/(400|401|403|404|405|502|503)\.html$ 
                root /home/wwwroot/xxx.xxx.com/web;

        location ~ .*\.php$
                fastcgi_pass  unix:/tmp/php-cgi-xxx.xxx.com.sock;
                fastcgi_index index.php;
                include fcgi-host.conf;
                fastcgi_param DOCUMENT_ROOT  /web$subdomain;
                fastcgi_param SCRIPT_FILENAME  /web$subdomain$fastcgi_script_name;

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp3|wma)$
                expires      30d;

        location ~ .*\.(js|css)$
                expires      12h;

        access_log off; #access_log end
        error_log /dev/null; #error_log end

1 Answer 1


For HTTP basic authentication using a .htpasswd file, use the auth_basic module in a location block for the protected directory:

location /123 {
  auth_basic "Protected";
  auth_basic_user_file /path/to/.htpasswd;
  • Placed in what position?
    – YingRui
    Commented Sep 9, 2014 at 3:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.