Regex not being evaluated correctly in location directive

Please use this template for troubleshooting questions.

My issue:
Nginx 1.21.5 / SUSE 15SP6 build

nginx -V
nginx version: nginx/1.21.5
built by gcc 7.5.0 (SUSE Linux)
built with OpenSSL 3.1.4 24 Oct 2023
TLS SNI support enabled
configure arguments: --prefix=/usr/ --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/run/nginx.pid --lock-path=/run/nginx.lock --http-client-body-temp-path=/var/lib/nginx/tmp/ --http-proxy-temp-path=/var/lib/nginx/proxy/ --http-fastcgi-temp-path=/var/lib/nginx/fastcgi/ --http-uwsgi-temp-path=/var/lib/nginx/uwsgi/ --http-scgi-temp-path=/var/lib/nginx/scgi/ --user=nginx --group=nginx --without-select_module --without-poll_module --with-threads --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-perl=/usr/bin/perl --with-mail=dynamic --with-mail_ssl_module --with-stream=dynamic --with-stream_ssl_module --with-stream_realip_module --with-stream_ssl_preread_module --with-pcre --with-pcre-jit --with-cc-opt=‘-fmessage-length=0 -grecord-gcc-switches -O2 -Wall -D_FORTIFY_SOURCE=2 -fstack-protector-strong -funwind-tables -fasynchronous-unwind-tables -fstack-clash-protection -g -fPIC -D_GNU_SOURCE’ --with-ld-opt=‘-Wl,-z,relro,-z,now -pie’ --with-compat

How I encountered the problem: Nginx not using the correct matching regex location directive and dropping to the save prefixed.

Solutions I’ve tried: Different regex expressions, debug mode, then setting up different responses based on the location directive used.

**Version of NGINX or NGINX adjacent software (e.g. NGINX Gateway Fabric):**nginx version: nginx/1.21.5

Deployment environment:

This is a reverse proxy setup to either redirect to a offsite hosted site or to a tomcat/web server backend based on words contained in the path of the uri.

Not actual code but same structure & order of location prefex/modifiers:

  1. location ~* ^/app1.*(word1|word2).*$

regex #1 (app#1 ) based on certain words{ redirect to offsite. }

  1. location ~* ^/app1.*(word1|word2).*$

regex #2 (app#1 ) based on certain words { proxy pass to tomcat instance#1 }

  1. location ~* ^/app1$ regex #3 based app name /app1 { proxy pass to tomcat instance#1 }

  2. location ~* ^/app2$ regex #4 based app name /app2 { proxy pass to tomcat instance#2 }

  3. location / { proxy pass to tomcat instance } { proxy pass to tomcat instance#1 }

If a uri, that has been tested on multiple regex testing engines and matches either location Directives 1 or 2 nginx seem to fail to match and drops to location directive 5.

Directives 3 and 4 however appear to work.

No idea why this is not working correctly.

Hey @chrisi88! NGINX regexes can sometimes be a bit finicky! Any chance you can share the actual regexes you are using? Maybe change the words if you have potential security concerns?

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.