Skip to content

Commit 8e496f6

Browse files
authored
Use envsubst in our NGINX plugin (#1435)
## Summary NGINX does not support using environment variables in it's configuration file, which makes it difficult for our plugin to configure NGINX to run in a local devbox shell. This update to the NGINX plugin installs envsubst, and then uses it to generate a valid `nginx.conf` from an `nginx.template` file. Developers can further customize their nginx.conf by adding environment variables to the `nginx.template` file. Envsubst will generate the nginx.conf whenever `devbox services up` or `devbox service start` is run. ## How was it tested? Using the nginx example: 1. Change the NGINX_WEB_PORT variable 2. Run `devbox services up` 3. NGINX should generate a valid nginx.conf in the `devbox.d/nginx` folder, and run on the NGINX_WEB_PORT
1 parent c796a47 commit 8e496f6

File tree

10 files changed

+122
-63
lines changed

10 files changed

+122
-63
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
events {}
2+
http{
3+
server {
4+
listen $NGINX_WEB_PORT;
5+
listen [::]:$NGINX_WEB_PORT;
6+
server_name $NGINX_WEB_SERVER_NAME;
7+
root $NGINX_WEB_ROOT;
8+
9+
error_log error.log error;
10+
access_log access.log;
11+
client_body_temp_path temp/client_body;
12+
proxy_temp_path temp/proxy;
13+
fastcgi_temp_path temp/fastcgi;
14+
uwsgi_temp_path temp/uwsgi;
15+
scgi_temp_path temp/scgi;
16+
17+
index index.html;
18+
server_tokens off;
19+
}
20+
}

examples/servers/nginx/devbox.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,5 @@
22
"packages": [
33
"nginx@latest"
44
],
5-
"shell": {
6-
"init_hook": null
7-
}
5+
"shell": {}
86
}

examples/servers/nginx/devbox.lock

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
"lockfile_version": "1",
33
"packages": {
44
"nginx@latest": {
5-
"last_modified": "2023-05-01T16:53:22Z",
6-
"plugin_version": "0.0.1",
7-
"resolved": "github:NixOS/nixpkgs/8670e496ffd093b60e74e7fa53526aa5920d09eb#nginx",
5+
"last_modified": "2023-08-30T00:25:28Z",
6+
"plugin_version": "0.0.3",
7+
"resolved": "github:NixOS/nixpkgs/a63a64b593dcf2fe05f7c5d666eb395950f36bc9#nginx",
8+
"source": "devbox-search",
89
"version": "1.24.0"
910
}
1011
}
11-
}
12+
}

