1
0
mirror of https://github.com/chatopera/cosin.git synced 2025-08-01 16:38:02 +08:00
lecjy 29d7756959 #1001
Signed-off-by: lecjy <5655726966@qq.com>
2024-01-28 10:34:21 +08:00

98 lines
4.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 {
instance_map = {}
balancer = require "ngx.balancer"
nkeys = require "table.nkeys"
cjson = require "cjson"
}
server {
root /usr/share/nginx/html;
client_max_body_size 100M;
location ~* /api/.*?/(.*) {
access_by_lua_block {
local prefix = ngx.var.uri:match("^/api/([^/]+)/")
local service_name = 'cskefu-'..prefix..'-service'
local cjson = require "cjson"
if not instance_map[service_name] then
instance_map[service_name] = {}
end
local queue_length = table.nkeys(instance_map[service_name])
local mapper = {}
for i = 1, queue_length do
mapper[instance_map[service_name][i]["ip"]..":"..instance_map[service_name][i]["port"]] = i
end
local result = ngx.location.capture("/nacos", {args='serviceName='..service_name})
local hosts = cjson.decode(result.body)['hosts']
local hosts_length = table.nkeys(hosts)
for i = 1, hosts_length do
if not mapper[hosts[i]["ip"]..":"..hosts[i]["port"]] then
table.insert(instance_map[service_name], {
["ip"] = hosts[i]["ip"],
["port"] = hosts[i]["port"]
})
end
end
local instance = table.remove(instance_map[service_name], 1)
ngx.ctx.host = instance["ip"]
ngx.ctx.port = instance["port"]
}
proxy_http_version 1.1;
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-upstream/$1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
location = /nacos {
internal;
set $args "$args&namespaceId=cskefu&groupName=cskefu-default";
proxy_pass http://nacos:8848/nacos/v1/ns/instance/list;
}
}
upstream cskefu-upstream {
server 0.0.0.1;
balancer_by_lua_block {
local ok, err = balancer.set_current_peer(ngx.ctx.host, ngx.ctx.port)
if not ok then
ngx.log(ngx.ERR, "failed to set the current peer: ", err)
return ngx.exit(500)
end
}
}
# Don't reveal OpenResty version to clients.
# server_tokens off;
}