#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
#以上代碼都是基礎配置
#設置服務
upstream xxx1{
server 192.168.1.101:8080;
}
upstream xxx2 {
server 192.168.1.101:8090;
}
server{
listen 8888;
location / {
#引入ual腳本地址
content_by_lua_file "lua/test.lua";
}
#請求的反向代理地址
location @tomcat1 {
proxy_pass http://xxx1;
}
location @tomcat2 {
proxy_pass http://xxx2;
}
}
}
#配置lua腳本
#引入獲取參數的方法,
local method = ngx.var.request_method
local code = nil
--判斷請求方式。
if method == "GET" then
code = ngx.req.get_uri_args()["id"] or 0
ngx.log(ngx.INFO, " code:", code)
elseif method == "POST" then
ngx.req.read_body()
code = ngx.req.get_post_args()["id"] or 0
end
#math.fmod("prams", 2 )取餘函數,tonumber(code)轉換成整數函數
if math.fmod(tonumber(code), 2 ) == 0 then
ngx.exec('@tomcat1') #執行對應的location
end
if math.fmod(tonumber(code), 2 ) == 1 then
ngx.exec('@tomcat2')
end