Nginx Block/Deny Access to multiple locations regex

apache, nginx, regex

Solution

As the apache regex has '^', we can put '^' to force matching from the start of the path too.

location ~ ^/(xampp|security|phpmyadmin|licenses|webalizer|server-status|server-info) {
  proxy_pass         http://127.0.0.1:8080$request_uri;
  .... allow/deny directives come here
}

[EDIT] The matched string inside the brackets is stored in $1. So you may try

http://127.0.0.1:8080/$1

if that's what you want. However, my understanding is that you want to pass the entire uri path to the apache server. In that case, it's simpler to use nginx variable $request_uri.

Problem

I am using Nginx as a reverse proxy for my Apache instillation and as a security feature it blocks access to phpmyadmin, webalizer etc for everyone except localhost but using nginx it makes Apache think it is localhost so it displays it publicly for everyone. ``` <LocationMatch "^/(?i:(?:xampp|security|phpmyadmin|licenses|webalizer|server-status|server-info))"> Order deny,allow Deny from all Allow from ::1 127.0.0.0/8 \ fc00::/7 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16 \ fe80::/10 169.254.0.0/16 ErrorDocument 403 / </LocationMatch> ``` I need to turn the above rules pattern matching regex into the following. ``` location /phpmyadmin { proxy_pass htt://127.0.0.1:8080/phpmyadmin; allow 127.0.0.1; deny all; } ``` Much appreciated for help from anyone who is familiar with regex in Nginx. The following method works but breaks normal site urls that would be search engine friendly such as domain.com/forums/server-info ``` location ~ /(xampp|security|phpmyadmin|licenses|webalizer|server-status|server-info) { deny all; } ```

Original source