How to create a share variable between 2 server blocks

bouvierh nginx-forum at forum.nginx.org
Tue Apr 13 00:17:14 UTC 2021


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.

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,291224,291224#msg-291224



More information about the nginx mailing list