6

I am using the Nginx container to host a SPA application in Kubernetes.

Aside from the static files hosted for the SPA app, I also need to host the routes for health checks. So, the route /health/startup needs to return the text healthy when a GET request is sent to it.

I suppose I could just make a folder called "health" and then put a file in it called startup with the text healthy in it. But that seems a bit odd to me. And I worry that if the file structure may get changed and that then my health checks will start failing.

Is there a way, using the Nginx container to return a given value (ie "healthy")when a request comes to a specific route? (And not mess up the rest of my static file serving that is going on.)

Vaccano
  • 78,325
  • 149
  • 468
  • 850

1 Answers1

10

Yes you can, in the configuration you are importing for the server, you can just do this:

location /health/startup {
    add_header Content-Type text/plain;
    return 200 'healthy';
}
Alberto Sinigaglia
  • 12,097
  • 2
  • 20
  • 48
  • This looks perfect! However, I don't know where the configuration file you refer to is located. (The container image just works for static files dropped into `/usr/share/nginx/html`.) Do you know where the configuration file is usually found? – Vaccano Jul 08 '21 at 00:31
  • it should be on `/etc/nginx/conf.d/app.conf` – Alberto Sinigaglia Jul 08 '21 at 00:33
  • also from the link you posted, under `Complex configuration` I see that the location might be `/etc/nginx/nginx.conf` (might also happen that you need a surrounding `server { ... }` to the snipped i've posted... depends on the version you are using) – Alberto Sinigaglia Jul 08 '21 at 00:35
  • Looks like `/etc/nginx/nginx.conf` is the "real" one. But, by default, it has this line at the bottom: `include /etc/nginx/conf.d/*.conf;` That makes `/etc/nginx/conf.d/default.conf` also get loaded. It also means that any `.conf` file in that folder will get loaded. (Source: https://stackoverflow.com/a/22164038/16241) I will add a file to that location for my health checks. Thank you so much for your help! – Vaccano Jul 08 '21 at 00:45
  • @Vaccano never dug that deep about Nginx config, that's new also for me, hope it works ahah good luck with K8s – Alberto Sinigaglia Jul 08 '21 at 00:48
  • Per site configurations can be in `/etc/nginx/sites-available` ... – nooblag May 24 '23 at 00:31
  • `add_header Content-Type text/plain;` is not necessary – Mechanic Aug 20 '23 at 10:58