events {}
error_log /var/log/nginx/error.log debug;
http {
# Add CORS headers for all requests
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization' always;
map $http_x_proxy_port $xproxyport {
default 80; # Default port if X-PROXY-PORT header is not set
~^[0-9]+$ $http_x_proxy_port; # Use the value of X-PROXY-PORT if it is a number
}
log_format custom_debug 'Host: $host, Container: $container, Port: $xproxyport, Path: $uri, X-PROXY-PORT: $http_x_proxy_port, complete proxying URL will be: http://$container:$xproxyport$rest_of_path';
access_log /var/log/nginx/access.log custom_debug;
server {
listen 80;
http2 on;
server_name proxy;
resolver 127.0.0.11;
location / {
root /usr/share/nginx/html;
index index.html;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $proxy_protocol_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
if ($request_method = OPTIONS) {
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}
}
location ~ ^/proxy/([^/]+)(/.*)?$ {
# Extract container name from the URL
set $container $1;
set $rest_of_path $2;
if ($request_method = OPTIONS) {
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}
# Proxy the request to the respective container and port
proxy_pass http://$container:$xproxyport$rest_of_path;
proxy_set_header Host "0.0.0.0";
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;
}
}
}