"Hello, world!" from Lua file?

Hello

Now that I have a working Nginx with the ngx_lua module, I’d like to
start
learning how to write web scripts.

The following page doesn’t have a basic sample:
http://wiki.nginx.org/HttpLuaModule

So I used the following…
http://yichunzhang.wordpress.com/2010/05/18/a-simple-ngx_lua-example-for-the-future/
… to edit nginx.conf and write a basic hello.lua file:

================ nginx.conf

server {
listen 12345;
server_name localhost;

    location / {
        root   html;
        index  index.html index.htm;
        content_by_lua_file hello.lua;
    }


================ html/hello.lua
print(“Hello, world!”)

But when I call http://192.168.0.10:12345/hello.lua, Chrome downloads
the
file instead of displaying the output.

What should I do?

Thank you.

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,246864,246864#msg-246864

Hello.

You need to set the content-type to this location. e.g.:

add_header Content-Type text/plain;

2014-01-27 Shohreh [email protected]

Thanks for the tip.

After editing nginx.conf thusly…


location / {
root html;
index index.html index.htm;
content_by_lua_file html/hello.lua;
add_header Content-Type text/plain;
}

… the script simply displays an empty page, with no error in
logs/error.log.

To avoid bothering you guys with newbie questions, is there a tutorial
to
get started with ngx_lua?

Thank you.

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,246864,246870#msg-246870

Hello. You must use ngx.print
http://wiki.nginx.org/HttpLuaModule#ngx.printinstead of print to
output
into the response body.

2014-01-27 Shohreh [email protected]

Yichun Z. (agentzh) Wrote:

Alternatively one can set the Content-Type response header directly in Lua
(which is more flexible):

content_by_lua '
    ngx.header["Content-Type"] = "text/plain"
    ngx.say("hello world")
';

Thanks for the tip, it works. Is there a tutorial besides the following
page
about how to write Lua scripts through ngx_lua?
http://wiki.nginx.org/HttpLuaModule

One other thing: I’d like / to map to index.html which contains a form,
and
the Lua script should be called as the action to handle the form. How
can I
do this?

This calls the script any time I hit the root directory:

location / {
root html;
index index.html index.htm;
content_by_lua_file html/hello.lua;
#add_header Content-Type text/plain;
}

Thank you.

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,246864,246907#msg-246907

Hello!

On Mon, Jan 27, 2014 at 6:00 AM, Jader H. Silva wrote:

You need to set the content-type to this location. e.g.:

add_header Content-Type text/plain;

Alternatively one can set the Content-Type response header directly in
Lua (which is more flexible):

content_by_lua '
    ngx.header["Content-Type"] = "text/plain"
    ngx.say("hello world")
';

or use the default_type directive:

default_type text/plain;
content_by_lua '
    ngx.say("hello world")
';

Regards,
-agentzh

Hello!

On Tue, Jan 28, 2014 at 3:51 AM, Shohreh wrote:

Thanks for the tip, it works. Is there a tutorial besides the following page
about how to write Lua scripts through ngx_lua?
http://wiki.nginx.org/HttpLuaModule

You can find a lot of resources on the openresty.org website:

http://openresty.org

One other thing: I’d like / to map to index.html which contains a form, and
the Lua script should be called as the action to handle the form. How can I
do this?

There’re various ways to do this.

The simplest way is to define a dedicated location (say, location =
/post) for the POST/GET target of your HTML form.

Regards,
-agentzh

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs