Welcome! Log In Create A New Profile


How to create a share variable between 2 server blocks

April 12, 2021 08:17PM

On a previous post I asked how I could get give a token to my Nginx server with having to put it in plain text in the config.
One suggestion was to send to token in a request and use the javascript module to extract it.
This is what I have been trying:
load_module modules/ngx_http_js_module.so;

events { }

http {
js_include /iotedge/edge-modules/api-proxy-module/templates/hello_world.js;

proxy_buffers 32 160k;
proxy_buffer_size 160k;
proxy_read_timeout 3600;
error_log /dev/stdout info;
access_log /dev/stdout;

js_var $test;

server {
listen 80 default_server;

location / {
js_content hello;

server {
listen 8000 default_server;

location / {
js_content change;

function hello(r) {
r.return(200, r.variables['test']);

function change(r) {
r.variables['test'] = 2;
r.return(200, r.variables['test']);

curl returns correctly 2.
curl returns 1, even after curl was called.
It seems that the test variable is reset to 1 everytime. If there a way to make it persistent.
Subject Author Posted

How to create a share variable between 2 server blocks

bouvierh April 12, 2021 08:17PM

Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 71
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready