• The new Python extension is now available. It allows customers to deploy and manage WSGI-based Python applications on their websites directly from Plesk.
  • The Plesk Mobile apps for iOS and Android and the Plesk Mobile Center extension have been deprecated, and are no longer available for new installations. In Plesk Obsidian 18.0.84, the Plesk Mobile Center extension will be removed from servers during the upgrade, and the Plesk Mobile apps will lose their server management features.
  • The Social Login extension has been deprecated and will be removed in Plesk Obsidian 18.0.84. After that, it will no longer be possible to log in to Plesk with a Google, Facebook, GitHub account, or with a passkey. Plesk usernames and passwords, as well as logging in with a WebPros Account, are not affected.

Question Nginx Directive Using .htpasswd While Blocking Others

tetrahall

Regular Pleskian
Server operating system version
CentOS 7.6
Plesk version and microupdate number
Plesk Obsidian 18.0.81.6
Hi,

I need to add a directive from within Plesk (Additional nginx directive)

The aim is:

1. Allow users to access URIs beginnig with specific words.

2. The word can be "a folder name" or just a word in the path immediately after mydomain.com

3. I need to allow myself to access URIs blocked for others

I have created the file ".htpasswd" containing a generated string (username and password). It is located in "/var/www/vhosts" outside "mydomain.com"

I have sought the help of AI on Google. Ai suggestions kept causing errors and AI kept changing its mind correcting the errors with an apology until I gave up.

The number of words is around 30.

I was told that using "MAP" is the better option.

Please help in writing the directive.

KInd regards.
 
Hi,

the reason the AI kept running into errors is probably that "map" is only allowed in the http {} context, but the "Additional nginx directives" field in Plesk is included inside the server {} block of your domain. So you need two pieces: the map in a separate file, and two small directives in the Plesk field.

The idea: the map decides per request whether basic auth is required or not. For the allowed words it returns "off" (= no login), for everything else it returns a realm text (= login required). Since you have around 30 words, this stays tidy and you only ever edit one list.

Step 1 - create the map file (replace "mydomain" and the words with your own):

Code:
nano /etc/nginx/conf.d/mydomain-auth-map.conf

Content:

Code:
map $uri $mydomain_auth {
    default                                      "Restricted area";
    ~^/(word1|word2|word3|folder1|folder2)(/|$)  off;
    ~^/\.well-known/                             off;
}

Notes on the map:
- Just extend the list inside the brackets with your ~30 words, separated by "|".
- "(/|$)" means the word has to be a full path segment, so /word1 and /word1/anything are open, but /word1abc is not. If you want real "begins with" matching (/word1abc also open), remove the "(/|$)" part.
- If your homepage should be public too, add this line: ~^/$ off;
- I left the /.well-known/ line in so Let's Encrypt renewals don't break.
- I used $uri (not $request_uri) on purpose, because it is already normalized/decoded, so nobody can sneak past the rules with encoded characters.

Step 2 - in Plesk go to Domains > mydomain.com > Apache & nginx Settings > Additional nginx directives and add:

Code:
auth_basic $mydomain_auth;
auth_basic_user_file /var/www/vhosts/mydomain.com/.htpasswd;

This works because auth_basic accepts a variable, and if that variable evaluates to "off", the authentication is simply skipped for that request.

Step 3 - make sure nginx can read your .htpasswd. On CentOS 7 it's best to use the default apr1 (MD5) hash, because the system crypt() there doesn't understand the bcrypt hashes that "htpasswd -B" creates:

Code:
htpasswd -c /var/www/vhosts/mydomain.com/.htpasswd yourusername
chown root:nginx /var/www/vhosts/mydomain.com/.htpasswd
chmod 640 /var/www/vhosts/mydomain.com/.htpasswd

(No htpasswd tool? "yum install httpd-tools", or generate the hash with "openssl passwd -apr1" and write the line "username:hash" into the file yourself.)

Step 4 - test the config and reload:

Code:
nginx -t && systemctl reload nginx

Then test with curl, one open and one blocked URL:

Code:
curl -I https://mydomain.com/word1/
curl -I https://mydomain.com/something-else/

The first one should return your normal status (200 or whatever the page gives), the second one should return "401 Unauthorized". With the correct login (curl -u yourusername https://mydomain.com/something-else/) you get in everywhere.

Two more things:
- Don't put the map into the Plesk field, that is exactly what throws "map directive is not allowed here".
- Files in /etc/nginx/conf.d/ are not touched by Plesk when it regenerates the domain configs, so the map survives updates. Just remember to run "nginx -t" before each reload when you edit the list.

Hope that helps, let us know if something doesn't work and post the output of "nginx -t".

Regards
Tessa
 
Tessa,

Thank you very much for your detailed and useful reply.

A few points, please:

1) Is "?word" considered a word or should "?" be ignored?

