Äú¿ÉÒÔ¾èÖú£¬Ö§³ÖÎÒÃǵĹ«ÒæÊÂÒµ¡£

1Ôª 10Ôª 50Ôª





ÈÏÖ¤Â룺  ÑéÖ¤Âë,¿´²»Çå³þ?Çëµã»÷Ë¢ÐÂÑéÖ¤Âë ±ØÌî



  ÇóÖª ÎÄÕ ÎÄ¿â Lib ÊÓÆµ iPerson ¿Î³Ì ÈÏÖ¤ ×Éѯ ¹¤¾ß ½²×ù Model Center   Code  
»áÔ±   
   
 
     
   
 ¶©ÔÄ
  ¾èÖú
NGINX + LUAʵÏÖ¸´ÔӵĿØÖÆ
 
×÷Õߣº ÕçÂëÅ©
  1942  次浏览      27
 2019-10-28
 
±à¼­ÍƼö:
ÎÄÕ¼òµ¥µÄ˵Ã÷°²×°lua_nginx_module Ä£¿é,lua µ÷ÓÃmysqlµÄÀý×Ó,º¯Êý°æµÄ·ÃÎÊ,Ï£Íû¶ÔÄúÓÐËù°ïÖú
±¾ÎÄÀ´×ÔÓÚOutOfMemor£¬ÓÉ»ðÁú¹ûÈí¼þç÷ç÷±à¼­ÍƼö

°²×°lua_nginx_module Ä£¿é

lua_nginx_module ¿ÉÒÔÒ»²½²½µÄ°²×°£¬Ò²¿ÉÒÔÖ±½ÓÓÃÌÔ±¦µÄOpenResty

CentosºÍdebianµÄ°²×°¾Í¼òµ¥ÁË¡£¡£

ÕâÀï˵ÏÂfreebsdµÄ°²×°£º

fetch http://www.lua.org/ftp/lua-5.1.4.tar.gz
tar zxvf lua-5.1.4.tar.gz
cd lua-5.1.4
make freebsd
make install
cd ..
fetch https://github.com/chaoslawful/lua-nginx-module/zipball/v0.1.6rc2
fetch https://github.com/simpl/ngx_devel_kit/zipball/v0.2.17rc2
tar zxvf v0.1.6rc2
mv chaoslawful-lua-nginx-module-ccaf132 lua_nginx_module
tar zxvf v0.2.17rc2
mv simpl-ngx_devel_kit-bc97eea ngx_devel_kit
tar zxvf pcre-8.12.tar.gz
tar zxvf nginx-1.0.3.tar.gz
cd nginx-1.0.3
./configure --prefix=/data/soft/nginx --with-pcre=../pcre-8.12 --add-module=../ngx_devel_kit --add-module=../lua_nginx_module
make && make install

°²×°Íê³Éºó£¬ÎÒÃÇÌåÑéÒ»ÏÂlua

µÚÒ»¸ölua½Å±¾

ngx.say ÊÇ´òÓ¡µÄ´òÓ¡Êä³öµÄÒâ˼¡£¡£¡£

location /echo {
default_type text/plain;
echo hello lua;
}
location /lua {
default_type text/plain;
content_by_lua 'ngx.say("hello world")';
}

ÓÃlua½Å±¾×önginxµÄ·ÃÎʵÄÏÞÖÆ...

location @client{
proxy_pass http://www.ruifengyun.com;
}
location ~ /test {
default_type text/html;
content_by_lua 'ngx.say("this is ruifengyun.com!")';
access_by_lua '
if ngx.var.remote_addr == "10.2.20.110" then
ngx.exit(ngx.HTTP_FORBIDDEN)
end
if ngx.var.remote_addr == "10.2.20.112" then
ngx.exec("@client")
end
';
}

¿ØÖƾ­¹ýÅжÏÖ®ºó£¬²ÅÄÜ·ÃÎÊ

location / {
access_by_lua '
local res = ngx.location.capture("/auth")
if res.status == ngx.HTTP_OK then
return
end
if res.status == ngx.HTTP_FORBIDDEN then
ngx.exit(res.status)
end
ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
';
# proxy_pass/fastcgi_pass/postgres_pass/...
}

ʹÓÃlua×önginxµÄrewriteÌø×ª

Õâ¸öÊÇÏÈÅÐ¶Ï check-pam½Ó¿ÚµÄreturnµÄÄÚÈÝÊDz»ÊÇspam£¬Êǵϰ£¬×ªÌøµ½ÆäËûµÄÒ³Ãæ

location / {
rewrite_by_lua '
local res = ngx.location.capture("/check-spam")
if res.body == "spam" then
ngx.redirect("/terms-of-use.html")
end
'; fastcgi_pass ...;
}

¸ù¾Ýip×ö²»Í¬µÄÏìÓ¦

location / {
content_by_lua '
myIP = ngx.req.get_headers()["X-Real-IP"]
if myIP == nil then
myIP = ngx.req.get_headers()["x_forwarded_for"]
end
if myIP == nil then
myIP = ngx.var.remote_addr
end
if myIP == "" then
ngx.exec("@client")
else
ngx.exec("@client_test")
end
';
}

redirectµÄʹÓÃ

return ngx.redirect("/foo")
return ngx.redirect("http://localhost:1984/foo", ngx.HTTP_MOVED_TEMPORARILY)
return ngx.redirect("/foo", 301)

·µ»Ø302ÁÙÊ±ÖØ¶¨Ïò µØÖ·À¸»áÏÔÊ¾Ìø×ªºóµÄµØÖ·

rewrite ^ /foo? redirect; # nginx config
return ngx.redirect('/foo'); -- lua code

lua¹ýÂËpost¹ýÀ´µÄ²ÎÊý

location = /test {
content_by_lua '
ngx.req.read_body()
local args = ngx.req.get_post_args()
for key, val in pairs(args) do
if type(val) == "table" then
ngx.say(key, ": ", table.concat(val, ", "))
else
ngx.say(key, ": ", val)
end
end
';
}

Ò»¸öLuaµÄÀý×Ó£º

