1
0
mirror of https://github.com/chatopera/cosin.git synced 2025-08-01 16:38:02 +08:00
2023-10-16 18:41:24 +08:00

173 lines
7.2 KiB
Plaintext

http {
include /usr/local/openresty/nginx/conf/mime.types;
default_type application/octet-stream;
log_format main '$server_name $remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for" $ssl_protocol $ssl_cipher $upstream_addr $request_time $upstream_response_time';
# access_log logs/access.log main;
# log_format nginxlog_json escape=json '{ "timestamp": "$time_iso8601", ' '"remote_addr": "$remote_addr", ' '"body_bytes_sent": $body_bytes_sent, ' '"request_time": $request_time, ' '"response_status": $status, ' '"request": "$request", ' '"request_method": "$request_method", ' '"host": "$host",' '"upstream_addr": "$upstream_addr",' '"http_x_forwarded_for": "$http_x_forwarded_for",' '"http_referrer": "$http_referer", ' '"http_user_agent": "$http_user_agent", ' '"http_version": "$server_protocol", ' '"nginx_access": true }';
# access_log /dev/stdout nginxlog_json;
# See Move default writable paths to a dedicated directory (#119)
# https://github.com/openresty/docker-openresty/issues/119
client_body_temp_path /var/run/openresty/nginx-client-body;
proxy_temp_path /var/run/openresty/nginx-proxy;
fastcgi_temp_path /var/run/openresty/nginx-fastcgi;
uwsgi_temp_path /var/run/openresty/nginx-uwsgi;
scgi_temp_path /var/run/openresty/nginx-scgi;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
init_by_lua_block {
instanceQueue = {}
cjson = require "cjson"
}
upstream cskefu_web_gateway {
server 0.0.0.1;
balancer_by_lua_block {
local balancer = require "ngx.balancer"
local instance = table.remove(instanceQueue, 1)
local ok, err = balancer.set_current_peer(instance["ip"], instance["port"])
if not ok then
ngx.log(ngx.ERR, "failed to set the current peer: ", err)
return ngx.exit(500)
end
}
}
upstream cskefu_websocket_service {
server 0.0.0.1;
balancer_by_lua_block {
local balancer = require "ngx.balancer"
local instance = table.remove(instanceQueue, 1)
local ok, err = balancer.set_current_peer(instance["ip"], instance["port"])
if not ok then
ngx.log(ngx.ERR, "failed to set the current peer: ", err)
return ngx.exit(500)
end
}
}
server {
root /usr/share/nginx/html;
client_max_body_size 100M;
location /api/ {
access_by_lua_block {
local cjson = require "cjson"
local queueLength = table.getn(instanceQueue)
local mapper = {}
if queueLength > 0 then
for i = 1, queueLength do
local id = instanceQueue[i]["ip"]..":"..instanceQueue[i]["port"]
mapper[id] = i
end
end
local result = ngx.location.capture("/nacos", {args='serviceName=cskefu-web-gateway'})
local hosts = cjson.decode(result.body)['hosts']
local hostsLength = table.getn(hosts)
local others = {}
instanceQueue = {}
for i = 1, hostsLength do
local id = hosts[i]["ip"]..":"..hosts[i]["port"]
if hosts[i]["healthy"] == true then
if mapper[id] then
instanceQueue[mapper[id]] = {
["ip"] = hosts[i]["ip"],
["port"] = hosts[i]["port"]
}
else
table.insert(others, {
["ip"] = hosts[i]["ip"],
["port"] = hosts[i]["port"]
})
end
end
end
local othersLength = table.getn(others)
if othersLength > 0 then
for i = 1, othersLength do
table.insert(instanceQueue, {
["ip"] = others[i]["ip"],
["port"] = others[i]["port"]
})
end
end
}
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_set_header X-Forwarded-Proto $scheme;
# proxy_pass http://cskefu-web-gateway:8080/;
proxy_pass http://cskefu_web_gateway/;
}
location /ws/ {
access_by_lua_block {
local cjson = require "cjson"
local queueLength = table.getn(instanceQueue)
local mapper = {}
if queueLength > 0 then
for i = 1, queueLength do
local id = instanceQueue[i]["ip"]..":"..instanceQueue[i]["port"]
mapper[id] = i
end
end
local result = ngx.location.capture("/nacos", {args='serviceName=cskefu-websocket-service'})
local hosts = cjson.decode(result.body)['hosts']
local hostsLength = table.getn(hosts)
local others = {}
instanceQueue = {}
for i = 1, hostsLength do
local id = hosts[i]["ip"]..":"..hosts[i]["port"]
if hosts[i]["healthy"] == true then
if mapper[id] then
instanceQueue[mapper[id]] = {
["ip"] = hosts[i]["ip"],
["port"] = hosts[i]["port"]
}
else
table.insert(others, {
["ip"] = hosts[i]["ip"],
["port"] = hosts[i]["port"]
})
end
end
end
local othersLength = table.getn(others)
if othersLength > 0 then
for i = 1, othersLength do
table.insert(instanceQueue, {
["ip"] = others[i]["ip"],
["port"] = others[i]["port"]
})
end
end
}
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_http_version 1.1;
# proxy_pass http://cskefu-websocket-service:10000/ws/;
proxy_pass http://cskefu_websocket_service;
}
location = /nacos {
internal;
set $args "$args&namespaceId=cskefu&groupName=cskefu-default";
proxy_pass http://nacos:8848/nacos/v1/ns/instance/list;
}
}
# Don't reveal OpenResty version to clients.
# server_tokens off;
}