If you haven't installed Nginx already, do so with:
sudo apt update
sudo apt install nginx
For basic authentication, you'll create a password file. The htpasswd
tool from the apache2-utils
package will help:
sudo apt install apache2-utils
Now, create a user and password. This will create a new file with the user and the encrypted password:
sudo htpasswd -c /etc/nginx/.htpasswd username
You'll be prompted to enter and confirm your password.
Open the default configuration:
sudo nano /etc/nginx/sites-available/default
Now, within the server block, add or modify the location
directive:
server {
listen 80 default_server;
listen [::]:80 default_server;
...
location /your-url {
proxy_pass http://localhost:42110;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
# Basic Auth setup
auth_basic "Restricted Content";
auth_basic_user_file /etc/nginx/.htpasswd;
}
...
}
Save and exit the editor (in nano, it's CTRL+X
to close, Y
to confirm changes, and Enter
to save).
Test the configuration to ensure there are no syntax errors:
sudo nginx -t
If the configuration test is successful, restart Nginx to apply the changes:
sudo systemctl restart nginx
Now, when you try to access http://your_server_ip/your-url
, Nginx will prompt you for the username and password. Once authenticated, you'll be routed to the service running on localhost:42110
.