Defining locations in Nginx supports PCRE regular expressions. This is nothing new, it has been around for many years.
The most popular and widely mentioned use case is a location used for file extensions:
location ~* \.(png|ico|gif|jpg|jpeg|css|js)$ {
...
}
This case insensitive (~*) regular expression location matches requested paths ending in either .png, .ico, .gif, .jpg, .jpeg, .css or .js.
In PHP-FPM environments this is often used to define what should happen with requests to .php files:
location ~* \.php$ {
try_files $uri =404;
fastcgi_pass fastcgi_backend;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_read_timeout 600s;
fastcgi_connect_timeout 600s;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
A much less documented way to use regular expressions in locations are "optional" paths. For example if you want to define a location /shop, you may also want to use the same location to match different languages: /de/shop, /en/shop, /fr/shop.
Of course you could do this by defining different locations for each language:
location /shop {
proxy_pass http://upstream;
}
location /de/shop {
proxy_pass http://upstream;
}
location /en/shop {
proxy_pass http://upstream;
}
location /fr/shop {
proxy_pass http://upstream;
}
Or you can use just one single location using regular expression and define optional (sub-) paths:
location ~ ^/(?:de|fr|en)(?:\/)shop/ {
proxy_pass http://upstream;
}
Let's analyze this regex in detail:
Now looking at this from top to bottom reveals:
No comments yet.
AWS Android Ansible Apache Apple Atlassian BSD Backup Bash Bluecoat CMS Chef Cloud Coding Consul Containers CouchDB DB DNS Database Databases Docker ELK Elasticsearch Filebeat FreeBSD Galera Git GlusterFS Grafana Graphics HAProxy HTML Hacks Hardware Icinga Influx Internet Java KVM Kibana Kodi Kubernetes LVM LXC Linux Logstash Mac Macintosh Mail MariaDB Minio MongoDB Monitoring Multimedia MySQL NFS Nagios Network Nginx OSSEC OTRS Observability Office OpenSearch PGSQL PHP Perl Personal PostgreSQL Postgres PowerDNS Proxmox Proxy Python Rancher Rant Redis Roundcube SSL Samba Seafile Security Shell SmartOS Solaris Surveillance Systemd TLS Tomcat Ubuntu Unix VMWare VMware Varnish Virtualization Windows Wireless Wordpress Wyse ZFS Zoneminder