diff --git a/.github/workflows/pr-nginx-tests.yml b/.github/workflows/pr-nginx-tests.yml index 69505a0..0843e1e 100644 --- a/.github/workflows/pr-nginx-tests.yml +++ b/.github/workflows/pr-nginx-tests.yml @@ -34,6 +34,7 @@ jobs: - examples/1.17 - examples/1.16 - examples/custom + - examples/backends steps: - name: Checkout code diff --git a/builders/nginx.js b/builders/nginx.js index 21b6a6e..fc921bb 100644 --- a/builders/nginx.js +++ b/builders/nginx.js @@ -62,6 +62,7 @@ module.exports = { }, renderArch: 'amd64', renderTemplate: '1.0.6', + backends: [], ssl: false, webroot: '.', }, @@ -108,6 +109,17 @@ module.exports = { ], }; + // Add depends_on for any configured backends + const backends = _.isArray(options.backends) ? options.backends : []; + if (!_.isEmpty(backends)) { + nginx.depends_on = {}; + backends.forEach(backend => { + nginx.depends_on[backend] = {condition: 'service_started'}; + }); + // Also inject backend names as env var for use in templates + nginx.environment.LANDO_NGINX_BACKENDS = backends.join(','); + } + // Send it downstream super(id, options, {services: _.set({}, options.name, nginx)}); } diff --git a/examples/backends/.lando.yml b/examples/backends/.lando.yml new file mode 100644 index 0000000..34d7b77 --- /dev/null +++ b/examples/backends/.lando.yml @@ -0,0 +1,27 @@ +name: lando-nginx-backends +services: + frontend: + type: nginx:1.29 + webroot: . + backends: + - backend1 + - backend2 + build_as_root: + - apt-get update && apt-get install -y curl + config: + vhosts: config/proxy.conf + backend1: + type: nginx:1.29 + webroot: backend1 + build_as_root: + - apt-get update && apt-get install -y curl + backend2: + type: nginx:1.29 + webroot: backend2 + build_as_root: + - apt-get update && apt-get install -y curl + +# This is important because it lets lando know to test against the plugin in this repo +# DO NOT REMOVE THIS! +plugins: + "@lando/nginx": ../.. diff --git a/examples/backends/README.md b/examples/backends/README.md new file mode 100644 index 0000000..04ca33b --- /dev/null +++ b/examples/backends/README.md @@ -0,0 +1,50 @@ +# NGINX Backends Example + +This example exists primarily to test the following documentation: + +* [nginx Service](https://docs.lando.dev/plugins/nginx) + +## Start up tests + +Run the following commands to get up and running with this example. + +```bash +# Should start up successfully +lando poweroff +lando start +``` + +## Verification commands + +Run the following commands to validate things are rolling as they should. + +```bash +# Should serve frontend content from the app root +lando ssh -s frontend -c "curl -s http://localhost" | grep FRONTEND + +# Should be able to reach backend1 through the frontend proxy +lando ssh -s frontend -c "curl -s http://localhost/backend1/" | grep BACKEND1 + +# Should be able to reach backend2 through the frontend proxy +lando ssh -s frontend -c "curl -s http://localhost/backend2/" | grep BACKEND2 + +# Should have LANDO_NGINX_BACKENDS env var set on frontend +lando ssh -s frontend -c "env" | grep LANDO_NGINX_BACKENDS | grep backend1 +lando ssh -s frontend -c "env" | grep LANDO_NGINX_BACKENDS | grep backend2 + +# Should serve backend1 directly on its own port +lando ssh -s backend1 -c "curl -s http://localhost" | grep BACKEND1 + +# Should serve backend2 directly on its own port +lando ssh -s backend2 -c "curl -s http://localhost" | grep BACKEND2 +``` + +## Destroy tests + +Run the following commands to trash this app like nothing ever happened. + +```bash +# Should be destroyed with success +lando destroy -y +lando poweroff +``` diff --git a/examples/backends/backend1/index.html b/examples/backends/backend1/index.html new file mode 100644 index 0000000..e964f4c --- /dev/null +++ b/examples/backends/backend1/index.html @@ -0,0 +1 @@ +BACKEND1 \ No newline at end of file diff --git a/examples/backends/backend2/index.html b/examples/backends/backend2/index.html new file mode 100644 index 0000000..d29dcb9 --- /dev/null +++ b/examples/backends/backend2/index.html @@ -0,0 +1 @@ +BACKEND2 \ No newline at end of file diff --git a/examples/backends/config/proxy.conf b/examples/backends/config/proxy.conf new file mode 100644 index 0000000..239e88f --- /dev/null +++ b/examples/backends/config/proxy.conf @@ -0,0 +1,21 @@ +server { + listen 80; + server_name localhost; + + location /backend1 { + proxy_pass http://backend1:80/; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + } + + location /backend2 { + proxy_pass http://backend2:80/; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + } + + location / { + root "{{LANDO_WEBROOT}}"; + index index.html; + } +} diff --git a/examples/backends/index.html b/examples/backends/index.html new file mode 100644 index 0000000..abeaeff --- /dev/null +++ b/examples/backends/index.html @@ -0,0 +1 @@ +FRONTEND \ No newline at end of file