header routing
This commit is contained in:
parent
ef47d7082c
commit
71d1b559a8
5 changed files with 36 additions and 55 deletions
|
|
@ -18,6 +18,7 @@ app.MapGet("/{**ok}", async (HttpContext context) =>
|
||||||
var headers = request.Headers;
|
var headers = request.Headers;
|
||||||
|
|
||||||
var responseText = new System.Text.StringBuilder();
|
var responseText = new System.Text.StringBuilder();
|
||||||
|
responseText.AppendLine("<pre>");
|
||||||
responseText.AppendLine($"Server Name: {name}");
|
responseText.AppendLine($"Server Name: {name}");
|
||||||
responseText.AppendLine($"IP Address: {ip}");
|
responseText.AppendLine($"IP Address: {ip}");
|
||||||
responseText.AppendLine($"Method: {method}");
|
responseText.AppendLine($"Method: {method}");
|
||||||
|
|
@ -30,6 +31,7 @@ app.MapGet("/{**ok}", async (HttpContext context) =>
|
||||||
{
|
{
|
||||||
responseText.AppendLine($" {header.Key}: {header.Value}");
|
responseText.AppendLine($" {header.Key}: {header.Value}");
|
||||||
}
|
}
|
||||||
|
responseText.AppendLine("</pre>");
|
||||||
|
|
||||||
await context.Response.WriteAsync(responseText.ToString());
|
await context.Response.WriteAsync(responseText.ToString());
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
public class Application {
|
public class Application {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(Application.class, args);
|
SpringApplication.run(Application.class, args);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,23 +1,27 @@
|
||||||
package java_app.spring_boot;
|
package java_app.spring_boot;
|
||||||
|
|
||||||
import org.springframework.web.bind.annotation.*;
|
|
||||||
import org.springframework.http.HttpHeaders;
|
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
public class MirrorController {
|
public class MirrorController {
|
||||||
|
|
||||||
private final String serverName = System.getenv().getOrDefault("NAME", "unnamed");
|
private final String serverName = System.getenv().getOrDefault(
|
||||||
|
"NAME",
|
||||||
|
"unnamed"
|
||||||
|
);
|
||||||
|
|
||||||
@GetMapping(path = "/**") // Obsługuje dowolną ścieżkę
|
@GetMapping(path = "/**") // Obsługuje dowolną ścieżkę
|
||||||
public String mirrorRequest(HttpServletRequest request) {
|
public String mirrorRequest(HttpServletRequest request) {
|
||||||
|
|
||||||
String ip = request.getRemoteAddr();
|
String ip = request.getRemoteAddr();
|
||||||
String method = request.getMethod();
|
String method = request.getMethod();
|
||||||
String scheme = request.getScheme();
|
String scheme = request.getScheme();
|
||||||
String host = request.getHeader(HttpHeaders.HOST);
|
String host = request.getHeader(HttpHeaders.HOST);
|
||||||
String path = request.getRequestURI();
|
String path = request.getRequestURI();
|
||||||
String queryString = request.getQueryString() != null ? "?" + request.getQueryString() : "";
|
String queryString = request.getQueryString() != null
|
||||||
|
? "?" + request.getQueryString()
|
||||||
|
: "";
|
||||||
|
|
||||||
StringBuilder responseText = new StringBuilder();
|
StringBuilder responseText = new StringBuilder();
|
||||||
|
|
||||||
|
|
@ -30,10 +34,18 @@ public class MirrorController {
|
||||||
responseText.append("QueryString: ").append(queryString).append("\n");
|
responseText.append("QueryString: ").append(queryString).append("\n");
|
||||||
responseText.append("Headers:\n");
|
responseText.append("Headers:\n");
|
||||||
|
|
||||||
request.getHeaderNames().asIterator().forEachRemaining(headerName -> {
|
request
|
||||||
responseText.append(" ").append(headerName).append(": ").append(request.getHeader(headerName)).append("\n");
|
.getHeaderNames()
|
||||||
|
.asIterator()
|
||||||
|
.forEachRemaining(headerName -> {
|
||||||
|
responseText
|
||||||
|
.append(" ")
|
||||||
|
.append(headerName)
|
||||||
|
.append(": ")
|
||||||
|
.append(request.getHeader(headerName))
|
||||||
|
.append("\n");
|
||||||
});
|
});
|
||||||
|
|
||||||
return "<pre>" + responseText.toString() + "</pre>";
|
return "<pre>" + responseText.toString() + "</pre>\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,13 +1 @@
|
||||||
hello from nginx!
|
welcome to 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,52 +1,30 @@
|
||||||
events {}
|
events { }
|
||||||
|
|
||||||
http {
|
http {
|
||||||
upstream dotnet_app {
|
upstream dotnet_app {
|
||||||
server dotnet-1;
|
server dotnet-1;
|
||||||
server dotnet-2;
|
server dotnet-2;
|
||||||
}
|
}
|
||||||
|
|
||||||
upstream java_app {
|
upstream java_app {
|
||||||
server java-1;
|
server java-1;
|
||||||
server java-2;
|
server java-2;
|
||||||
}
|
}
|
||||||
|
|
||||||
# Map the header to the chosen upstream
|
map $http_x_app_type $app_backend {
|
||||||
map $http_x_app_type $app_upstream {
|
default dotnet_app;
|
||||||
dotnet dotnet_app;
|
"dotnet" dotnet_app;
|
||||||
java java_app;
|
"java" java_app;
|
||||||
default "";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
server {
|
server {
|
||||||
listen 80;
|
listen 80;
|
||||||
|
|
||||||
root /usr/share/nginx/html;
|
location / {
|
||||||
index index.html;
|
proxy_pass http://$app_backend;
|
||||||
|
proxy_http_version 1.1;
|
||||||
#
|
|
||||||
# 1) If header is present -> route here
|
|
||||||
#
|
|
||||||
location @proxy_handler {
|
|
||||||
proxy_pass http://$app_upstream;
|
|
||||||
proxy_set_header Host $host;
|
proxy_set_header Host $host;
|
||||||
proxy_set_header X-Real-IP $remote_addr;
|
proxy_set_header X-Real-IP $remote_addr;
|
||||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||||
}
|
proxy_set_header X-Forwarded-Proto $scheme;
|
||||||
|
|
||||||
#
|
|
||||||
# 2) Default location for all requests
|
|
||||||
#
|
|
||||||
location / {
|
|
||||||
# If upstream selected -> jump to proxy handler
|
|
||||||
error_page 418 = @proxy_handler;
|
|
||||||
|
|
||||||
if ($app_upstream != "") {
|
|
||||||
return 418;
|
|
||||||
}
|
|
||||||
|
|
||||||
# Otherwise serve index.html or static files
|
|
||||||
try_files $uri $uri/ /index.html;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue