[TOC]
Implementation of middleware with nginx openresty
Be careful
In the formal ngx.exec(), you must not output anything, such as ngx.say("debug"), otherwise you will be trapped in redirection
The log will constantly prompt attempt to call ngx.exec after sending out response headers
This should be a common sense problem. We all know that we can't output anything in the header. I forgot,,, and struggled for a long time
install
- Mac
Default installation directory / usr / local / cell / openresty / 1.15.8.2
brew install openresty/brew/openresty
If you encounter the following error message on your Mac
make[1]: *** [lj_folddef.h] Segmentation fault: 11 make[1]: *** Deleting file `lj_folddef.h' make[1]: *** Waiting for unfinished jobs.... make: *** [default] Error 2 ERROR: failed to run command: gmake -j8 TARGET_STRIP=@: CCDEBUG=-g XCFLAGS='-msse4.2 -DLUAJIT_NUMMODE=2 -DLUAJIT_ENABLE_LUA52COMPAT' CC=cc PREFIX=/usr/local/Cellar/openresty/1.15.8.2/luajit If reporting this issue please do so at (not Homebrew/brew or Homebrew/core): https://github.com/openresty/homebrew-brew/issues These open issues may also help: The openresty-debug package should use openresty-openssl-debug instead https://github.com/openresty/homebrew-brew/issues/3 Fails to install OpenResty https://github.com/openresty/homebrew-brew/issues/5 Can't install openresty on macOS 10.15 https://github.com/openresty/homebrew-brew/issues/10 Error: A newer Command Line Tools release is available. Update them from Software Update in System Preferences or https://developer.apple.com/download/more/.
To this place, please Download the development plug-in of apple official website Just download the latest Xcode command line tools. I'm confused. I can't download them all night. I thought there was a problem with the official resources, but it was Xunlei's problem
Some people say that brew install *gcc * directly, but I think it's too rude. I didn't do it. If you want to save time, you can try it
- Ubuntu
Default installation directory / usr/local/openresty/nginx/
sudo apt install -y libpcre3-dev libssl-dev perl make build-essential curl\ && sudo apt-get -y install --no-install-recommends wget gnupg ca-certificates\ && wget -O - https://openresty.org/package/pubkey.gpg | sudo apt-key add -\ && sudo apt-get -y install --no-install-recommends software-properties-common\ && sudo add-apt-repository -y "deb http://openresty.org/package/ubuntu $(lsb_release -sc) main"\ && sudo apt-get update\ && sudo apt-get -y install openresty
NGINX configuration
location ~ /lua_request { #default_type 'text/json'; default_type 'text/html'; #content_by_lua_file '/Users/liuhao/my-shell/lua/index.lua'; content_by_lua_file '/Users/liuhao/my-shell/lua/middle.lua'; } # Forward to / / 192.168.56.100 location @server_100 { proxy_pass http://192.168.56.100:80; # The IP information of users will be brought in at the same time. Otherwise, the IP address obtained by the back-end server will be the IP address of the proxy server; proxy_set_header X-Forwarded_For $remote_addr; } # Forward to / / 192.168.56.101 location @server_101 { proxy_pass http://192.168.56.101:80; # The IP information of users will be brought in at the same time. Otherwise, the IP address obtained by the back-end server will be the IP address of the proxy server; proxy_set_header X-Forwarded_For $remote_addr; } # Forward to / / 192.168.56.102 location @server_102 { proxy_pass http://192.168.56.102:80; # The IP information of users will be brought in at the same time. Otherwise, the IP address obtained by the back-end server will be the IP address of the proxy server; proxy_set_header X-Forwarded_For $remote_addr; }
lua code
This is a forwarding case, a simple scenario
If you want to do more complex functions, such as a distributed swap save, you can use consistency hash
You can also cache the unique value of user name to the following, and then forward IP as required
In fact, the most important thing about this case is to learn that you can write middleware and do data filtering in lua
local restyMemcached = require "resty.memcached" --Obtain GET Request parameters,_name Is the parameter name,If it's something else like ID Namely_id local name = ngx.var.arg_name --lua Initialization memcached local memcached ,err = restyMemcached:new() if not memcached then return ngx.say("memcached init failed: ",err) end -- Connect memcached local ok,err = memcached:connect("127.0.0.1",11211) if not ok then return ngx.say("connect failed: ",err) end -- from memcached Get parameters local value,flags,err =memcached:get("name-"..name) if err then return ngx.say("get failed: ",err) end --Be sure to pay attention,When underneath ngx.exec()When,You must not use any output at the front,I just forgot that,Stuck for half a day -- ngx.say("request key:", name," cache:", value) --This is not real switch,lua No, switch,This is through lua Of table To realize the false switch local switch = { ["100"] = function() --@server_100 see nginx Defined,Hand NGINX Forwarding -- ngx.say("switch:100") ngx.exec("@server_100") end, ["101"] = function() --@server_101 see nginx Defined,Hand NGINX Forwarding -- ngx.say("switch:101") ngx.exec("@server_101") end, ["102"] = function() --@server_102 see nginx Defined,Hand NGINX Forwarding -- ngx.say("switch:102") ngx.exec("@server_102") end, } --Obtained by subscript at switch Data in local server = switch[value] if server then --implement switch Lower case --Be sure to pay attention,When here ngx.exec()When,You must not use any output at the front,I just forgot that,Stuck for half a day server() return end return ngx.say("server not exist")
Request test
Send request
curl http://localhost/lua_request?name=100 curl http://localhost/lua_request?name=101 curl http://localhost/lua_request?name=102
You can see that all three machines have received the request
End