From 811a8d1349ed8807f5c4963f3a647063b6918d4d Mon Sep 17 00:00:00 2001 From: bangbang93 Date: Wed, 15 Jun 2022 18:43:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81byoc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nginx/nginx-http.conf | 54 +++++++++++++++++++++++++++++++++++++++++++ src/bootstrap.ts | 11 +++++---- src/cluster.ts | 4 ++-- 3 files changed, 63 insertions(+), 6 deletions(-) create mode 100644 nginx/nginx-http.conf diff --git a/nginx/nginx-http.conf b/nginx/nginx-http.conf new file mode 100644 index 0000000..786f1bb --- /dev/null +++ b/nginx/nginx-http.conf @@ -0,0 +1,54 @@ +daemon off; +events { + worker_connections 10240; + accept_mutex on; +} +pid <%= root %>/nginx.pid; +worker_rlimit_nofile 65535; +http { + include mime.types; + default_type application/octet-stream; + access_log <%= root %>/access.log; + error_log stderr; + + map $arg_name $name { + ~.+ $arg_name; + default $prefix; + } + + server { + listen <%= port %> default; + root <%= root %>/cache; + + if ($uri ~ ^/download/(..)) { + set $prefix $1; + } + + location @be { + proxy_pass http://localhost:<%= port+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 ~ /download/(.*) { + set $hash $1; + sendfile on; + tcp_nopush on; + tcp_nodelay on; + add_header x-bmclapi-hash $hash; + try_files /$prefix/$hash @be; + add_header content-disposition "attachment; filename=\"$arg_name\""; + + } + + location / { + proxy_pass http://localhost:<%= port+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; + } + } +} diff --git a/src/bootstrap.ts b/src/bootstrap.ts index 910c06a..37ab4e9 100644 --- a/src/bootstrap.ts +++ b/src/bootstrap.ts @@ -20,12 +20,15 @@ export async function bootstrap(version: string): Promise { await cluster.syncFiles(files) await cluster.connect() - console.log('请求证书') - await cluster.requestCert() + const proto = process.env.CLUSTER_BYOC !== 'true' ? 'https' : 'http' + if (proto === 'https') { + console.log('请求证书') + await cluster.requestCert() + } if (process.env.ENABLE_NGINX) { - await cluster.setupNginx(join(__dirname, '..'), cluster.port) + await cluster.setupNginx(join(__dirname, '..'), cluster.port, proto) } - const server = cluster.setupExpress(!process.env.ENABLE_NGINX) + const server = cluster.setupExpress(proto === 'https') try { await cluster.listen() await cluster.enable() diff --git a/src/cluster.ts b/src/cluster.ts index 01413c9..9b68c4a 100644 --- a/src/cluster.ts +++ b/src/cluster.ts @@ -163,10 +163,10 @@ export class Cluster { return this.server } - public async setupNginx(pwd: string, appPort: number): Promise { + public async setupNginx(pwd: string, appPort: number, proto: string): Promise { this._port++ const dir = await mkdtemp(join(tmpdir(), 'openbmclapi')) - const confFile = `${dir}/nginx/nginx.conf` + const confFile = proto === 'https' ? `${dir}/nginx/nginx.conf` : `${dir}/nginx/nginx-http.conf` const confTemplate = await readFile(join(__dirname, '..', 'nginx', 'nginx.conf'), 'utf8') console.log('nginx conf', confFile)