2) I did mention "30 words" in my post. Just in case more are needed, Is there a limit?

3) I am doing this straight on the server as I don't have the means to test it on a production pc.

As a quick test, can I use a directive in Plesk to do the following:

block everyone visiting a uri beginning with "users". I would use the username and password to access that uri.

The file .htpasswd has been created using the code you suggested.

Kind regards
 
Hi,

1) The "?" is not part of the path. $uri only contains the path without the query string, so /word1?foo=bar is treated as /word1 (open), and /?word1 is treated as just / (so "word1" is not matched). Simply don't put any "?" into your list. Two small things: the list is a regex, so if a word contains a dot or another special character, escape it with a backslash (word.txt becomes word\.txt). And matching is case-sensitive, so use ~* instead of ~ if /Word1 should match too.

2) I'm not aware of any hard limit, 30 words is nothing and a few hundred are no problem either. If the line gets too long for your taste, you can simply split it over several lines, nginx checks regex entries in the order they appear and the first match wins:
Code:
    ~^/(word1|word2|word3)(/|$)     off;
    ~^/(word4|word5|word6)(/|$)     off;

3) Testing directly on the live server is fine if you stick to this order, because "nginx -t" checks the config first and a reload with a broken config is refused, so the old working config simply keeps running.

For your quick test ("block everything starting with users") use the same mechanism, just inverted: everything is open by default, only /users... asks for the login. First create the map file (before touching Plesk, otherwise nginx complains about an unknown variable):
Code:
nano /etc/nginx/conf.d/mydomain-auth-map.conf

Content:
Code:
map $uri $mydomain_auth {
    default         off;
    ~^/users        "Restricted area";
}

This matches /users, /users/anything and also /usersfoo. If you only want /users and below it, use ~^/users(/|$) instead.

Check and reload:
Code:
nginx -t && systemctl reload nginx

Only then add the two lines in Plesk (Domains > mydomain.com > Apache & nginx Settings > Additional nginx directives), same as before:
Code:
auth_basic $mydomain_auth;
auth_basic_user_file /var/www/vhosts/mydomain.com/.htpasswd;

Then test (the first should work without login, the second should give 401, the third should work with your password):
Code:
curl -I https://mydomain.com/
curl -I https://mydomain.com/users/
curl -I -u yourusername https://mydomain.com/users/

Please don't try to do the quick test with a "location /users { ... }" block in the Plesk field. In Plesk's nginx config such a prefix location can bypass the handlers for PHP and other files, and then your PHP files under /users could be delivered as plain source code. With the variable approach the auth_basic sits on server level and is inherited by all of Plesk's locations, so nothing gets bypassed.

When the test works, just replace the map with the real list from my first post (default "Restricted area", your words set to off), run "nginx -t && systemctl reload nginx" again, and you're done. The two lines in Plesk stay exactly the same.

Regards
Tessa
 
I just need to double-check some thing, please.

Some files like css, js and images should not be blocked.

Files: css and js are in the same folder as the homepage.
Images are in folder /media.

Should some additional code be added to either "mydomain-auth-map.conf" or in Nginx directive, to allow for that?
 
Hi,

good that you ask, and yes: those need to be added to the map file (mydomain-auth-map.conf). Nothing changes in the Plesk field, the two auth_basic lines stay as they are. Everything that is not listed in the map is protected, so css, js and images are currently blocked too, and the browser would then ask for the login for each of them.

For your case it looks like this (keep your own word list in the first line):
Code:
map $uri $mydomain_auth {
    default                                      "Restricted area";
    ~^/(word1|word2|word3)(/|$)                  off;
    ~^/media/                                    off;
    ~^/[^/]+\.(css|js)$                          off;
    ~^/(index\.(php|html?))?$                    off;
    ~^/\.well-known/                             off;
}

