Thursday 5 December 2019

NGINX: Map query parameters to variables

'$arg_{queryParameterName}' is used to access the value associated with given query parameter

nginx.conf
events {
    
}

http {
    server {
        listen 9090;

        server_name localhost;

        location = /welcome {
            return 200 "name: $arg_name, \nage: $arg_age";
        }
    }
}

Validate and reload the configuration.
$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 the url ‘http://localhost:9090/welcome?name=krishna&age=30’ in browser, you can see below screen.


Previous                                                    Next                                                    Home

No comments:

Post a Comment