nginx protect directory



Terminal:
sudo apt-get install apache2-utils



Terminal:
nano /etc/nginx/sites-available/default
replace with this:


server {

    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /var/www/web/;
    index index.php index.html index.htm;

    server_name localhost;

    location / {
        auth_basic "Restricted";                                #For Basic Auth
        auth_basic_user_file /etc/nginx/.htpasswd;  #For Basic Auth
        try_files $uri $uri/ =404;
    }

    client_max_body_size 10G;

    location /monitor/ {
        auth_basic "Restricted";                                #For Basic Auth
        auth_basic_user_file /etc/nginx/.htpasswd;  #For Basic Auth
    }
    
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }   
}


now generate user and password for access:

Terminal:
sudo htpasswd -c /etc/nginx/.htpasswd exampleuser
   
Terminal:
sudo htpasswd /etc/nginx/.htpasswd another_user

Terminal:
service nginx restart 


0 comentarios :