Platform
Load Balancing
If you have launched services on different instances and are using Nginx to load balance these services, you may encounter a 404 error when directly using Nginx Upstream for proxying.The reason is as follows: Since WebCal needs to route requests to different instances based on the Host header in the user’s request (you’ll notice that the domain names in the URLs of different instances are different; under normal circumstances, the domain name from which the request originates is set in the Host header),when using Nginx to proxy multiple backend services, the address of the user’s frontend request is that of the server hosting Nginx. However, when Nginx forwards the request to WebCal, it cannot reset the Host variable in the request headers to WebCal’s domain name, resulting in failed access.Below are two methods for implementing proper proxying: 1. A workaround approach, suitable for scenarios with a small number of services to proxy; 2. A method using Lua scripts, which is more versatile but also more complex.
A More Clever Approach
server {
listen 8001 default_server;
server_name a.example.com;
location / {
proxy_set_header Host a.cqa1.seetacloud.com:8443;
proxy_pass https://a.cqa1.seetacloud.com:8443;
}
}
server {
listen 8002 default_server;
server_name b.example.com;
location / {
proxy_set_header Host b.cqa1.seetacloud.com:8443;
proxy_pass https://b.cqa1.seetacloud.com:8443;
}
}
upstream main_balancer {
server 127.0.0.1:8001;
server 127.0.0.1:8002;
}
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://main_balancer;
}
}
Using Lua Scripts
Nginx Configuration
server {
listen 8000 default_server;
server_name web1.example.com;
location / {
set $proxy_pass_target "";
set_by_lua_file $proxy_pass_target /usr/local/openresty/nginx/lua/xxxxxx.lua;
if ($proxy_pass_target = ""){
return 404;
}
proxy_set_header Host $proxy_pass_target;
proxy_pass https://$proxy_pass_target;
}
}
Lua scripts
local servers = {
["0"] = "a.cqa1.seetacloud.com:8443",
["1"] = "b.cqa1.seetacloud.com:8443",
["2"] = "c.cqa1.seetacloud.com:8443",
}
local path = ngx.var.uri
local target = ""
if path == "/api/v1" then
target = "api_v1.example.com"
elseif path == "/api/v2" then
target = "api_v2.example.com"
else
target = "default.api.example.com"
end
return target
