Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx location regex

Tags:

regex

nginx

I'm setting up nginx and I need to use the location directive to match all requests that begin with /site1 AND end with .php. What regex do I use to do this?

I know I can use ^ to match the beginning of the string and $ to match the end of string. So I'm able to match the beginning of the string OR the end of the string as ^(/site)|(\.php)$. Is there any 'and' operator so I can write something like ^(/site)&(\.php)$ ?

like image 410
ErJab Avatar asked Feb 04 '10 06:02

ErJab


People also ask

How does nginx match location?

To find a location match for an URI, NGINX first scans the locations that is defined using the prefix strings (without regular expression). Thereafter, the location with regular expressions are checked in order of their declaration in the configuration file.

Where is nginx location directive?

NGINX Configuration: Understanding Directives. Every NGINX configuration file will be found in the /etc/nginx/ directory, with the main configuration file located in /etc/nginx/nginx.

Does nginx support regex?

Support for regular expressions is one of the powerful features of NGINX, but regexes can be complex and difficult to get right, especially if you don't work with them regularly. NGINX allows regexes in multiple parts of a configuration, for example locations, maps, rewrites, and server names.

What is location in nginx conf?

By default, the configuration file is named nginx. conf and placed in the directory /usr/local/nginx/conf , /etc/nginx , or /usr/local/etc/nginx .


1 Answers

You want this: ^(/site).*(\.php)$. This means that first you match the beginning followed by "/site", then anything any number of times, and finally ".php" followed by the end of the string. If you don't need the captures, it would be simpler as: ^/site.*\.php$.

like image 77
Max Shawabkeh Avatar answered Oct 19 '22 00:10

Max Shawabkeh