11

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.)

1 Answer 1

18

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';
}
7
  • 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
    Commented Jul 8, 2021 at 0:31
  • it should be on /etc/nginx/conf.d/app.conf
    – Alberto
    Commented Jul 8, 2021 at 0: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
    Commented Jul 8, 2021 at 0: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: 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
    Commented Jul 8, 2021 at 0:45
  • @Vaccano never dug that deep about Nginx config, that's new also for me, hope it works ahah good luck with K8s
    – Alberto
    Commented Jul 8, 2021 at 0:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.