Introduction to Nginx + Lua (suitable for beginners)

Source: Internet
Author: User

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 ))

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.