Monday 2 December 2019

NGINX: location: match Regular Expression


Below snippet case insensitively match any uri that contains the string ‘welcome’.

location ~* /welcome[0-9] {
    return 200 "Hey, welcome to NGINX";
}

nginx.conf
events {
    
}

http {
    server {
        listen 9090;

        server_name localhost;

        location ~* /welcome[0-9] {
            return 200 "Hey, welcome to NGINX";
        }
    }
}


Validate and reload the configuration file.
$sudo nginx -t
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
$
$sudo nginx -s reload

Open any of the following uris.

http://localhost:9090/123/WELCOME123/asdf
http://localhost:9090/WELCOME123/asdf
http://localhost:9090/WELCOME123
http://localhost:9090/welcome2


You can see below kind of screen.

Previous                                                    Next                                                    Home

No comments:

Post a Comment