add tls to nginx redirections
This commit is contained in:
parent
6c0038b421
commit
30d22895d4
2 changed files with 92 additions and 9 deletions
|
@ -114,7 +114,7 @@ export class Runner {
|
|||
const sites = new Set<string>();
|
||||
for (const redir of this.config.redirections) {
|
||||
if (redir.kind !== "http") continue;
|
||||
await this.nginx.add_proxy(redir.domain, redir.port, paths.sites);
|
||||
await this.nginx.add_proxy(redir.domain, redir.port, paths.sites, redir.tls);
|
||||
sites.add(redir.domain);
|
||||
}
|
||||
for await (const domains of this.nginx.read_all_in_dir(paths.sites)) {
|
||||
|
|
|
@ -10,15 +10,52 @@ export class NginxController {
|
|||
this.enabled_conf_dir = enabled_conf_dir;
|
||||
}
|
||||
|
||||
public async add_proxy(domain: string, port: number, conf_dir: string) {
|
||||
/*
|
||||
server {
|
||||
server_name barnulf.net;
|
||||
location / {
|
||||
proxy_pass http://barnulf.net:8051;
|
||||
}
|
||||
|
||||
listen [::]:443 ssl ipv6only=on; # managed by Certbot
|
||||
listen 443 ssl; # managed by Certbot
|
||||
ssl_certificate /etc/letsencrypt/live/barnulf.net/fullchain.pem; # managed by Certbot
|
||||
ssl_certificate_key /etc/letsencrypt/live/barnulf.net/privkey.pem; # managed by Certbot
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
public async add_proxy(domain: string, port: number, conf_dir: string, tls: boolean) {
|
||||
const conf_file_path = await this.set_http_config(domain, port, conf_dir);
|
||||
await this.reload();
|
||||
if (tls) {
|
||||
await this.gen_cert(domain);
|
||||
await this.set_https_config(domain, port, conf_dir);
|
||||
await this.reload();
|
||||
}
|
||||
return conf_file_path;
|
||||
}
|
||||
|
||||
private async set_https_config(domain: string, port: number, conf_dir: string) {
|
||||
const conf_file_content = `
|
||||
server {
|
||||
listen 80;
|
||||
listen [::]:80;
|
||||
server_name ${domain};
|
||||
location / {
|
||||
proxy_pass http://${this.proxy_target_domain}:${port};
|
||||
}
|
||||
listen 80;
|
||||
listen [::]:80;
|
||||
server_name ${domain};
|
||||
return 301 https://$host$request_uri;
|
||||
}
|
||||
|
||||
server {
|
||||
listen 443 ssl;
|
||||
listen [::]:443 ssl;
|
||||
server_name ${domain};
|
||||
ssl_certificate /etc/letsencrypt/live/${domain}/fullchain.pem; # managed by Certbot
|
||||
ssl_certificate_key /etc/letsencrypt/live/${domain}/privkey.pem; # managed by Certbot
|
||||
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
|
||||
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
|
||||
location / {
|
||||
proxy_pass http://${this.proxy_target_domain}:${port};
|
||||
}
|
||||
}
|
||||
`;
|
||||
const conf_file_path = path.join(conf_dir, domain + ".conf");
|
||||
|
@ -26,10 +63,39 @@ server {
|
|||
await Deno.writeTextFile(conf_file_path, conf_file_content);
|
||||
if (await exists(enabled_conf_file_path)) await Deno.remove(enabled_conf_file_path);
|
||||
await run("ln", "-s", await Deno.realPath(conf_file_path), enabled_conf_file_path);
|
||||
await this.reload();
|
||||
return conf_file_path;
|
||||
}
|
||||
|
||||
private async set_http_config(domain: string, port: number, conf_dir: string) {
|
||||
const conf_file_content = `
|
||||
server {
|
||||
listen 80;
|
||||
listen [::]:80;
|
||||
server_name ${domain};
|
||||
location / {
|
||||
proxy_pass http://${this.proxy_target_domain}:${port};
|
||||
}
|
||||
}
|
||||
`;
|
||||
const conf_file_path = path.join(conf_dir, domain + ".conf");
|
||||
const enabled_conf_file_path = path.join(this.enabled_conf_dir, domain + ".conf");
|
||||
await Deno.writeTextFile(conf_file_path, conf_file_content);
|
||||
if (await exists(enabled_conf_file_path)) await Deno.remove(enabled_conf_file_path);
|
||||
await run("ln", "-s", await Deno.realPath(conf_file_path), enabled_conf_file_path);
|
||||
return conf_file_path;
|
||||
}
|
||||
|
||||
private async gen_cert(domain: string) {
|
||||
await run(
|
||||
"certbot",
|
||||
"--nginx",
|
||||
...["-d", domain],
|
||||
"--non-interactive",
|
||||
"--agree-tos",
|
||||
...["--email", "matthieu@imagevo.fr"],
|
||||
);
|
||||
}
|
||||
|
||||
public async remove_proxy(domain: string, conf_dir: string) {
|
||||
const conf_file_path = path.join(conf_dir, domain + ".conf");
|
||||
const enabled_conf_file_path = path.join(this.enabled_conf_dir, domain + ".conf");
|
||||
|
@ -50,3 +116,20 @@ server {
|
|||
await run("systemctl", "restart", "nginx");
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
server {
|
||||
if ($host = barnulf.net) {
|
||||
return 301 https://$host$request_uri;
|
||||
} # managed by Certbot
|
||||
|
||||
|
||||
listen 80;
|
||||
listen [::]:80;
|
||||
server_name barnulf.net;
|
||||
return 404; # managed by Certbot
|
||||
|
||||
|
||||
}
|
||||
|
||||
*/
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue