Difference between revisions of "Setup password authentication in nginx"

From DevOps Notebook
(Created page with "Generate htpasswd password and put it in file <pre> # echo "admin:$(openssl passwd -apr1 Q89C82CNCeLxfSX3)" | sudo tee -a /etc/nginx/.htpasswd # cat /etc/nginx/.htpasswd </pre...")
 
Line 7: Line 7:
 
In server block, under location you wanna add password for, add:
 
In server block, under location you wanna add password for, add:
 
<pre>
 
<pre>
        auth_basic "Restricted Content";
+
auth_basic "Restricted Content";
        auth_basic_user_file /etc/nginx/.htpasswd;
+
auth_basic_user_file /etc/nginx/.htpasswd;
 
</pre>
 
</pre>
  

Revision as of 09:43, 9 April 2020

Generate htpasswd password and put it in file

# echo "admin:$(openssl passwd -apr1 Q89C82CNCeLxfSX3)" | sudo tee -a /etc/nginx/.htpasswd
# cat /etc/nginx/.htpasswd

In server block, under location you wanna add password for, add:

auth_basic "Restricted Content";
auth_basic_user_file /etc/nginx/.htpasswd;

Example:

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

    root /usr/share/nginx/html;
    index index.html index.htm;

    server_name localhost;

    location / {
        try_files $uri $uri/ =404;
        auth_basic "Restricted Content";
        auth_basic_user_file /etc/nginx/.htpasswd;
    }
}