• If you are still using CentOS 7.9, it's time to convert to Alma 8 with the free centos2alma tool by Plesk or Plesk Migrator. Please let us know your experiences or concerns in this thread:
    CentOS2Alma discussion

Resolved Location regex does not work in Additional Nginx directives

blueberry

Basic Pleskian
Hi,
I have a problem with additional nginx directives. Some location blocks are ignored by Nginx.
"Location =" does work but location (regex) (when involving a folder) does not work. I have performed some test on my test domain inadoo.com

I have entered the following code in additional nginx directives:
JavaScript:
location = /page321/ {
    deny all; # returns 403 it works. e.g: https://inadoo.com/page321/
}

location /page322/ {
    deny all; # it should return a 403 too but it does not work. It returns a 404 E.g: https://inadoo.com/page322/
}

location ~* .(page\/[0-9]+\/)$ {
    deny all; # https://inadoo.com/page/1/ among other should return a 403 but it does not work
}


1604567700829.png

The following should return a 403 for example according to the last regex directive but it returns a 404.

jojo@DESKTOP-KC3T7B7:~$ curl https://inadoo.com/page/32/ -I
HTTP/2 404
server: nginx
date: Thu, 05 Nov 2020 09:11:37 GMT
content-type: text/html; charset=UTF-8
vary: Accept-Encoding
x-powered-by: PHP/7.4.12
expires: Wed, 11 Jan 1984 05:00:00 GMT
cache-control: no-cache, must-revalidate, max-age=0
link: <https://inadoo.com/wp-json/>; rel="REST API Handbook | WordPress Developer Resources"

Is my formatting wrong?


I also added:
Code:
location ~* /(media|images|cache|tmp|logs)/.*.(php|jsp|pl|py|asp|cgi|sh)$ {
    return 403; # i tested https://inadoo.com/media/bloup.php but it does not return a 403
}
 
Last edited:
I have checked and this one worked on my nginx test server (1.19) but it does not work on PLesk

location ~* "^/page/[\d]+/$" {return 410;}

(to return a 410 for domain.com/page/2/ domain.com/page/23/ domain.com/page/14) ...)
 
I decided to take my pilgrim staff and go up the Plesk mountain.

My first stop was at /etc/nginx/plesk.conf.d/vhosts/inadoo.com.conf (my test domain)

I used location ~* "^/page/[\d]+/$" {return 410;} to see whether it worked.

I haved added the location block with regex at the end of the configuration file and after saving and doing service nginx restart it still didn't work (within server block listening on 80 port)
plesk problem nginx 2.jpg


Then, I have decided to move the directive just below the root directive and it worked.

Then, I have added the location regex just above the location block for index... and boom it worked. So it looks like this location block for index is the culprit.

plesk problem nginx 3.jpg

This file is a Plesk configuration file. Does it mean it is a PLesk bug?

The code added to the Nginx additional settings can be found in vhost_nginx.conf. This code is located after the location block (index) which cause trouble in the system. Would inserting the "include" before the location block indexes would solve the problem?
 
Last edited:
Problem was solved by using the regex in a context like this:

Code:
location / {


    location ~* "^/page/[\d]+/$" {
        return 410;
    }


}

In nginx additional settings
 
If you have unticked "Proxy Mode" to serve files with Nginx, then make sure "Serve static files directly by nginx" is also unticked, then your location block will work. Seems to me if proxy mode is off then the files are served by Nginx anyway.

All the controls for this are a bit cryptic and it should be reworked to make it clearer.
 
location ^~ /exampledirectory {
allow 225.238.108.185;
deny all;
}

Hi above is my ip address but still not allowing me to access the directory.
 
Back
Top