Set Variable from Content of Text File?

Nick Pearson nick.pearson at gmail.com
Fri Jan 9 21:00:46 MSK 2009


Hi John,

That's an interesting problem.  There's no way that I've seen that would let
you read the contents of a file from the nginx config.  Maybe someone else
can shed some light on this.

In any case, I'm not sure you'd want the overhead of opening and reading a
file on every request.  However, you might be able to come up with a
solution using symlinks and/or a simple cron script that will make the
appropriate file available to nginx when necessary.  A command like this
might work...

if [ -s maintenance_file.txt ]; then ln -s `cat maintenance_file.txt`
maintenance.html; elif [ -f maintenance.html ]; then rm maintenance.html; fi

I tested this out locally, and it works.  If the maintenance_file.txt file
has text in it, it assumes the text is a filename and creates a symlink to
that filename (at a location where nginx would see the symlink and serve the
linked file as a maintenance page).  If the maintenance_file.txt is empty
and the symlink exists, then the symlink is deleted.  (This could be made
much more robust, but it works as is if the maintenance_file.txt is either
empty or contains a single line with no newline/return characters.)

I realize this isn't exactly what you're going for, but it might accomplish
your goal.  And ultimately, this will be more performant than having nginx
read the contents of a file on each request.

Nick


On Fri, Jan 9, 2009 at 11:12 AM, Resicow <resicow at gmail.com> wrote:

> Hi Nick,
>
> Thanks for your response... I understand the response below, but I was
> hoping for a way of doing this by reading the contents of the text file...
> So something like this...
>
>   if (-f $document_root/system/back_soon.txt) {
>
>           set $file_to_load read /$document_root/system/back_soon.txt
>           rewrite  ^(.*)$  $file_to_load last;
>           break;
>
> }
>
> So if the file exists read and set a variable based on the contents of the
> file, and then rewrite to the variable path.
>
> Thanks,
>
> John
>
>
>
>
>
> Nick Pearson wrote:
>
>> John,
>>
>> See Ezra's response in the thread here:
>> http://thread.gmane.org/gmane.comp.web.nginx.english/8978/focus=8990
>>
>> That explanation is just for a single downtime notice, but you can use a
>> variation of his solution to accomplish what you want.  For example:
>>
>> server {
>>    ...
>>    if (-f $document_root/system/back_soon.html) {
>>        rewrite  ^(.*)$  /system/back_soon.html last;
>>        break;
>>    }
>>    if (-f $document_root/system/down_for_a_while.html) {
>>        rewrite  ^(.*)$  /system/down_for_a_while.html last;
>>        break;
>>    }
>>    ...
>> }
>>
>> This will show the back_soon.html page if it exists.  If it doesn't exist
>> and the down_for_a_while.html page does, then that will be displayed.
>>  Otherwise, processing will continue normally.
>>
>>
>> On Thu, Jan 8, 2009 at 2:05 PM, Resicow <resicow at gmail.com <mailto:
>> resicow at gmail.com>> wrote:
>>
>>    Hello Nginx Community, and Happy New Year...
>>
>>    Is it possible to set the value of a variable based on the
>>    contents of a text file?
>>
>>    So if there is site downtime, I can place a file in a directory
>>    that nginx will see and will know to serve a downtime notice, but
>>    then can nginx read the contents of the file to determine which
>>    downtime notice to serve?
>>
>>    Thanks,
>>
>>    John
>>
>>
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://nginx.org/pipermail/nginx/attachments/20090109/6195709c/attachment.html>


More information about the nginx mailing list