Last active
March 3, 2024 06:41
-
-
Save kmjones1979/9ada952922e56c78dbceadd614d2df73 to your computer and use it in GitHub Desktop.
Example NGINX configuration to do service routing based on headers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
events { worker_connections 2014; } | |
http { | |
include /etc/nginx/conf.d/*.conf; | |
default_type text/plain; | |
error_log /var/log/nginx/example_debug_error.log debug; | |
access_log /var/log/nginx/example_access.log; | |
match health_check { | |
status 200; | |
} | |
# map HTTP header Service-Name-Code to a variable | |
map $http_service_name_code $service_upstream { | |
processPartsOrder processPartsOrder; | |
registerNewAccount registerNewAccount; | |
default default; | |
} | |
# map HTTP header Service-Name-Code to a HTTP header named host_dynamic | |
map $http_service_name_code $host_dynamic { | |
processPartsOrder domain1.example.com; | |
registerNewAccount domain2.example.com; | |
default default; | |
} | |
# map HTTP header Service-Version to a variable | |
map $http_service_version $service_version { | |
"5.4.0.0" "v5.4.0.0"; | |
"5.3.9.9" "v5.3.9.9"; | |
default ""; | |
} | |
server { | |
listen 8098; | |
server_name domain1.example.com; | |
status_zone processPartsOrder; | |
return 200 "You have reached the processPartsOrder service.\n | |
Info: | |
Server Address:\t\t\t $server_addr:$server_port\n | |
Service-Name-Code Header:\t $http_service_name_code | |
Service-Version Header:\t\t $http_service_version\n | |
Host Header:\t\t\t $host | |
Upstream:\t\t\t $service_upstream | |
Request URI:\t\t\t $request_uri | |
Full Upstream URL:\t\t http://$service_upstream$uri$is_args$args\n | |
Served by NGINX Plus $nginx_version\n"; | |
} | |
server { | |
listen 9091; | |
server_name domain2.example.com; | |
status_zone registerNewAccount; | |
return 200 "You have reached the registerNewAccount service.\n | |
Info: | |
Server Address:\t\t\t $server_addr:$server_port\n | |
Service-Name-Code Header:\t $http_service_name_code | |
Service-Version Header:\t\t $http_service_version\n | |
Host Header:\t\t\t $host | |
Upstream:\t\t\t $service_upstream | |
Request URI:\t\t\t $request_uri | |
Full Upstream URL:\t\t http://$service_upstream$uri$is_args$args\n | |
Served by NGINX Plus $nginx_version\n"; | |
} | |
server { | |
listen 7001; | |
server_name default; | |
status_zone default; | |
return 200 "Please specify a Service-Name-Code header to reach a service.\n | |
Info: | |
Server Address:\t\t\t $server_addr:$server_port\n | |
Service-Name-Code Header:\t $http_service_name_code | |
Service-Version Header:\t\t $http_service_version\n | |
Host Header:\t\t\t $host | |
Upstream:\t\t\t $service_upstream | |
Request URI:\t\t\t $request_uri | |
Full Upstream URL:\t\t http://$service_upstream$uri$is_args$args\n | |
Served by NGINX Plus $nginx_version\n"; | |
} | |
upstream processPartsOrder { | |
zone processPartsOrder 64k; | |
server 127.0.0.1:8098; | |
} | |
upstream registerNewAccount { | |
zone registerNewAccount 64k; | |
server 127.0.0.1:9091; | |
} | |
upstream default { | |
zone default 64k; | |
server 127.0.0.1:7001; | |
} | |
server { | |
listen 80 default; | |
location / { | |
proxy_pass http://$service_upstream/$service_upstream/$service_version$uri$is_args$args; | |
proxy_set_header Host $host_dynamic; | |
proxy_set_header X-Forwarded-For $remote_addr; | |
} | |
location @hc-ppo { | |
proxy_pass http://processPartsOrder; | |
health_check interval=3s fails=1 passes=2 uri=/ match=health_check; | |
} | |
location @hc-rno { | |
proxy_pass http://registerNewAccount; | |
health_check interval=3s fails=1 passes=2 uri=/ match=health_check; | |
} | |
location @hc-def { | |
proxy_pass http://default; | |
health_check interval=3s fails=1 passes=2 uri=/ match=health_check; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I don't see how the named locations gets selected. Could you explain it?