What the new lines do:
- "~^/media/" opens everything inside /media (including subfolders), so all your images in there are free.
- "~^/[^/]+\.(css|js)$" opens css and js files that sit directly in the top-level folder, like /style.css or /script.js. Files with the same name in subfolders stay protected. If you also have css/js in other folders that should be open, use this instead: ~*\.(css|js)$ off; Be aware that this opens every css/js file everywhere on the site, which is usually harmless, but it's your call.
- "~^/(index\.(php|html?))?$" opens the homepage. This is worth explaining: when someone requests "/", nginx internally redirects to the index file (/index.php or /index.html), and the login check runs again for that new path. If only "/" is allowed, the homepage can still ask for a password. With this line both variants are covered. Adjust the file names if your homepage uses a different one.
- If you have other files that must stay public, like /favicon.ico or /robots.txt, add them the same way, for example: ~^/(favicon\.ico|robots\.txt)$ off;

Two reminders:
- If a request matches several lines, the first matching line wins. With the lines above there is no conflict, but keep it in mind if you add more.
- The regex is case-sensitive. Use "~*" instead of "~" if /Media/ or STYLE.CSS should match as well.

After editing, check and reload:
Code:
nginx -t && systemctl reload nginx

Then test with curl instead of the browser, because the browser caches your login and can fool you. Replace the file names with real ones from your site. These should all work without login (200):
Code:
curl -I https://mydomain.com/
curl -I https://mydomain.com/style.css
curl -I https://mydomain.com/media/yourimage.jpg

And this one must still give 401 (replace "somethingblocked" with a path that is not in your list):
Code:
curl -I https://mydomain.com/somethingblocked/

If one of the first three returns 401, post the URL and your current map here and we'll sort it out.

Regards
Tessa
 
All work as expected. Much obliged!

The very last:
I have access logs in:
/var/www/vhosts/mydomain.com/logs

access_log
access_ssl_log
proxy_access_log
proxy_access_ssl_log

For analysis purposes, it would be useful to join then in one file.
Is that possible?
 
Hi,

yes, that's possible, but one thing first, because it decides which files you should merge: on a standard Plesk setup nginx sits in front of Apache. The proxy_access_log and proxy_access_ssl_log are written by nginx, which sees every request (including the 401s from your auth_basic setup and everything nginx serves itself, like static files). The access_log and access_ssl_log are written by Apache and only contain the requests nginx passed on to it. So those are mostly a subset of the same requests. If you merge all four, many hits are counted twice and your statistics are wrong.

For analysis I'd merge only the two nginx logs (http + https). Have a quick look at the format first, both files should look like a normal "combined" log line:
Code:
cd /var/www/vhosts/mydomain.com/logs
head -n 2 proxy_access_log proxy_access_ssl_log

If the lines look alike, merge them and sort by time. A plain "sort" doesn't work on the date field because of the month names (Nov, Dec...), so the sort keys below take year, month, day and time one after another:
Code:
cd /var/www/vhosts/mydomain.com/logs
cat proxy_access_log proxy_access_ssl_log | LC_ALL=C sort -s -k4.9,4.12n -k4.5,4.7M -k4.2,4.3n -k4.14,4.21 > /root/mydomain-access-merged.log

I write the result to /root on purpose, so the merged file doesn't end up in the logs folder, where Plesk's log rotation and statistics tools could pick it up.

Check the result:
Code:
head -n 3 /root/mydomain-access-merged.log
wc -l proxy_access_log proxy_access_ssl_log /root/mydomain-access-merged.log

The last line of the wc output (the merged file) should show the sum of the first two.

Notes:
- The sort keys assume that the date is in the 4th space-separated field, like in the combined format ([01/Nov/2026:05:58:00 +0100]). That is true unless the user-name field contains a space, which is not the case with your htpasswd user.
- The time zone offset is ignored in the sorting, which doesn't matter as long as all lines come from the same server.
- These are only the current logs. Plesk rotates them and keeps the old ones next to them, some of them gzipped. Look at what is there before you add them:
Code:
ls -la /var/www/vhosts/mydomain.com/logs

If you see rotated nginx files you want to include (for example proxy_access_log.processed and the .gz ones), name them explicitly and use "zcat -f", which reads plain and gzipped files alike:
Code:
zcat -f proxy_access_log.processed.1.gz proxy_access_log.processed proxy_access_log | LC_ALL=C sort -s -k4.9,4.12n -k4.5,4.7M -k4.2,4.3n -k4.14,4.21 > /root/mydomain-proxy-http.log

(The file names above are just an example, use what ls shows you, and do the same for the ssl files.)

- If your analysis tool (GoAccess, AWStats, ...) can read several files at once, you can skip all of this and just hand it the files directly. GoAccess, for example, accepts more than one log file on the command line and doesn't need them sorted.

If the head output looks different, then the sort keys need an adjustment.

Regards
Tessa
 
Back
Top