Nginx Ingress

Rewrite URI using a regex pattern

Evaluate different approaches to rewrite the URI based on the subdomain of Nginx Ingress

Sometimes you may need to use a wild card domain name in the Nginx ingress and route traffic by rewriting the URI based on the subdomain name. For example, consider wildcard domain *; > service-name:port/foo/bar
server {     
server_name ~^(?<prefix>\w+)-(?<suffix>\w+)\.domain\.com$;
location / {
proxy_pass http://service_upstream/$prefix/$suffix$uri;
How Nginx configuration generated from Nginx ingress resource.

Adding regex pattern to the default server block

Adding URI rewrite logic using a Lua Script

Adding URI rewrite logic using a Map directive

This is the recommended approach considering the drawbacks of the aforementioned approaches. A map directive[6] is an Nginx way of defining a method; a map creates a new variable whose value depends on the values of one or more of the source variables specified in the first parameter.

map $http_host $prefix { 
~(?<prefix>\\w+)-(?<suffix>\\w+)\\.domain\\.com $prefix;
default ‘’;

Cloud Engineer