examples/stacks/lepp-stack/devbox.d/nginx/nginx.conf

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ server {
44
listen 8089;
55
listen [::]:8089;
66
server_name localhost;
7-
root ../../../my_app;
7+
root ../../../devbox.d/web;
88

99
error_log error.log error;
1010
access_log access.log;
@@ -14,17 +14,7 @@ server {
1414
uwsgi_temp_path temp/uwsgi;
1515
scgi_temp_path temp/scgi;
1616

17-
index index.php index.htm index.html;
18-
19-
location / {
20-
try_files $uri $uri/ /index.php$is_args$args;
21-
}
22-
23-
location ~ \.php$ {
24-
include fastcgi.conf;
25-
fastcgi_split_path_info ^(.+\.php)(/.+)$;
26-
fastcgi_pass 127.0.0.1:8082;
27-
fastcgi_index index.php;
28-
}
17+
index index.html;
18+
server_tokens off;
2919
}
3020
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
events {}
2+
http{
3+
server {
4+
listen $NGINX_WEB_PORT;
5+
listen [::]:$NGINX_WEB_PORT;
6+
server_name $NGINX_WEB_SERVER_NAME;
7+
root $NGINX_WEB_ROOT;
8+
9+
error_log error.log error;
10+
access_log access.log;
11+
client_body_temp_path temp/client_body;
12+
proxy_temp_path temp/proxy;
13+
fastcgi_temp_path temp/fastcgi;
14+
uwsgi_temp_path temp/uwsgi;
15+
scgi_temp_path temp/scgi;
16+
17+
index index.html;
18+
server_tokens off;
19+
}
20+
}
Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,35 @@
11
{
2-
"packages": [
3-
4-
"postgresql@14",
5-
6-
"php81Extensions.pgsql@latest",
7-
8-
],
9-
"env": {
10-
"PGPORT": "5433"
11-
},
12-
"shell": {
13-
"scripts": {
14-
"init_db": "initdb",
15-
"create_db": [
16-
"dropdb --if-exists devbox_lepp",
17-
"createdb devbox_lepp",
18-
"psql devbox_lepp < setup_postgres_db.sql"
19-
],
20-
"run_test": [
21-
"mkdir -p /tmp/devbox/lepp",
22-
"export PGHOST=/tmp/devbox/lepp",
23-
"initdb",
24-
"devbox services up -b",
25-
"echo 'sleep 2 second for the postgres server to initialize.' && sleep 2",
26-
"dropdb --if-exists devbox_lepp",
27-
"createdb devbox_lepp",
28-
"psql devbox_lepp < setup_postgres_db.sql",
29-
"curl localhost:8089",
30-
"devbox services stop"
31-
]
32-
}
2+
"packages": [
3+
4+
"postgresql@14",
5+
6+
"php81Extensions.pgsql@latest",
7+
8+
],
9+
"env": {
10+
"NGINX_WEB_PORT": "8089",
11+
"PGPORT": "5433"
12+
},
13+
"shell": {
14+
"scripts": {
15+
"create_db": [
16+
"dropdb --if-exists devbox_lepp",
17+
"createdb devbox_lepp",
18+
"psql devbox_lepp < setup_postgres_db.sql"
19+
],
20+
"init_db": "initdb",
21+
"run_test": [
22+
"mkdir -p /tmp/devbox/lepp",
23+
"export PGHOST=/tmp/devbox/lepp",
24+
"initdb",
25+
"devbox services up -b",
26+
"echo 'sleep 2 second for the postgres server to initialize.' && sleep 2",
27+
"dropdb --if-exists devbox_lepp",
28+
"createdb devbox_lepp",
29+
"psql devbox_lepp < setup_postgres_db.sql",
30+
"curl localhost:$NGINX_WEB_PORT",
31+
"devbox services stop"
32+
]
3333
}
34-
}
34+
}
35+
}

examples/stacks/lepp-stack/devbox.lock

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
"version": "8.0.1"
88
},
99
10-
"last_modified": "2023-05-01T16:53:22Z",
11-
"plugin_version": "0.0.1",
12-
"resolved": "github:NixOS/nixpkgs/8670e496ffd093b60e74e7fa53526aa5920d09eb#nginx",
10+
"last_modified": "2023-07-06T12:20:10Z",
11+
"plugin_version": "0.0.3",
12+
"resolved": "github:NixOS/nixpkgs/5daaa32204e9c46b05cd709218b7ba733d07e80c#nginx",
13+
"source": "devbox-search",
1314
"version": "1.24.0"
1415
},
1516
"php81Extensions.pgsql@latest": {
@@ -19,15 +20,15 @@
1920
},
2021
2122
"last_modified": "2023-05-01T16:53:22Z",
22-
"plugin_version": "0.0.1",
23+
"plugin_version": "0.0.2",
2324
"resolved": "github:NixOS/nixpkgs/8670e496ffd093b60e74e7fa53526aa5920d09eb#php",
2425
"version": "8.1.18"
2526
},
2627
"postgresql@14": {
2728
"last_modified": "2023-05-01T16:53:22Z",
28-
"plugin_version": "0.0.1",
29+
"plugin_version": "0.0.2",
2930
"resolved": "github:NixOS/nixpkgs/8670e496ffd093b60e74e7fa53526aa5920d09eb#postgresql",
3031
"version": "14.7"
3132
}
3233
}
33-
}
34+
}

