Welcome! Log In Create A New Profile

Advanced

How to create a share variable between 2 server blocks

April 12, 2021 08:17PM
Hello!

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 http://127.0.0.1:8000 returns correctly 2.
curl http://127.0.0.1 returns 1, even after curl http://127.0.0.1:8000 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: 247
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready