mirror of
https://github.com/CyberL1/dlinux-dashboard.git
synced 2025-01-22 01:19:18 -05:00
use nginx for proxy
This commit is contained in:
parent
3e6c1b9ddd
commit
84f0d2080a
@ -5,6 +5,13 @@ services:
|
||||
build: .
|
||||
ports:
|
||||
- 3000:3000
|
||||
- 3001:3001
|
||||
volumes:
|
||||
- /var/run/docker.sock:/var/run/docker.sock:ro
|
||||
proxy:
|
||||
image: openresty/openresty:alpine-fat
|
||||
env_file: .env
|
||||
ports:
|
||||
- 80:80
|
||||
volumes:
|
||||
- ./proxy.conf:/etc/nginx/conf.d/proxy.conf
|
||||
network_mode: host
|
||||
|
36
proxy.conf
Normal file
36
proxy.conf
Normal file
@ -0,0 +1,36 @@
|
||||
server {
|
||||
listen 80;
|
||||
listen [::]:80;
|
||||
|
||||
server_name code-containers.localhost;
|
||||
|
||||
location / {
|
||||
proxy_pass http://127.0.0.1:3000;
|
||||
}
|
||||
}
|
||||
|
||||
server {
|
||||
listen 80;
|
||||
listen [::]:80;
|
||||
|
||||
server_name "~^(port-(?<port>.*)\.)?(?<container>.*)\.code-containers.localhost";
|
||||
|
||||
location / {
|
||||
if ($port = "") {
|
||||
set $port "80";
|
||||
}
|
||||
|
||||
set $ip '';
|
||||
rewrite_by_lua_block {
|
||||
local shell = require("resty.shell")
|
||||
|
||||
local ok, stdout, strerr, reason, status = shell.run("curl http://127.0.0.1:3000/containers/" .. ngx.var.container .. "/ip")
|
||||
ngx.var.ip = stdout
|
||||
}
|
||||
|
||||
proxy_pass http://$ip:$port;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Upgrade $http_upgrade;
|
||||
proxy_set_header Connection "upgrade";
|
||||
}
|
||||
}
|
@ -1,7 +1,6 @@
|
||||
import "dotenv/config";
|
||||
import fastify from "fastify";
|
||||
import { readdirSync } from "fs";
|
||||
import "./proxy.ts";
|
||||
|
||||
const app = fastify();
|
||||
|
||||
|
41
src/proxy.ts
41
src/proxy.ts
@ -1,41 +0,0 @@
|
||||
import fastify from "fastify";
|
||||
import { getContainer } from "./utils/containers.ts";
|
||||
|
||||
const app = fastify();
|
||||
|
||||
app.all(
|
||||
"/*",
|
||||
{ constraints: { host: new RegExp(`.*\.${process.env.PROXY_DOMAIN}`) } },
|
||||
async (req, reply) => {
|
||||
const subdomains = req.headers.host.split(".");
|
||||
|
||||
const port = subdomains[0].startsWith("port-")
|
||||
? subdomains[0].slice(5)
|
||||
: 80;
|
||||
|
||||
const containerName = subdomains[subdomains[0].startsWith("port-") ? 1 : 0];
|
||||
|
||||
const { NetworkSettings } = await getContainer(containerName).inspect();
|
||||
const { IPAddress: ip } = NetworkSettings;
|
||||
|
||||
const proxyRequest = await fetch(`http://${ip}:${port}${req.url}`, {
|
||||
headers: req.headers,
|
||||
method: req.method,
|
||||
});
|
||||
|
||||
const proxyResponse = await proxyRequest.text();
|
||||
reply.header("content-type", proxyRequest.headers.get("content-type"));
|
||||
|
||||
return proxyResponse;
|
||||
},
|
||||
);
|
||||
|
||||
await app.listen({
|
||||
port: Number(process.env.PROXY_PORT),
|
||||
host: process.env.HOST,
|
||||
});
|
||||
|
||||
console.log(
|
||||
"Proxy ready on",
|
||||
`http://${process.env.HOST}:${Number(process.env.PROXY_PORT)}`,
|
||||
);
|
12
src/routes/containers/_id/ip.ts
Normal file
12
src/routes/containers/_id/ip.ts
Normal file
@ -0,0 +1,12 @@
|
||||
import type { Container } from "#src/types/Container.ts";
|
||||
import { getContainer } from "#src/utils/containers.ts";
|
||||
import type { FastifyInstance, FastifyRequest } from "fastify";
|
||||
|
||||
export default (fastify: FastifyInstance) => {
|
||||
fastify.get("/", async (req: FastifyRequest<{ Params: Container }>) => {
|
||||
const container = getContainer(req.params.id);
|
||||
|
||||
const { NetworkSettings } = await container.inspect();
|
||||
return NetworkSettings.IPAddress;
|
||||
});
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user