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...")
 
 
(3 intermediate revisions by the same user not shown)
Line 6: Line 6:
  
 
In server block, under location you wanna add password for, add:
 
In server block, under location you wanna add password for, add:
<pre>
+
<syntaxhighlight lang="nginx">
        auth_basic "Restricted Content";
+
auth_basic "Restricted Content";
        auth_basic_user_file /etc/nginx/.htpasswd;
+
auth_basic_user_file /etc/nginx/.htpasswd;
</pre>
+
</syntaxhighlight>
  
 
Example:
 
Example:
<pre>
+
<syntaxhighlight lang="nginx">
 
server {
 
server {
 
     listen 80 default_server;
 
     listen 80 default_server;
Line 30: Line 30:
  
  
</pre>
+
</syntaxhighlight>

Latest revision as of 09:45, 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;
    }
}