I am trying to restrict users visiting a site via username and password. Doing it in apache is very simple. I am trying to do it in nginx for different ports, not just 80, but some other ports that the application hosted in the server will use. (It's a nodejs application, and currently it opens on port 3000)
What I have tried till now: Installed apache2 utility:
sudo apt-get install apache2-utils
create the htpassword user/password:
sudo htpasswd -c /etc/nginx/.htpasswd username
added the auth_user in etc/nginx/conf.d/default.conf:
. . .
listen 80;
server_name localhost;
location / {
. . .
auth_basic "Private Property";
auth_basic_user_file /etc/nginx/.htpasswd;
}
. . .
reload nginx:
sudo service nginx reload
Now, when I open the site it's asking me for the username/password, but once I give them, it keeps asking me and doesn't let me log in.
I also want to add another port to be blocked via user/pass, like port 3000 ;
In etc/nginx/conf.d/default.conf I tried to add the ports the following ways:
. . .
listen 80;
listen 3000;
server_name localhost;
location / {
. . .
auth_basic "Private Property";
auth_basic_user_file /etc/nginx/.htpasswd;
}
. . .
AND
server {
listen 80;
server_name localhost;
location / {
...
auth_basic "Restricted Content";
auth_basic_user_file .htpasswd;
}
}
server {
listen 3000 default_server;
server_name localhost;
location / {
...
auth_basic "Restricted Content";
auth_basic_user_file .htpasswd;
}
}
But it throws error, when nginx is reloaded.
So my question is:
How do I make the htpassword to work properly?
How do I make the htpassword to work properly for ports other than 80 and the application still runs properly?