plugins/nginx.json

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
{
22
"name": "nginx",
3-
"version": "0.0.2",
4-
"readme": "nginx can be configured with env variables\n\nTo customize:\n* Use $NGINX_CONFDIR to change the configuration directory\n* Use $NGINX_LOGDIR to change the log directory\n* Use $NGINX_PIDDIR to change the pid directory\n* Use $NGINX_RUNDIR to change the run directory\n* Use $NGINX_SITESDIR to change the sites directory\n* Use $NGINX_TMPDIR to change the tmp directory. Use $NGINX_USER to change the user\n* Use $NGINX_GROUP to customize.",
3+
"version": "0.0.3",
4+
"readme": "nginx can be configured with env variables\n\nTo customize:\n* Use $NGINX_CONFDIR to change the configuration directory\n* Use $NGINX_TMPDIR to change the tmp directory. Use $NGINX_USER to change the user\n* Use $NGINX_WEB_PORT to change the port NGINX runs on. \n Note: This plugin uses envsubst when running `devbox services` to generate the nginx.conf file from the nginx.template file. To customize the nginx.conf file, edit the nginx.template file.\n",
5+
"__packages": ["envsubst@latest"],
56
"env": {
6-
"NGINX_CONFDIR": "{{ .DevboxDir }}/nginx.conf",
7+
"NGINX_CONF": "{{ .DevboxDir }}/nginx.conf",
8+
"NGINX_CONFDIR": "{{ .DevboxDir }}",
79
"NGINX_PATH_PREFIX": "{{ .Virtenv }}",
8-
"NGINX_TMPDIR": "{{ .Virtenv }}/temp"
10+
"NGINX_TMPDIR": "{{ .Virtenv }}/temp",
11+
"NGINX_WEB_PORT": "8081",
12+
"NGINX_WEB_ROOT": "../../../devbox.d/web",
13+
"NGINX_WEB_SERVER_NAME": "localhost"
914
},
1015
"create_files": {
1116
"{{ .Virtenv }}/temp": "",
1217
"{{ .Virtenv }}/process-compose.yaml": "nginx/process-compose.yaml",
18+
"{{ .DevboxDir }}/nginx.template": "nginx/nginx.template",
1319
"{{ .DevboxDir }}/nginx.conf": "nginx/nginx.conf",
1420
"{{ .DevboxDir }}/fastcgi.conf": "nginx/fastcgi.conf",
1521
"{{ .DevboxDirRoot }}/web/index.html": "web/index.html"

plugins/nginx/nginx.template

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
events {}
2+
http{
3+
server {
4+
listen $NGINX_WEB_PORT;
5+
listen [::]:$NGINX_WEB_PORT;
6+
server_name $NGINX_WEB_SERVER_NAME;
7+
root $NGINX_WEB_ROOT;
8+
9+
error_log error.log error;
10+
access_log access.log;
11+
client_body_temp_path temp/client_body;
12+
proxy_temp_path temp/proxy;
13+
fastcgi_temp_path temp/fastcgi;
14+
uwsgi_temp_path temp/uwsgi;
15+
scgi_temp_path temp/scgi;
16+
17+
index index.html;
18+
server_tokens off;
19+
}
20+
}

plugins/nginx/process-compose.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ version: "0.5"
22

33
processes:
44
nginx:
5-
command: "nginx -p $NGINX_PATH_PREFIX -c $NGINX_CONFDIR -e error.log -g \"pid nginx.pid;daemon off;\""
5+
command: |
6+
if [ -f $NGINX_CONFDIR/nginx.template ]; then envsubst < $NGINX_CONFDIR/nginx.template > $NGINX_CONFDIR/nginx.conf; fi
7+
nginx -p $NGINX_PATH_PREFIX -c $NGINX_CONFDIR/nginx.conf -e error.log -g "pid nginx.pid;daemon off;"
68
availability:
79
restart: on_failure
810
max_restarts: 5

0 commit comments

Comments
 (0)