system-phone
Version:
手机模块 前端组件
305 lines (261 loc) • 9.43 kB
Plain Text
#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 'text/html';
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
lua_need_request_body on;
server {
listen 80;
server_name localhost;
charset utf-8;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
location /phone {
content_by_lua '
ngx.say("ok")
';
}
#在Lua中访问Redis
location /redis {
set_by_lua $mykey '
return string.gsub(ngx.var.arg_key, "%%(%x%x)", function(h) return string.char(tonumber(h, 16)) end)
';
internal; #只能内部访问
redis2_query get $mykey;
redis2_pass '192.168.50.220:6000';
}
#返回所有文件名
location /phone/rs/dir {
content_by_lua '
local lfs = require("lfs")
local rootpath = "/usr/local/openresty/nginx/html"
--返回index.html文件内容
local str = "index.html"
local htmlAttr = lfs.attributes (rootpath .. "/index.html")
str = str .. "," .. htmlAttr.modification
rootpath = rootpath .. "/static"
for entry in lfs.dir(rootpath) do
--去掉.及..
if entry ~= "." and entry ~= ".." then
local attr = lfs.attributes (rootpath .. "/" .. entry)
if str ~= "" then
str = str .. "|"
end
str = str .. "static/" .. entry .. "," .. attr.modification
end
end
ngx.print(str)
';
}
#返回具体文件
location /phone/rs/dir/ {
lua_need_request_body on;
content_by_lua '
local resp_body = ngx.req.get_body_data()
resp_body = string.gsub(resp_body, "\\\\", "/")
return ngx.exec("/"..resp_body)
';
}
#取业务逻辑等后台组件内容
location /phone/rs/dir2 {
#把安检、维修等模块内容统一发送回去
content_by_lua '
--如果body有内容,返回给定业务逻辑内容
local resp_body = ngx.req.get_body_data()
if resp_body ~= nil then
ngx.log(ngx.INFO, "resp_body:" .. resp_body)
resp_body = string.gsub(resp_body, "\\\\", "/")
local part = string.match(resp_body, "/(.+)%.")
-- 从redis里获取地址,转发到对应地址去
local parser = require("redis.parser")
local res = ngx.location.capture("/redis", {
args = { key = part }
})
if res.status == 200 then
local reply = parser.parse_reply(res.body)
return ngx.exec("/file2", {url = reply})
end
end
--定义获取内容的地址
local array = {
"http://192.168.50.220:9000/telephone",
"http://192.168.50.220:9200/safecheck",
"http://192.168.50.220:8200/system",
}
--对每一个地址,通过转发获取内容,进行拼接
local result = ""
for k,v in ipairs(array) do
local res = ngx.location.capture("/dir2", {
method = ngx.HTTP_GET,
args = { key = v }
})
if res.status == 200 and res.body ~= "" then
if result == "" then
result = res.body
else
result = result .. "|" .. res.body
end
end
end
ngx.print(result)
';
}
#获取模块内容
location /dir2 {
internal;
set_by_lua $myurl '
local result = string.gsub(ngx.var.arg_key, "%%(%x%x)", function(h) return string.char(tonumber(h, 16)) end)
return result
';
proxy_pass $myurl/rs/dir2/noxml;
}
#获取模块文件内容
location /file2 {
set_by_lua $myurl '
local result = string.gsub(ngx.var.arg_url, "%%(%x%x)", function(h) return string.char(tonumber(h, 16)) end)
return result
';
proxy_pass http://$myurl/rs/dir2;
}
#获取数据表元数据
location /phone/rs/db {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://192.168.50.220:9300/manage/rs/db;
}
location /phone/rs/dir/global {
rewrite /phone/rs/dir/global /static/globals.txt last;
}
location /phone/rs/dir/version {
rewrite /phone/rs/dir/version /static/app.json last;
}
#取vue配置
location /phone/rs/vue {
content_by_lua '
--定义获取内容的地址
local array = {
"http://192.168.50.220:9000/telephone",
"http://192.168.50.220:9200/safecheck",
}
--对每一个地址,通过转发获取内容,进行拼接
local result = ""
for k,v in ipairs(array) do
local res = ngx.location.capture("/vue2", {
method = ngx.HTTP_GET,
args = { key = v }
})
--截取array中最后"/"后的值
local ts = string.reverse(v)
local param1, param2 = string.find(ts, "/")
local m = string.len(v) - param2 + 1
v = string.sub(v, m+1, string.len(v))
v = [["]] .. v .. [["]]
--拼接字符串
if res.status == 200 and res.body ~= "" then
if result == "" then
result =v .. ":" .. res.body
else
result = result .. "," .. v .. ":" .. res.body
end
end
end
result = "{" .. result .. "}"
ngx.print(result)
';
}
#获取模块内容
location /vue2 {
internal;
set_by_lua $myurl '
local result = string.gsub(ngx.var.arg_key, "%%(%x%x)", function(h) return string.char(tonumber(h, 16)) end)
return result
';
proxy_pass $myurl/rs/vue;
}
#转发到资源服务
location /rs {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://192.168.50.220:7000/ldap/rs;
# access_log "logs/test.log";
}
error_page 405 =200 $uri;
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
#
#
#
#
}