# Fetching HTTP value into config at start-up

**URL:** <https://www.ruby-forum.com/t/fetching-http-value-into-config-at-start-up/231881>\
**Category:** NGINX\
**Created:** [September 4, 2014, 7:07am UTC](https://www.ruby-forum.com/t/fetching-http-value-into-config-at-start-up/231881 "2014-09-04T07:07:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![addis\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c4cdca/32.png) [@addis\_a](https://www.ruby-forum.com/u/addis_a)\
**Post date:** [September 4, 2014, 7:07am UTC](https://www.ruby-forum.com/t/fetching-http-value-into-config-at-start-up/231881/1 "2014-09-04T07:07:52Z")

</div>

Hi all,

I want to read some remote values via HTTP into nginx config variables,  
just  
once at nginx startup / reload time, and pass them as fastcgi\_params  
into  
PHP scripts. I control the remote system, so I’ll know if it needs to  
re-read and can send a HUP to nginx. I don’t need it to happen every  
request  
though, because the value won’t change for long periods - but it will  
change  
sometimes, and I don’t want to have push more config out to multiple  
nginx  
servers if I can avoid it.

Is there a way to do this that doesn’t create problems?

Here’s what I’ve tried so far.

This first one doesn’t work, because the “API [is] disabled in the  
context  
of set\_by\_lua\*”:

```
set_by_lua $config_variable "
    local result = ngx.location.capture('http://url.to.fetch/')
    return result
"

```

(Same with ngx.socket.\*)

This next one works; it feels horrible to do this but if it was just  
once at  
start-up, it might be OK - but this executes for every incoming request  
(confirmed by using /bin/date as the ‘command’ and just hitting  
refresh):

```
set_by_lua $config_variable "
    local command = "/usr/bin/curl http://url.to.fetch/"
    local handle = io.popen(command)
    local result = handle:read("*a")
    handle:close()
    return result
"

```

Same for this one, it executes for every request:

```
perl_set $config_variable '
    sub {
        $var = `/bin/date`;
        return $var;
    }
'

```

Is there a good way to do this, that only executes once, and doesn’t  
have  
horrible shell interactions?

Thanks very much for your help,  
Igor

Posted at Nginx Forum:

> **[Fetching HTTP value into config at start-up](https://forum.nginx.org/read.php?2,253063,253063#msg-253063)**
>
> Hi all, I want to read some remote values via HTTP into nginx config variables, just once at nginx startup / reload time, and pass them as fastcgi\_params into PHP scripts. I control the remote system, so I'll know if it needs to re-read and can send...

---

<div class="post-metadata">

**Author:** ![igorclark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/cab0a1/32.png) [@igorclark](https://www.ruby-forum.com/u/igorclark)\
**Post date:** [September 4, 2014, 8:37am UTC](https://www.ruby-forum.com/t/fetching-http-value-into-config-at-start-up/231881/2 "2014-09-04T08:37:53Z")

</div>

Hello, igorclark

Perhahs you can create a shell to launch nginx, and before that, ejecute  
a  
shell to assign a environment variable as a result of the execution of a  
program, or create (update) a file that is to be included from nginx  
config  
file when starting

Some ideas:

[http://nginx.2469901.n2.nabble.com/Want-to-access-UNIX-environment-variable-td7584005.html](http://nginx.2469901.n2.nabble.com/Want-to-access-UNIX-environment-variable-td7584005.html)  
[https://www.google.com/search?client=ubuntu&channel=fs&q=get+environment+variable+from+nginx&ie=utf-8&oe=utf-8](https://www.google.com/search?client=ubuntu&channel=fs&q=get+environment+variable+from+nginx&ie=utf-8&oe=utf-8)

Greetings,

Oscar

---

<div class="post-metadata">

**Author:** ![igorclark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/cab0a1/32.png) [@igorclark](https://www.ruby-forum.com/u/igorclark)\
**Post date:** [September 4, 2014, 4:32pm UTC](https://www.ruby-forum.com/t/fetching-http-value-into-config-at-start-up/231881/3 "2014-09-04T16:32:47Z")

</div>

Thanks Oscar. Yes I’d read that first page and a bunch of those search  
results before. I guess I’ll end up writing custom config files per  
environment, too, and maybe a script to regenerate and send a HUP to  
nginx  
when the data changes. I really wanted to avoid this because it’s just  
one  
more moving part that I’d rather not have.

It seems pretty reasonable to want to do this sort of thing just once at  
startup. Even getting an env variable for every HTTP request seems kind  
of  
an overhead, however tiny. Maybe I’ll look at writing a module to do it  
some  
day. When I have enough time to learn how to write network-accessing  
ngx\_\*  
modules without breaking everything 😉

Anyway, if anyone knows of another way to do it, that’d be great, but in  
the  
meantime I appreciate your response. Thanks.

Best,  
Igor

Posted at Nginx Forum:

> **[Re: Fetching HTTP value into config at start-up](https://forum.nginx.org/read.php?2,253063,253073#msg-253073)**
>
> Hi all, I want to read some remote values via HTTP into nginx config variables, just once at nginx startup / reload time, and pass them as fastcgi\_params into PHP scripts. I control the remote system, so I'll know if it needs to re-read and can send...
