P
P
pcdesign2014-07-12 11:57:06
Nginx
pcdesign, 2014-07-12 11:57:06

How to restrict access to the site on a schedule in nginx?

There is a site that should work strictly from 9-00 to 18-00.
And in the evening and at night, give a thrashing.
How it is better to implement it?
It is clear that you can slip the config into the crown at the right time and restart nginx.
But it's somehow not pretty. Yes, there are other sites out there. And I would not want to restart nginx once again.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
F
foboss, 2014-07-28
@pcdesign

This can be done very simply without any reboots.
The example below shows visitors a page from the /srv/www/maint/maintenance.html directory with a 503 code (so that search engines do not save it) if it exists and proxies traffic to the backend if it does not exist.

location / {
try_files $uri $uri/index.html $uri.html @backend;
}

location @backend {
if (-f /system/maintenance.html) {
return 503;
break;
}
proxy_pass http://backend;
}

error_page 503 /system/maintenance.html;

location = /system/maintenance.html {
root /srv/www/maint;
}

D
Dmitry Entelis, 2014-07-12
@DmitriyEntelis

Sorry
%D0%BE%D0%B1%D0%B5%D0%B4-%D0%BF%D0%B8%D0

S
Sergey, 2014-07-12
Protko @Fesor

well, not a restart, but a reload, there will be practically no downtime in this case.
But in general... You can try to write a condition in the config, saying that if the current time is greater than such and such, and less than such and such, return some status code. You have the ability to take the current time in the config (SSI) and do something. For example here .

I
Igor Vorotnev, 2014-07-13
@HeadOnFire

In general, a non-working site (when the web server does not serve it and it turns out an error and a pichalka) is not good, illogical, non-seo and generally inhuman (not user-friendly). It is much more logical to treat your visitors with due respect, and instead of magic with server configs, do a check at the site itself at the entrance. If the time is "working" - let it go to the site. Non-working - show a fig and the inscription "come in XX hours XX minutes, we work from XX to YU.

S
Stanislav Chernov, 2014-07-14
@uinx

And in my opinion, the best option is to create a cron job, create two different config files - one with working settings, and the other - redirect all requests to the default page of the Index.html type, in which to write - "sorry, go to sleep."
change config files according to the tasks and restart nginx so that it re-reads the settings.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question