Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Lua under nginx (writing a website with Lua)

Tags:

nginx

cgi

lua

As a learning exercise I've dedicated some time to picking up Lua by creating some basic apps. I've gotten it installed and running great on Natty/Ubuntu, however, I'm a bit lost as to how to get it to play nice with nginx.

I've read a bit here http://wiki.nginx.org/HttpLuaModule#Installation And cloned this repo https://github.com/chaoslawful/lua-nginx-module into my /etc/nginx folder...

However, I'm still rather lost and unsure how to get it working even on a basic level. Is it possible to just include something into my nginx.conf file to handle /lua requests, or do I need to recompile/reinstall nginx altogether? (i'd rather avoid this).

I've already been using php under nginx via fpm for quite a while, but I'm really not sure where to start getting Lua working in a similar fashion.

like image 299
Jonathan Coe Avatar asked Feb 24 '12 16:02

Jonathan Coe


People also ask

Does Nginx support Lua?

OpenResty. Embed Lua programming language into NGINX Plus to enable the integration of the powerful Lua threads (Lua coroutines) into the NGINX Plus event‑processing model. Support details: Supported by NGINX for active NGINX Plus subscribers.

What is OpenResty Nginx?

OpenResty is a web server which extends Nginx by bundling it with many useful Nginx modules and Lua libraries. OpenResty excels at scaling web applications and services.

How do I start OpenResty?

To start your OpenResty®, you can just use the openresty command in place of your original nginx command as long as you have correctly added the <openresty-prefix>/bin/ directory to your system environment PATH ( <openresty-prefix> is default to /usr/local/openresty/ unless being overridden by ./configure 's --prefix= ...


3 Answers

The ngx_lua module is for running Lua code directly in the nginx webserver. It is possible to run entire Lua applications in this way but this is not the specific target of that module. Actually, some of the module directives specifically should not be used with long running or complex routines.

You will need to recompile Nginx with this module as you cannot just download an Nginx module and use it like that.

To run Lua applications similar to the way you run PHP, you can configure nginx to pass ".lua" requests to a Lua handler (Similar to PHP).

  1. You can set up a webserver such as the Lua webserver, xavante or thttpd or even Apache and "proxy_pass" to this similarly to how many do with Apache for PHP.

  2. You can set Lua up to run as CGI (similar to PHP with FastCGI although Lua does not have the equivalent of FPM) and call this as needed.

You do not need ngx_lua for either of the two options.

Basically, PHP, Lua and such fall under the broad category of "CGI" scripts and any "how to" on running these can be applied to Lua.

BTW openresty is just regular Nginx with some 3rd party modules bundled in including ngx_lua and the people behind openresty are the same behind ngx_lua.

You can manually add as many of the same bundled modules to Nginx yourself as you wish.

like image 103
Dayo Avatar answered Oct 25 '22 02:10

Dayo


I haven't tried with the official nginx distribution, but it was easy enough with OpenResty (http://openresty.org/)

See the "getting started" page for a simple nginx.conf to test it.

like image 35
finnw Avatar answered Oct 25 '22 02:10

finnw


In ubuntu you can use lua module for nginx by simply installing nginx-extras.

like image 36
timurb Avatar answered Oct 25 '22 03:10

timurb