M
M
Maksimkk2019-05-24 13:59:35
Nginx
Maksimkk, 2019-05-24 13:59:35

How to store a variable in Nginx?

Tell me, is it possible to make such a system using Nginx, without using Python / PHP: We save the current time in the
request . In the query , return the last saved value? So far this is the option:http://example.com/uuid/ping
http://example.com/uuid/check

location /163e065a629a40fbb1e4d6aafc29bdce/ping {
        return 200 $date_gmt;
    }

    location /163e065a629a40fbb1e4d6aafc29bdce/check {
        add_header Content-Type text/plain;
        alias /tmp/163e065a629a40fbb1e4d6aafc29bdce;
    }

It returns the current time on a ping request. And how to save the $date_gmt variable to the file /tmp/163e065a629a40fbb1e4d6aafc29bdce?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
O. J, 2019-05-24
@OrlovEvgeny

- Nginx + Lua,
lua has such a thing as lua_shared_dict, this is a hashmap that works atomically regardless of the number of workers in nginx.
rough sketch with example, above you need to define lua_shared_dict store

location /163e065a629a40fbb1e4d6aafc29bdce/ping {
        let current_date = $date_gmt
        cache:set('date', current_date, 3600)
        return 200 current_date;
    }
location /163e065a629a40fbb1e4d6aafc29bdce/check {
        add_header Content-Type text/plain;
        local last_ping = cache:get('date')
        return 200 last_ping;
    }

info here - https://github.com/openresty/lua-nginx-module#ngxs...

T
TyzhSysAdmin, 2019-05-24
@POS_troi

All variables that can be used live within the current request, the next request is the next request.
Write the simplest backend and the software does not need to screw on functions that are not characteristic of it.
Here is the simplest example of an api server that is enough for your task https://proglib.io/p/rest-api-go/ (GOLang)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question