How do I password protect a website using NGINX?
I'm needing to password protect a (private) event website we are looking to host on our Linode so therefore need to apply a password to the homepage html file stored within a directory. Some research shows this is usually possible using .htaccess and .htpasswd files but it is dependent on server support for this. Do you know if this is possible with our Linode at all?
1 Reply
This is definitely possible with a Linode. The server support here is talking about the web server - in your case it's NGINX. NGINX doesn't support .htaccess files like Apache does, but it's still pretty simple to get basic password protection on your web page. NGINX has some great documentation, and I was able to find this one regarding how to set up some basic authentication:
Your server block configuration will vary depending on what you want password protected, but to protect the whole site just throw the auth_basic
lines in the main portion of the block after you've run htpasswd
to make your .htpasswd file:
server {
listen 80;
listen [::]:80;
server_name example.com;
root /var/www/example.com;
index index.html;
auth_basic "Password Protected";
auth_basic_user_file /var/www/example.com/.htpasswd;
location / {
try_files $uri $uri/ =404;
}
}
After that, test out your config with sudo nginx -t
and reload it by running sudo nginx -s reload
. You should be all set!