Access control for users to specific HTML files

Could anyone help me create an NGINX configuration where users can log in with different credentials and then access specific HTML files based on their credentials? For example, user 'test' should have access to 'test.html', while user 'test2' can see 'test2.html'.
server {
listen 443 ssl;
server_name dev.DOMAIN.de;
root /var/www/html_DOMAIN/dev;
ssl_certificate /etc/letsencrypt/live/DOMAIN.de/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/DOMAIN.de/privkey.pem;

location / {
try_files $uri $uri/ =404;
auth_basic "Restricted Content";
auth_basic_user_file /etc/nginx/.htpasswd;
fancyindex on;
fancyindex_css_href /etc/nginx/dev/i.css;
fancyindex_localtime on;
fancyindex_exact_size off;
}

location = /test.html {
auth_basic "Restricted Content";
auth_basic_user_file /etc/nginx/.htpasswd;

if ($remote_user = "1") {
rewrite ^ /test.html break;
}
}

location = /test2.html {
auth_basic "Restricted Content";
auth_basic_user_file /etc/nginx/.htpasswd;

if ($remote_user = "2") {
rewrite ^ /test2.html break;
}
}
}
server {
listen 443 ssl;
server_name dev.DOMAIN.de;
root /var/www/html_DOMAIN/dev;
ssl_certificate /etc/letsencrypt/live/DOMAIN.de/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/DOMAIN.de/privkey.pem;

location / {
try_files $uri $uri/ =404;
auth_basic "Restricted Content";
auth_basic_user_file /etc/nginx/.htpasswd;
fancyindex on;
fancyindex_css_href /etc/nginx/dev/i.css;
fancyindex_localtime on;
fancyindex_exact_size off;
}

location = /test.html {
auth_basic "Restricted Content";
auth_basic_user_file /etc/nginx/.htpasswd;

if ($remote_user = "1") {
rewrite ^ /test.html break;
}
}

location = /test2.html {
auth_basic "Restricted Content";
auth_basic_user_file /etc/nginx/.htpasswd;

if ($remote_user = "2") {
rewrite ^ /test2.html break;
}
}
}
2 Replies
fynnpunkt
fynnpunkt7mo ago
So I would like that when I go to dev.DOMAIN.de and enter my password and username that I am then redirected to the corresponding page
Thibault
Thibault7mo ago
Hey, Why use NGINX to do this? A back-end would be more appropriate.