header routing
This commit is contained in:
parent
005ec377d8
commit
ef47d7082c
3 changed files with 41 additions and 19 deletions
|
|
@ -5,4 +5,4 @@ COPY . .
|
|||
|
||||
RUN mvn clean package -DskipTests
|
||||
|
||||
ENTRYPOINT ["java", "-jar", "target/spring-boot-0.0.1-SNAPSHOT.jar"]
|
||||
ENTRYPOINT ["java","-Dserver.port=80", "-jar", "target/spring-boot-0.0.1-SNAPSHOT.jar"]
|
||||
|
|
|
|||
|
|
@ -1 +1,13 @@
|
|||
hello from nginx!
|
||||
|
||||
<button onclick="goToApp('java')">Go to Java</button>
|
||||
<button onclick="goToApp('dotnet')">Go to .NET</button>
|
||||
<script>
|
||||
function goToApp(appName) {
|
||||
fetch("/", {
|
||||
headers: { "X-App-Type": appName },
|
||||
})
|
||||
.then((r) => r.text())
|
||||
.then((t) => (document.body.innerHTML = t));
|
||||
}
|
||||
</script>
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
events { }
|
||||
events {}
|
||||
|
||||
http {
|
||||
upstream dotnet_app {
|
||||
|
|
@ -11,32 +11,42 @@ http {
|
|||
server java-2;
|
||||
}
|
||||
|
||||
# Map the header to the chosen upstream
|
||||
map $http_x_app_type $app_upstream {
|
||||
dotnet dotnet_app;
|
||||
java java_app;
|
||||
default "";
|
||||
}
|
||||
|
||||
server {
|
||||
listen 80;
|
||||
|
||||
root /usr/share/nginx/html;
|
||||
index index.html;
|
||||
|
||||
#
|
||||
# 1) If header is present -> route here
|
||||
#
|
||||
location @proxy_handler {
|
||||
proxy_pass http://$app_upstream;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
}
|
||||
|
||||
#
|
||||
# 2) Default location for all requests
|
||||
#
|
||||
location / {
|
||||
try_files $uri $uri/ =404;
|
||||
# If upstream selected -> jump to proxy handler
|
||||
error_page 418 = @proxy_handler;
|
||||
|
||||
if ($app_upstream != "") {
|
||||
return 418;
|
||||
}
|
||||
|
||||
location /dotnet/ {
|
||||
proxy_pass http://dotnet_app;
|
||||
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;
|
||||
}
|
||||
|
||||
location /java/ {
|
||||
proxy_pass http://java_app;
|
||||
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;
|
||||
# Otherwise serve index.html or static files
|
||||
try_files $uri $uri/ /index.html;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue