Win download address: http://code.google.com/p/luaforwindows/
Hello Lua
Nginx uses content_by_lua and content_by_lua_file to embed the lua script.
Content_by_lua
Modify nginx configuration file nginx. conf
Location/hellolua {
Content_by_lua'
Ngx. header. content_type = "text/html ";
Ngx. say ("Hello Lua .");
';
}
Restart nginx to access http: // localhost // hellolua. You can see Hello Lua.
Content_by_lua_file
Location/demo {
Lua_code_cache off;
Content_by_lua_file lua_script/demo. lua;
}
Lua_code_cache indicates that the cache is disabled. When the cache is disabled, you do not need to restart nginx to modify the lua script. Content_by_lua_file specifies the script path. This is the relative path, relative to the nginx root directory, and then write the following lua script
-- Filename: demo. lua
Ngx. header. content_type = "text/html"
Ngx. say ("Hello Lua Demo .")
Restart Nginx and turn off lua_code_cache. Then nginx will have an alert.
Nginx: [alert] lua_code_cache is off; this will hurt performance in./conf/nginx. conf: 56
Visit http: // localhost/demo to see Hello Lua Demo.
Obtain common Nginx parameters
Ngx. header. content_type = "text/html"
Ngx. header. PowerBy = "Lua"
-- Request header table
For k, v in pairs (ngx. req. get_headers () do
Ngx. say (k, ":", v)
End
-- Request methods such as GET and POST
Ngx. say ("METHOD:"... ngx. var. request_method)
-- GET parameters
For k, v in pairs (ngx. req. get_uri_args () do
Ngx. say (k, ":", v)
End
-- Get POST parameters
Ngx. req. read_body ()
For k, v in pairs (ngx. req. get_post_args () do
Ngx. say (k, ":", v)
End
-- HTTP version
Ngx. say (ngx. req. http_version ())
-- Unparsed request header string
Ngx. say (ngx. req. raw_header ())
-- Unparsed BODY string
Ngx. print (ngx. req. get_body_data ())
-- Ngx. exit (400)
-- Ngx. redirect ("/", 200)
MD5 example
The following is a small example to generate the md5 value of the string.
Ngx. header. content_type = "text/html"
Local resty_md5 = require "resty. md5"
Local md5 = resty_md5: new ()
Local s = "Hello Lua ."
Md5: update (s)
Local str = require "resty. string"
Ngx. say (str. to_hex (md5: final ()))
Ngx. say (ngx. md5 (s ))