Skip to content

Commit

Permalink
Refactor nginx configs to files
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubno committed Jul 18, 2024
1 parent e70f01d commit 954d8b2
Show file tree
Hide file tree
Showing 6 changed files with 251 additions and 269 deletions.
144 changes: 13 additions & 131 deletions packages/nomad/client-proxy.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ variable "domain_name" {
type = string
}

variable "load_balancer_conf" {
type = string
}

variable "nginx_conf" {
type = string
}

locals {
domain_name_escaped = replace(var.domain_name, ".", "\\.")
}
Expand Down Expand Up @@ -77,155 +85,29 @@ job "client-proxy" {
network_mode = "host"
ports = [var.client_proxy_health_port_name, var.client_proxy_port_name]
volumes = [
"local:/etc/nginx/conf.d",
"local:/etc/nginx",
"/var/log/client-proxy:/var/log/nginx"
]
}

template {
left_delimiter = "[["
right_delimiter = "]]"
destination = "local/load-balancer.conf"
data = var.load_balancer_conf
destination = "local/conf.d/load-balancer.conf"
change_mode = "signal"
change_signal = "SIGHUP"
data = <<EOF
map $http_upgrade $conn_upgrade {
default "";
"websocket" "Upgrade";
}
log_format logger-json escape=json
'{'
'"source": "client-proxy",'
'"time": "$time_iso8601",'
'"resp_body_size": $body_bytes_sent,'
'"host": "$http_host",'
'"address": "$remote_addr",'
'"request_length": $request_length,'
'"method": "$request_method",'
'"uri": "$request_uri",'
'"status": $status,'
'"user_agent": "$http_user_agent",'
'"resp_time": $request_time,'
'"upstream_addr": "$upstream_addr"'
'}';
access_log /var/log/nginx/access.log logger-json;
server {
listen 3002 default_server;
server_name _;
return 502 "Cannot resolve domain";
}
[[ range service "session-proxy" ]]
server {
listen 3002;
access_log /var/log/nginx/access.log logger-json;
server_name ~^(.+)-[[ index .ServiceMeta "Client" | sprig_substr 0 8 ]]\.${local.domain_name_escaped}$;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $conn_upgrade;
proxy_hide_header x-frame-options;
proxy_http_version 1.1;
client_body_timeout 86400s;
client_header_timeout 5s;
proxy_read_timeout 600s;
proxy_send_timeout 86400s;
proxy_cache_bypass 1;
proxy_no_cache 1;
client_max_body_size 1024m;
proxy_buffering off;
proxy_request_buffering off;
tcp_nodelay on;
tcp_nopush on;
sendfile on;
send_timeout 600s;
proxy_connect_timeout 30s;
keepalive_requests 65536;
keepalive_timeout 600s;
# TODO: Fix the config file so we can defined this
# keepalive_time: 86400s;
gzip off;
location / {
proxy_pass $scheme://[[ .Address ]]:[[ .Port ]]$request_uri;
}
}
[[ end ]]
server {
listen 3001;
location /health {
access_log off;
add_header 'Content-Type' 'application/json';
return 200 '{"status":"UP"}';
}
location /status {
access_log off;
stub_status;
allow all;
}
}
EOF
}


template {
left_delimiter = "[["
right_delimiter = "]]"
data = var.nginx_conf
destination = "local/nginx.conf"
change_mode = "signal"
change_signal = "SIGHUP"
data = <<EOF
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
keepalive_time 86400s;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
EOF
}
}
}
}
4 changes: 4 additions & 0 deletions packages/nomad/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ resource "nomad_job" "client_proxy" {
client_proxy_health_port_path = var.client_proxy_health_port.path
session_proxy_service_name = var.session_proxy_service_name
domain_name = var.domain_name
load_balancer_conf = file("${path.module}/proxies/client.conf")
nginx_conf = file("${path.module}/proxies/nginx.conf")
}
}
}
Expand All @@ -125,6 +127,8 @@ resource "nomad_job" "session_proxy" {
session_proxy_port_number = var.session_proxy_port.port
session_proxy_port_name = var.session_proxy_port.name
session_proxy_service_name = var.session_proxy_service_name
load_balancer_conf = file("${path.module}/proxies/session.conf")
nginx_conf = file("${path.module}/proxies/nginx.conf")
}
}
}
Expand Down
91 changes: 91 additions & 0 deletions packages/nomad/proxies/client.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
map $http_upgrade $conn_upgrade {
default "";
"websocket" "Upgrade";
}

log_format logger-json escape=json
'{'
'"source": "client-proxy",'
'"time": "$time_iso8601",'
'"resp_body_size": $body_bytes_sent,'
'"host": "$http_host",'
'"address": "$remote_addr",'
'"request_length": $request_length,'
'"method": "$request_method",'
'"uri": "$request_uri",'
'"status": $status,'
'"user_agent": "$http_user_agent",'
'"resp_time": $request_time,'
'"upstream_addr": "$upstream_addr"'
'}';
access_log /var/log/nginx/access.log logger-json;

server {
listen 3002 default_server;

server_name _;
return 502 "Cannot resolve domain";
}
[[ range service "session-proxy" ]]
server {
listen 3002;
access_log /var/log/nginx/access.log logger-json;

server_name ~^(.+)-[[ index .ServiceMeta "Client" | sprig_substr 0 8 ]]\.${local.domain_name_escaped}$;

proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;

proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $conn_upgrade;

proxy_hide_header x-frame-options;

proxy_http_version 1.1;

client_body_timeout 86400s;
client_header_timeout 5s;

proxy_read_timeout 600s;
proxy_send_timeout 86400s;

proxy_cache_bypass 1;
proxy_no_cache 1;

client_max_body_size 1024m;

proxy_buffering off;
proxy_request_buffering off;

tcp_nodelay on;
tcp_nopush on;
sendfile on;

send_timeout 600s;

proxy_connect_timeout 30s;

keepalive_requests 65536;
keepalive_timeout 600s;

gzip off;

location / {
proxy_pass $scheme://[[ .Address ]]:[[ .Port ]]$request_uri;
}
}
[[ end ]]
server {
listen 3001;
location /health {
access_log off;
add_header 'Content-Type' 'application/json';
return 200 '{"status":"UP"}';
}

location /status {
access_log off;
stub_status;
allow all;
}
}
33 changes: 33 additions & 0 deletions packages/nomad/proxies/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
user nginx;
worker_processes auto;

error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;


events {
worker_connections 1024;
}


http {
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main;

sendfile on;
#tcp_nopush on;

keepalive_timeout 65;
keepalive_time 86400s;

#gzip on;

include /etc/nginx/conf.d/*.conf;
}


Loading

0 comments on commit 954d8b2

Please sign in to comment.