Friday 6 December 2019

NGINX: Conditionals support


NGINX support if conditional statement.

Example
if ($arg_secret != 1234) {
         return 401 "Unauthorized";
}

nginx.conf
events {
  
}


http {
    server {
        listen 9090;

        server_name localhost;

        if ($arg_secret != 1234) {
   return 401 "Unauthorized";
  }

        set $app 'Chat Server';
        set $version 1.23;

        location = /aboutme {
            return 200 "Application Name: $app, \nversion: $version";
        }
    }
}


Validate and reload nginx configurations.
$sudo nginx -t
Password:
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/aboutme’ in browser, you will get Unauthorized message.


Open the url 'http://localhost:9090/aboutme?secret=1234' in browser, you will get application details.


Previous                                                    Next                                                    Home

No comments:

Post a Comment