#!/usr/bin/env lua
ngx.say('aaaaaa </br>')
local url = ngx.var.uri
ngx.say('<br>',url,'<br/>')
ngx.print('Õâ´Î·ÃÎʵÄheaderÍ·ÊÇ ',ngx.req.raw_header())
ngx.print('<meta http-equiv="content-type" content="text/html;charset=utf-8">')
ngx.print('<h1> Õâ¸öÊÇ h1 </h1>')
ngx.print('Õâ´Î·ÃÎʵÄÊÇ get »¹ÊÇ post ѽ ',ngx.req.get_Method())
local args = ngx.req.get_uri_args()
ngx.print(args)
local res = ngx.location.capture("/")
ngx.print('<br>http code <br>¡®,res.status)

lua µ÷ÓÃmysqlµÄÀý×Ó

worker_processes 2;
error_log logs/error.log warn;
events {
worker_connections 1024;
}
http {
upstream backend {
drizzle_server 127.0.0.1:3306 protocol=mysql
dbname=ngx_test user=ngx_test password=ngx_test;
drizzle_keepalive max=10 overflow=ignore mode=single;
}
server {
listen 8080;
location @cats-by-name {
set_unescape_uri $name $arg_name;
set_quote_sql_str $name;
drizzle_query 'select * from cats where name=$name';
drizzle_pass backend;
rds_json on;
}
location @cats-by-id {
set_quote_sql_str $id $arg_id;
drizzle_query 'select * from cats where id=$id';
drizzle_pass backend;
rds_json on;
}
location = /cats {
access_by_lua '
if ngx.var.arg_name then
return ngx.exec("@cats-by-name")
end
if ngx.var.arg_id then
return ngx.exec("@cats-by-id")
end
';
rds_json_ret 400 "expecting \"name\" or \"id\" query arguments";
}
}
}

¸Ä¸ÄÃÜÂë¾ÍÄÜÓÃÀ²~

lua»ñÈ¡urlÖеIJÎÊý

location = /adder {
set_by_lua $res "
local a = tonumber(ngx.arg[1])
local b = tonumber(ngx.arg[2])
return a + b" $arg_a $arg_b;

echo $res;
}

ngx.req.set_uri

nginxÀïÃæµÄÅäÖÃÊÇ£º

location /test {
rewrite ^/test/(.*) /$1 break;
proxy_pass http://my_backend;
}

luaÀïÃæµÄÅäÖÃÊÇ£º

location /test {
rewrite_by_lua '
local uri = ngx.re.sub(ngx.var.uri, "^/test/(.*)", "$1", "o")
ngx.req.set_uri(uri)
';
proxy_pass http://my_backend;
}

ÎÒÏë´ó¼Ò¿´Õâ¸ö¶ÔÕÕ£¬ÒѾ­ÖªµÀÊÇɶÒâ˼ÁË.

ͨ¹ýlua»ñÈ¡nginxµÄÄÚÖñäÁ¿£¬Í¨¹ýÕâЩ±äÁ¿×öЩÂß¼­µÄ´¦Àí~

NginxÌṩÁ˺ܶàÄÚÖõıäÁ¿,Èç:

$arg_PARAMETER Õâ¸ö±äÁ¿°üº¬ÔÚ²éѯ×Ö·û´®Ê±GETÇëÇóPARAMETERµÄÖµ¡£

$args Õâ¸ö±äÁ¿µÈÓÚÇëÇóÐÐÖеIJÎÊý¡£

$binary_remote_addr ¶þ½øÖÆÂëÐÎʽµÄ¿Í»§¶ËµØÖ·¡£

$body_bytes_sent ´«ËÍÒ³ÃæµÄ×Ö½ÚÊý

$content_length ÇëÇóÍ·ÖеÄContent-length×ֶΡ£

$content_type ÇëÇóÍ·ÖеÄContent-Type×ֶΡ£

$cookie_COOKIE cookie COOKIE掙歜

$document_root µ±Ç°ÇëÇóÔÚrootÖ¸ÁîÖÐÖ¸¶¨µÄÖµ¡£

$document_uri Óë$uriÏàͬ¡£

$host ÇëÇóÖеÄÖ÷»úÍ·×ֶΣ¬Èç¹ûÇëÇóÖеÄÖ÷»úÍ·²»¿ÉÓã¬ÔòΪ·þÎñÆ÷´¦ÀíÇëÇóµÄ·þÎñÆ÷Ãû³Æ¡£

$is_args Èç¹û$argsÉèÖã¬ÖµÎª"?"£¬·ñÔòΪ""¡£

$limit_rate Õâ¸ö±äÁ¿¿ÉÒÔÏÞÖÆÁ¬½ÓËÙÂÊ¡£

$nginx_version µ±Ç°ÔËÐеÄnginx°æ±¾ºÅ¡£

$query_string Óë$argsÏàͬ¡£

$remote_addr ¿Í»§¶ËµÄIPµØÖ·¡£

$remote_port ¿Í»§¶ËµÄ¶Ë¿Ú¡£

$remote_user ÒѾ­¾­¹ýAuth Basic ModuleÑéÖ¤µÄÓû§Ãû¡£

$request_filename µ±Ç°Á¬½ÓÇëÇóµÄÎļþ·¾¶£¬ÓÉroot»òaliasÖ¸ÁîÓëURIÇëÇóÉú³É¡£

$request_body Õâ¸ö±äÁ¿£¨0.7.58+£©°üº¬ÇëÇóµÄÖ÷ÒªÐÅÏ¢¡£ÔÚʹÓÃproxy_pass»òfastcgi_passÖ¸ÁîµÄlocationÖбȽÏÓÐÒâÒå¡£

$request_body_file ¿Í»§¶ËÇëÇóÖ÷ÌåÐÅÏ¢µÄÁÙʱÎļþÃû¡£

$request_completion δ֪¡£

$request_method Õâ¸ö±äÁ¿Êǿͻ§¶ËÇëÇóµÄ¶¯×÷£¬Í¨³£ÎªGET»òPOST¡£°üÀ¨0.8.20¼°Ö®Ç°µÄ°æ±¾ÖУ¬Õâ¸ö±äÁ¿×ÜΪmain requestÖе͝×÷£¬Èç¹ûµ±Ç°ÇëÇóÊÇÒ»¸ö×ÓÇëÇ󣬲¢²»Ê¹ÓÃÕâ¸öµ±Ç°ÇëÇóµÄ¶¯×÷¡£

$request_uri Õâ¸ö±äÁ¿µÈÓÚ°üº¬Ò»Ð©¿Í»§¶ËÇëÇó²ÎÊýµÄԭʼURI£¬ËüÎÞ·¨Ð޸ģ¬Çë²é¿´$uri¸ü¸Ä»òÖØÐ´URI¡£

$scheme ËùÓõÄЭÒ飬±ÈÈçhttp»òÕßÊÇhttps£¬±ÈÈçrewrite ^(.+)$ $scheme://example.com$1 redirect;

$server_addr ·þÎñÆ÷µØÖ·£¬ÔÚÍê³ÉÒ»´Îϵͳµ÷Óúó¿ÉÒÔÈ·¶¨Õâ¸öÖµ£¬Èç¹ûÒªÈÆ¿ªÏµÍ³µ÷Óã¬Ôò±ØÐëÔÚlistenÖÐÖ¸¶¨µØÖ·²¢ÇÒʹÓÃbind²ÎÊý¡£

$server_name ·þÎñÆ÷Ãû³Æ¡£

$server_port ÇëÇóµ½´ï·þÎñÆ÷µÄ¶Ë¿ÚºÅ¡£

$server_protocol ÇëÇóʹÓõÄЭÒ飬ͨ³£ÊÇHTTP/1.0»òHTTP/1.1¡£

$uri ÇëÇóÖеĵ±Ç°URI(²»´øÇëÇó²ÎÊý£¬²ÎÊýλÓÚ$args)£¬¿ÉÒÔ²»Í¬ÓÚä¯ÀÀÆ÷´«µÝµÄ

$request_uriµÄÖµ£¬Ëü¿ÉÒÔͨ¹ýÄÚ²¿Öض¨Ïò£¬»òÕßʹÓÃindexÖ¸Áî½øÐÐÐ޸ġ£

ÁíÍ⣺ HTTP_X_FORWARDED_FORÊÇ͸¹ý´úÀí·þÎñÆ÷È¡µÃ¿Í»§¶ËµÄÕæÊµIPµØÖ·£¬ÓÐЩÓô˷½·¨¶ÁÈ¡µ½µÄÈÔÈ»ÊÇ´úÀí·þÎñÆ÷µÄIP¡£»¹ÓÐÒ»µãÐèҪעÒâµÄÊÇ£ºÈç¹û¿Í»§¶ËûÓÐͨ¹ý´úÀí·þÎñÆ÷À´·ÃÎÊ£¬ÄÇôÓà HTTP_X_FORWARDED_FOR È¡µ½µÄÖµ½«Êǿյġ£

º¯Êý°æµÄ·ÃÎÊ

location /lua1 {
default_type 'text/plain';
content_by_lua 'ngx.say("hello, lua")';
}
# ÇëÇóÁíÍâµÄurl
location /lua2 {
content_by_lua '
¡¡¡¡local res = ngx.location.capture("/hello1")
¡¡¡¡ngx.say("data: " .. res.body)
';
}
   
1942 ´Îä¯ÀÀ       27
????

HTTP????
nginx??????
SD-WAN???
5G?????
 
????

??????????
IPv6???????
??????????
???????
????

????????
????????
???????????????
??????????