Skip to content

Instantly share code, notes, and snippets.

@craftpip
Last active March 25, 2024 13:35
Show Gist options
  • Save craftpip/1ee30be73d11ff53d66c0eba1906fc33 to your computer and use it in GitHub Desktop.
Save craftpip/1ee30be73d11ff53d66c0eba1906fc33 to your computer and use it in GitHub Desktop.
nginx reverse proxy config for <port>.domain.com to localhost:<port>
# This is a default site configuration which will simply return 404, preventing
# chance access to any other virtualhost.
resolver 172.17.0.1 valid=1s;
server {
listen 80;
server_name ~^(?<port>\d+)\.boniface\.pe$;
location / {
set $backend_port $port;
proxy_pass http://host.docker.internal:$port;
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;
}
}
@craftpip
Copy link
Author

for apache:

<VirtualHost *:80>
    ServerName 80.boniface.pe
    ServerAlias *.boniface.pe
    RewriteEngine On

    # Exclude port 80 from the rewrite rule to prevent a loop
    RewriteCond %{HTTP_HOST} !^80\.boniface\.pe$
    # Extract the port number using a RewriteCond
    RewriteCond %{HTTP_HOST} ^([0-9]+)\.boniface\.pe$
    RewriteRule ^/(.*)$ http://host.docker.internal:%1/$1 [P,L]

    # Proxy settings
    ProxyPreserveHost On
    ProxyPassReverse / http://host.docker.internal/
</VirtualHost>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment