Skip to content

Commit 639ff11

Browse files
committed
Added nginx
1 parent 9a45482 commit 639ff11

File tree

11 files changed

+432
-4
lines changed

11 files changed

+432
-4
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,20 @@ provider "nomad" {
2020
}
2121
```
2222

23+
24+
## nginx
25+
26+
Web server and reverse proxy, which can be placed on several nodes
27+
28+
* [Documentation](https://nginx.org/en/)
29+
* [Terraform Example](examples/nginx.tf)
30+
* [Nomad Job](nginx/nomad/nginx.hcl)
31+
32+
TODO:
33+
* [ ] In progress
34+
* [ ] Add TLS certificate support
35+
* [ ] Not sure how we can integrate with nomad services
36+
2337
## seaweedfs
2438

2539
Cluster filesystem, which can be spread across multiple nodes.

examples/nginx-default.conf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
server {
2+
listen 80;
3+
listen [::]:80;
4+
server_name localhost;
5+
access_log {{ env "NOMAD_META_alloc_dir" }}/logs/default_access.log main;
6+
7+
location / {
8+
root /usr/share/nginx/html;
9+
index index.html index.htm;
10+
}
11+
}

examples/nginx.tf

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
// Example nginx reverse proxy module
3+
module "nginx" {
4+
source = "github.com/mutablelogic/tf-nomad/grafana"
5+
6+
// Required parameters
7+
dc = "datacenter" // Nomad datacenter for the cluster
8+
9+
// Optional parameters
10+
enabled = true // If false, no-op
11+
namespace = "default" // Nomad namespace for the cluster
12+
docker_tag = "latest" // Pull the latest version of the docker image every job restart
13+
hosts = ["server1", "server2"] // Host constraint for the job, it not specified, deploys on a single host
14+
ports = { // Ports to expose
15+
http = 80
16+
https = 443
17+
}
18+
servers = [ // List of servers to configure
19+
{
20+
name = "default"
21+
data = file("nginx-default.conf")
22+
}
23+
]
24+
}

nginx/config/fastcgi.conf

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
3+
fastcgi_param QUERY_STRING $query_string;
4+
fastcgi_param REQUEST_METHOD $request_method;
5+
fastcgi_param CONTENT_TYPE $content_type;
6+
fastcgi_param CONTENT_LENGTH $content_length;
7+
8+
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
9+
fastcgi_param REQUEST_URI $request_uri;
10+
fastcgi_param DOCUMENT_URI $document_uri;
11+
fastcgi_param DOCUMENT_ROOT $document_root;
12+
fastcgi_param SERVER_PROTOCOL $server_protocol;
13+
fastcgi_param REQUEST_SCHEME $scheme;
14+
fastcgi_param HTTPS $https if_not_empty;
15+
16+
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
17+
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
18+
19+
fastcgi_param REMOTE_ADDR $remote_addr;
20+
fastcgi_param REMOTE_PORT $remote_port;
21+
fastcgi_param SERVER_ADDR $server_addr;
22+
fastcgi_param SERVER_PORT $server_port;
23+
fastcgi_param SERVER_NAME $server_name;
24+
25+
# PHP only, required if PHP was built with --enable-force-cgi-redirect
26+
fastcgi_param REDIRECT_STATUS 200;

nginx/config/mimetypes.conf

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
2+
types {
3+
text/html html htm shtml;
4+
text/css css;
5+
text/xml xml;
6+
image/gif gif;
7+
image/jpeg jpeg jpg;
8+
application/javascript js;
9+
application/atom+xml atom;
10+
application/rss+xml rss;
11+
12+
text/mathml mml;
13+
text/plain txt;
14+
text/vnd.sun.j2me.app-descriptor jad;
15+
text/vnd.wap.wml wml;
16+
text/x-component htc;
17+
18+
image/avif avif;
19+
image/png png;
20+
image/svg+xml svg svgz;
21+
image/tiff tif tiff;
22+
image/vnd.wap.wbmp wbmp;
23+
image/webp webp;
24+
image/x-icon ico;
25+
image/x-jng jng;
26+
image/x-ms-bmp bmp;
27+
28+
font/woff woff;
29+
font/woff2 woff2;
30+
31+
application/java-archive jar war ear;
32+
application/json json;
33+
application/mac-binhex40 hqx;
34+
application/msword doc;
35+
application/pdf pdf;
36+
application/postscript ps eps ai;
37+
application/rtf rtf;
38+
application/vnd.apple.mpegurl m3u8;
39+
application/vnd.google-earth.kml+xml kml;
40+
application/vnd.google-earth.kmz kmz;
41+
application/vnd.ms-excel xls;
42+
application/vnd.ms-fontobject eot;
43+
application/vnd.ms-powerpoint ppt;
44+
application/vnd.oasis.opendocument.graphics odg;
45+
application/vnd.oasis.opendocument.presentation odp;
46+
application/vnd.oasis.opendocument.spreadsheet ods;
47+
application/vnd.oasis.opendocument.text odt;
48+
application/vnd.openxmlformats-officedocument.presentationml.presentation
49+
pptx;
50+
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
51+
xlsx;
52+
application/vnd.openxmlformats-officedocument.wordprocessingml.document
53+
docx;
54+
application/vnd.wap.wmlc wmlc;
55+
application/wasm wasm;
56+
application/x-7z-compressed 7z;
57+
application/x-cocoa cco;
58+
application/x-java-archive-diff jardiff;
59+
application/x-java-jnlp-file jnlp;
60+
application/x-makeself run;
61+
application/x-perl pl pm;
62+
application/x-pilot prc pdb;
63+
application/x-rar-compressed rar;
64+
application/x-redhat-package-manager rpm;
65+
application/x-sea sea;
66+
application/x-shockwave-flash swf;
67+
application/x-stuffit sit;
68+
application/x-tcl tcl tk;
69+
application/x-x509-ca-cert der pem crt;
70+
application/x-xpinstall xpi;
71+
application/xhtml+xml xhtml;
72+
application/xspf+xml xspf;
73+
application/zip zip;
74+
75+
application/octet-stream bin exe dll;
76+
application/octet-stream deb;
77+
application/octet-stream dmg;
78+
application/octet-stream iso img;
79+
application/octet-stream msi msp msm;
80+
81+
audio/midi mid midi kar;
82+
audio/mpeg mp3;
83+
audio/ogg ogg;
84+
audio/x-m4a m4a;
85+
audio/x-realaudio ra;
86+
87+
video/3gpp 3gpp 3gp;
88+
video/mp2t ts;
89+
video/mp4 mp4;
90+
video/mpeg mpeg mpg;
91+
video/quicktime mov;
92+
video/webm webm;
93+
video/x-flv flv;
94+
video/x-m4v m4v;
95+
video/x-mng mng;
96+
video/x-ms-asf asx asf;
97+
video/x-ms-wmv wmv;
98+
video/x-msvideo avi;
99+
}

nginx/config/nginx.conf

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
user nginx;
3+
worker_processes auto;
4+
5+
error_log strerr;
6+
pid {{ env "NOMAD_META_alloc_dir" }}/tmp/nginx.pid;
7+
8+
events {
9+
worker_connections 1024;
10+
}
11+
12+
http {
13+
include {{ env "NOMAD_META_task_dir" }}/config/mimetypes.conf;
14+
default_type application/octet-stream;
15+
16+
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
17+
'$status $body_bytes_sent "$http_referer" '
18+
'"$http_user_agent" "$http_x_forwarded_for"';
19+
20+
access_log {{ env "NOMAD_META_alloc_dir" }}/logs/access.log main;
21+
22+
sendfile on;
23+
keepalive_timeout 65;
24+
gzip on;
25+
26+
include {{ env "NOMAD_META_task_dir" }}/config/conf.d/*.conf;
27+
}

nginx/input.tf

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
variable "dc" {
3+
type = string
4+
description = "Data center name"
5+
}
6+
7+
variable "namespace" {
8+
type = string
9+
description = "Nomad namespace"
10+
default = "default"
11+
}
12+
13+
variable "enabled" {
14+
type = bool
15+
description = "If false, then no job is deployed"
16+
default = true
17+
}
18+
19+
variable "docker_tag" {
20+
type = string
21+
description = "Version of the docker image to use, defaults to latest"
22+
default = "latest"
23+
}
24+
25+
variable "hosts" {
26+
type = list(string)
27+
description = "List of hosts to deploy on, deploys on a single host if empty"
28+
}
29+
30+
variable "ports" {
31+
type = map(number)
32+
description = "Ports to expose"
33+
default = {
34+
http = 80,
35+
https = 443
36+
}
37+
}
38+
39+
variable "servers" {
40+
description = "Servers configuration for nginx"
41+
type = list(object({
42+
name = string
43+
data = string
44+
}))
45+
}

nginx/locals.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
locals {
3+
docker_image = "nginx:${var.docker_tag}"
4+
docker_always_pull = var.docker_tag == "latest" ? true : false
5+
}

nginx/main.tf

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
resource "nomad_job" "nginx" {
3+
count = var.enabled ? 1 : 0
4+
jobspec = file("${path.module}/nomad/nginx.hcl")
5+
6+
hcl2 {
7+
allow_fs = true
8+
vars = {
9+
dc = jsonencode([var.dc])
10+
namespace = var.namespace
11+
docker_image = local.docker_image
12+
docker_always_pull = jsonencode(local.docker_always_pull)
13+
hosts = jsonencode(var.hosts)
14+
ports = jsonencode(var.ports)
15+
config = chomp(file("${path.module}/config/nginx.conf"))
16+
mimetypes = chomp(file("${path.module}/config/mimetypes.conf"))
17+
servers = jsonencode(var.servers)
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)