A
A
Anton B2017-04-10 14:24:20
Nginx
Anton B, 2017-04-10 14:24:20

How to prevent parallel access to the URL at the NGINX level?

Hello!
Let's say there is a location /cron/, which contains all PHP files that should be executed according to the schedule (cron).
Is it possible at the NGINX level when accessing a location to determine "if the URL is active, return the code 429"?
This is necessary to protect against running scripts in parallel.
It is relevant when there is one front-server, followed by several back-servers.
Of course, at the PHP level, I can use Redis to check/save the busy state of the script, but the Redis server is a remote storage and running the script in parallel can be faster than checking/saving the state in Redis.
Thank you!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey Sundukov, 2017-04-10
@bigton

but the Redis server is a remote storage and running the script in parallel can be faster than checking/saving the state in Redis

Not if it's done through getting a lock. Those. they didn’t start and immediately rushed to figure something out, but made a request to Redis, received permission to launch from it (received a resource lock) and only after that did they start working. After the end of the work, the script should send a request for an unlock, and ideally, it should be filed in register_shutdown_function () so that even an unplanned completion of the script would lead to the unlock of the resource. This scheme should be easy enough to file into the project, the code will have to be changed at least. In general, you need to go from the well-known scheme with locks through semaphores. And Redis handles locks correctly due to its natural single-threading.
Related : Distributed locks with Redis

E
evnuh, 2017-04-10
@evnuh

HTTP is a stateless protocol, without external storage - nothing. It could be a disk file, a radish, whatever.

N
Nurlan, 2017-04-10
@daager

php.net/manual/ru/function.flock.php a fairly simple way is to lock some file
, or to reconsider the way you start the task

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question