nginx lua printing contents of a file as a variable ?

956 views
Skip to first unread message

Centmin George Liu

unread,
Jun 12, 2015, 11:01:23 PM6/12/15
to openre...@googlegroups.com
Hi I am new to lua and nginx and wondering if the site example below is the best way of achieving what I want to do as a learning exercise ?

I plan to use a shell script which would output a value into a file i.e. /version/latest.html which contents say v1.0.0

then when the location /check it looks for value in /version/latest.html and uses lua to print it out

site example

     # transparent non-blocking I/O in Lua via subrequests
     location /lua {
         # MIME type determined by default_type:
         default_type 'text/plain';

         content_by_lua '
             local res = ngx.location.capture("/some_other_location")
             if res.status == 200 then
                 ngx.print(res.body)
             end';
     }

so my example would be ?

     # transparent non-blocking I/O in Lua via subrequests
     location /check {
         # MIME type determined by default_type:
         default_type 'text/plain';

         content_by_lua '
             local res = ngx.location.capture("/version/latest.html")
             if res.status == 200 then
                 ngx.print(res.body)
             end';
     }

best way of doing such with lua ?

thanks

George

David Birdsong

unread,
Jun 13, 2015, 12:02:51 AM6/13/15
to openresty-en
without fully understanding your example, it seems like you could just perform a rewrite. whether in lua or not depens on whether there exists some code snippet you're not sharing or some other dynamic thing that needs to happen to populate the file.

otherwise, a bare rewrite in nginx could do the same thing. if nothing is changing, you could even do the same thing without any rewrite or lua and just set a docroot and serve the file in plain old nginx.
 
George

--
You received this message because you are subscribed to the Google Groups "openresty-en" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openresty-en...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Centmin George Liu

unread,
Jun 13, 2015, 10:31:34 AM6/13/15
to openre...@googlegroups.com
Thanks David understood what you're saying with just letting nginx handling it. Tried that lua code i mentioned and that worked too but just wondering if that's the best way to do it in nginx lua ?

Yichun Zhang (agentzh)

unread,
Jun 13, 2015, 10:49:44 PM6/13/15
to openresty-en
Hello!

On Sat, Jun 13, 2015 at 10:31 PM, Centmin George Liu wrote:
> Thanks David understood what you're saying with just letting nginx handling
> it. Tried that lua code i mentioned and that worked too but just wondering
> if that's the best way to do it in nginx lua ?
>

Nginx's rewrite (or internal redirects) is much more efficient than
subrequests, especially capturing a subrequest's response.

Regards,
-agentzh

Centmin George Liu

unread,
Jun 13, 2015, 11:49:47 PM6/13/15
to openre...@googlegroups.com
thanks agentzh :)
Reply all
Reply to author
Forward
0 new messages