Link Search Menu Expand Document
Table of contents

Variables syntax

$arg 

Use variable $arg

server {
    listen 80;
    server_name 198.255.25.1;
    root /sites/demo; 

    if ( $arg_apikey != 1234 ) {
        return 401 "incorrect API key";
    }  

Example request:

https://example.com?apikey=1234
    > 200; 

Or, try request:

https://domain.com
    > 401;
    > incorrect API key;

Set new variables and use Set

set $weekend 'No'; 
    if ( $date_local ~ 'saturday|sunday' ) { 
        set $weekend 'Yes';
    } 
    location /is_weekend { return 200 $weekend; } 
    #If request URL is https://example.com/is_weekend
    #and the date is weekend page returns yes Otherwise no.
    
    location /inspect {
        return 200 "$host\n$uri\n$args"; 
        # page displays:hostname,uri,and query string($arg variable)
        return 200 "name: $arg_name"; 
        # page displays:name + query string
    }
}