{"id":2105,"date":"2011-09-30T14:35:17","date_gmt":"2011-09-30T14:35:17","guid":{"rendered":"https:\/\/supportex.net\/?p=2105"},"modified":"2019-05-10T18:07:30","modified_gmt":"2019-05-10T16:07:30","slug":"nginx-embedding-lua-webserver","status":"publish","type":"post","link":"https:\/\/supportex.net\/blog\/2011\/09\/nginx-embedding-lua-webserver\/","title":{"rendered":"Nginx: embedding Lua into webserver"},"content":{"rendered":"<p><a href=\"http:\/\/www.lua.org\/\">Lua<\/a> is a tiny but powerful programming language. Due to its small memory footprint (about hundreds of kilobytes) it&#8217;s widely used in application which need to be extended with more complex logic. For instance, Salvatore Sanfilippo has published a &nbsp;<a href=\"http:\/\/antirez.com\/post\/scripting-branch-released.html\">post<\/a> describing how you can use power of Lua with Redis. It also applied to nginx, you can use Lua in its configuration files to get them more flexible. Sometimes it can be handy. Here is what you need to do to get nginx with Lua in core. We will use<a href=\"https:\/\/github.com\/chaoslawful\/lua-nginx-module\"> Lua module <\/a>for nginx. At the end we will have nginx rpm package with Lua support. I assume that you already have rpmbuild environment and use Fedora 15. You also need to have Lua installed. Firstly <a href=\"https:\/\/github.com\/chaoslawful\/lua-nginx-module\/tarball\/master\">download<\/a> and unpack <em>ngx_lua <\/em>and to your SOURCES. You will also need <a href=\"https:\/\/github.com\/simpl-it\/ngx_devel_kit\/tarball\/master\">ngx_devel_kit<\/a>, it should be unpacked to SOURCES as well. After that you need to download srpm of nginx and install it.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\">yumdownloader --source nginx\nrpm -ivh nginx-1.0.5-1.fc15.src.rpm<\/pre>\n<p>Besides you need to <a href=\"http:\/\/nginx.org\/download\/nginx-1.0.6.tar.gz\">download<\/a> nginx tarboll with 1.0.6 version and put it &nbsp;to SOURCES. Change Version line to &#8220;1.0.6&#8221; and add next line to the configure section:<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\">--add-module=\/root\/rpmbuild\/SOURCES\/simpl-ngx_devel_kit-bc97eea\/<\/code><br \/>\n<code class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\">--add-module=\/root\/rpmbuild\/SOURCES\/chaoslawful-lua-nginx-module-752be5d\/<\/code><\/p>\n<p>Now we are ready to build rpm.<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\">rpmbuild -bb ~\/rpmbuild\/SPECS\/nginx.spec<\/code><\/p>\n<p>If there are &nbsp;no errors you should get nginx rpm package with Lua support:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\">[root@playground ~]# ls ~\/rpmbuild\/RPMS\/x86_64\/nginx-1.0.6-1.fc15.x86_64.rpm\n\/root\/rpmbuild\/RPMS\/x86_64\/nginx-1.0.6-1.fc15.x86_64.rpm\n[root@playground ~]#<\/pre>\n<p>Let&#8217;s install it and test.<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\">rpm -ivh ~\/rpmbuild\/RPMS\/x86_64\/nginx-1.0.6-1.fc15.x86_64.rpm<\/code><\/p>\n<p>As you can see nginx was built with Lua:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\">[root@playground ~]# ldd \/usr\/sbin\/nginx | grep lua\nliblua-5.1.so =&gt; \/usr\/lib64\/liblua-5.1.so (0x0000003331600000)\n[root@playground ~]#<\/pre>\n<p>Add next section to nginx configuration file (thats is a slightly modified example from the documentation):<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\">location \/foo {\nset $diff ''; # we have to predefine the $diff variable here\nset_by_lua $sum '\nlocal a = 32\nlocal b = 56\nngx.var.diff = a - b; -- write to $diff directly\nreturn a + b; -- return the $sum value normally\n';\nngx.print(ngx.var.sum); ngx.say(ngx.var.diff);';\n}<\/pre>\n<p>Start nginx and make a request:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\">[root@playground ~]# \/etc\/init.d\/nginx start ; curl localhost\/foo\nRestarting nginx (via systemctl): [ OK ]\n88-24\n[root@playground ~]#<\/pre>\n<p>Lua is working! More information on ngx_lua module can be found on <a href=\"https:\/\/github.com\/chaoslawful\/lua-nginx-module\">github page<\/a> or <a href=\"http:\/\/wiki.nginx.org\/HttpLuaModule#Nginx_API_for_Lua\">nginx wiki<\/a>. &nbsp;To get more information about Lua you can refer to&nbsp;<a href=\"http:\/\/www.lua.org\/manual\/5.1\/\">Lua 5.1 Reference Manual<\/a>.<br \/>\n<strong>Upd.<\/strong> Luckily for Ubuntu users, you can just install <em>nginx-extras<\/em> package to get nginx with Lua. Actually this package also contains modules nginx-echo,&nbsp; nginx-push-0.692 and nginx-upstream-fair.<\/p>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 1046px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\">&nbsp;<\/div>\n<p><strong>Didn&#8217;t find the answer to your question? <a href=\"\/contacts\/\">Ask it<\/a> our administrators to reply we will publish on website.<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Lua is a tiny but powerful programming language. Due to its small memory footprint (about hundreds of kilobytes) it&#8217;s widely used in application which need to be extended with more complex logic. For instance, Salvatore Sanfilippo has published a &nbsp;post describing how you can use power of Lua with Redis. It also applied to nginx,&hellip; <\/p>\n<div class=\"readmore-wrapper\"><a href=\"https:\/\/supportex.net\/blog\/2011\/09\/nginx-embedding-lua-webserver\/\" class=\"more-link\">Read <\/a><\/div>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[152],"tags":[275,245,246,247],"class_list":["post-2105","post","type-post","status-publish","format-standard","hentry","category-linux","tag-linux","tag-lua-en","tag-nginx-en","tag-ubuntu-en"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Nginx: embedding Lua into webserver &#8211; Supportex.NET blog<\/title>\n<meta name=\"description\" content=\"How to embedding Lua into Nginx webserver\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/supportex.net\/blog\/2011\/09\/nginx-embedding-lua-webserver\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Nginx: embedding Lua into webserver &#8211; Supportex.NET blog\" \/>\n<meta property=\"og:description\" content=\"How to embedding Lua into Nginx webserver\" \/>\n<meta property=\"og:url\" content=\"https:\/\/supportex.net\/blog\/2011\/09\/nginx-embedding-lua-webserver\/\" \/>\n<meta property=\"og:site_name\" content=\"Supportex.NET blog\" \/>\n<meta property=\"article:published_time\" content=\"2011-09-30T14:35:17+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-05-10T16:07:30+00:00\" \/>\n<meta name=\"author\" content=\"Oleksii Tykhonov\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Oleksii Tykhonov\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/supportex.net\/blog\/2011\/09\/nginx-embedding-lua-webserver\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/supportex.net\/blog\/2011\/09\/nginx-embedding-lua-webserver\/\"},\"author\":{\"name\":\"Oleksii Tykhonov\",\"@id\":\"https:\/\/supportex.net\/blog\/en\/#\/schema\/person\/0690c26a0266603129fc15eae6243251\"},\"headline\":\"Nginx: embedding Lua into webserver\",\"datePublished\":\"2011-09-30T14:35:17+00:00\",\"dateModified\":\"2019-05-10T16:07:30+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/supportex.net\/blog\/2011\/09\/nginx-embedding-lua-webserver\/\"},\"wordCount\":334,\"commentCount\":0,\"keywords\":[\"linux\",\"Lua\",\"nginx\",\"Ubuntu\"],\"articleSection\":[\"linux\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/supportex.net\/blog\/2011\/09\/nginx-embedding-lua-webserver\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/supportex.net\/blog\/2011\/09\/nginx-embedding-lua-webserver\/\",\"url\":\"https:\/\/supportex.net\/blog\/2011\/09\/nginx-embedding-lua-webserver\/\",\"name\":\"Nginx: embedding Lua into webserver &#8211; Supportex.NET blog\",\"isPartOf\":{\"@id\":\"https:\/\/supportex.net\/blog\/en\/#website\"},\"datePublished\":\"2011-09-30T14:35:17+00:00\",\"dateModified\":\"2019-05-10T16:07:30+00:00\",\"author\":{\"@id\":\"https:\/\/supportex.net\/blog\/en\/#\/schema\/person\/0690c26a0266603129fc15eae6243251\"},\"description\":\"How to embedding Lua into Nginx webserver\",\"breadcrumb\":{\"@id\":\"https:\/\/supportex.net\/blog\/2011\/09\/nginx-embedding-lua-webserver\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/supportex.net\/blog\/2011\/09\/nginx-embedding-lua-webserver\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/supportex.net\/blog\/2011\/09\/nginx-embedding-lua-webserver\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/supportex.net\/blog\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Nginx: embedding Lua into webserver\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/supportex.net\/blog\/en\/#website\",\"url\":\"https:\/\/supportex.net\/blog\/en\/\",\"name\":\"Supportex.NET blog\",\"description\":\"Server and network management company\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/supportex.net\/blog\/en\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/supportex.net\/blog\/en\/#\/schema\/person\/0690c26a0266603129fc15eae6243251\",\"name\":\"Oleksii Tykhonov\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/supportex.net\/blog\/en\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/4fd5f58002717075c88963469b9babef?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/4fd5f58002717075c88963469b9babef?s=96&d=mm&r=g\",\"caption\":\"Oleksii Tykhonov\"},\"url\":\"https:\/\/supportex.net\/blog\/author\/oleksiitykhonov\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Nginx: embedding Lua into webserver &#8211; Supportex.NET blog","description":"How to embedding Lua into Nginx webserver","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/supportex.net\/blog\/2011\/09\/nginx-embedding-lua-webserver\/","og_locale":"en_US","og_type":"article","og_title":"Nginx: embedding Lua into webserver &#8211; Supportex.NET blog","og_description":"How to embedding Lua into Nginx webserver","og_url":"https:\/\/supportex.net\/blog\/2011\/09\/nginx-embedding-lua-webserver\/","og_site_name":"Supportex.NET blog","article_published_time":"2011-09-30T14:35:17+00:00","article_modified_time":"2019-05-10T16:07:30+00:00","author":"Oleksii Tykhonov","twitter_misc":{"Written by":"Oleksii Tykhonov","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/supportex.net\/blog\/2011\/09\/nginx-embedding-lua-webserver\/#article","isPartOf":{"@id":"https:\/\/supportex.net\/blog\/2011\/09\/nginx-embedding-lua-webserver\/"},"author":{"name":"Oleksii Tykhonov","@id":"https:\/\/supportex.net\/blog\/en\/#\/schema\/person\/0690c26a0266603129fc15eae6243251"},"headline":"Nginx: embedding Lua into webserver","datePublished":"2011-09-30T14:35:17+00:00","dateModified":"2019-05-10T16:07:30+00:00","mainEntityOfPage":{"@id":"https:\/\/supportex.net\/blog\/2011\/09\/nginx-embedding-lua-webserver\/"},"wordCount":334,"commentCount":0,"keywords":["linux","Lua","nginx","Ubuntu"],"articleSection":["linux"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/supportex.net\/blog\/2011\/09\/nginx-embedding-lua-webserver\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/supportex.net\/blog\/2011\/09\/nginx-embedding-lua-webserver\/","url":"https:\/\/supportex.net\/blog\/2011\/09\/nginx-embedding-lua-webserver\/","name":"Nginx: embedding Lua into webserver &#8211; Supportex.NET blog","isPartOf":{"@id":"https:\/\/supportex.net\/blog\/en\/#website"},"datePublished":"2011-09-30T14:35:17+00:00","dateModified":"2019-05-10T16:07:30+00:00","author":{"@id":"https:\/\/supportex.net\/blog\/en\/#\/schema\/person\/0690c26a0266603129fc15eae6243251"},"description":"How to embedding Lua into Nginx webserver","breadcrumb":{"@id":"https:\/\/supportex.net\/blog\/2011\/09\/nginx-embedding-lua-webserver\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/supportex.net\/blog\/2011\/09\/nginx-embedding-lua-webserver\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/supportex.net\/blog\/2011\/09\/nginx-embedding-lua-webserver\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/supportex.net\/blog\/en\/"},{"@type":"ListItem","position":2,"name":"Nginx: embedding Lua into webserver"}]},{"@type":"WebSite","@id":"https:\/\/supportex.net\/blog\/en\/#website","url":"https:\/\/supportex.net\/blog\/en\/","name":"Supportex.NET blog","description":"Server and network management company","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/supportex.net\/blog\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/supportex.net\/blog\/en\/#\/schema\/person\/0690c26a0266603129fc15eae6243251","name":"Oleksii Tykhonov","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/supportex.net\/blog\/en\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/4fd5f58002717075c88963469b9babef?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4fd5f58002717075c88963469b9babef?s=96&d=mm&r=g","caption":"Oleksii Tykhonov"},"url":"https:\/\/supportex.net\/blog\/author\/oleksiitykhonov\/"}]}},"_links":{"self":[{"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/posts\/2105","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/comments?post=2105"}],"version-history":[{"count":2,"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/posts\/2105\/revisions"}],"predecessor-version":[{"id":3176,"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/posts\/2105\/revisions\/3176"}],"wp:attachment":[{"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/media?parent=2105"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/categories?post=2105"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/tags?post=2105"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}