How to create a share variable between 2 server blocks

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


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/;

events { }

http {

    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
It seems that the test variable is reset to 1 everytime. If there a way to
make it persistent.

Posted at Nginx Forum:,291224,291224#msg-291224

More information about the nginx mailing list