Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx status endpoint running inside Docker

I'm new to Nginx, which I'm running in a Docker container to serve a simple website. I want to add an /health endpoint that simply returns status 200 + some arbitrary content.

I copied and adjusted the standard nginx.conf from /etc/nginx/ by adding

server {
    location /health {
        return 200 "alive";
    }
}

at the bottom inside the http block. But when I run the Docker, and try to access localhost/health, I just get no such file or directory. Accessing the website at localhost works fine.

I also tried copying other code blocks, e.g., this one: https://gist.github.com/dhrrgn/8650077 But then I get conflicting server name "" on 0.0.0.0:80, ignored nginx: [warn] conflicting server name "" on 0.0.0.0:80, ignored.

Am I placing the location at a wrong location inside nginx.conf? Do I need some special server configuration? What's the problem?

like image 794
CGFoX Avatar asked May 11 '18 15:05

CGFoX


People also ask

Should nginx run inside Docker?

If nginx is running in a container then your site is going to be 100% dead to the world while Docker isn't running. Users will get a connection error. When nginx is installed directly on your host you can serve a 503 maintenance page that doesn't depend on Docker or any containers running.

How do I check my nginx status?

Through a simple command you can verify the status of the Nginx configuration file: $ sudo systemctl config nginx The output will show if the configuration file is correct or, if it is not, it will show the file and the line where the problem is.

Where is nginx config file in Docker?

By default, the configuration file is named nginx. conf and placed in the directory /usr/local/nginx/conf, /etc/nginx, or /usr/local/etc/nginx.


1 Answers

The problem was with my Nginx Docker setup/configuration: I am using nginx:alpine, which has the configuration files at /etc/nginx/conf.d/. There, default.conf defines the default configuration of Nginx. So, I had to remove default.conf and copy my configuration there instead. In the Dockerfile:

COPY nginx.conf /etc/nginx/conf.d/nginx.conf
RUN rm /etc/nginx/conf.d/default.conf

Of course, I also had to define the standard route in nginx.conf then:

server {
    location / {
        root /usr/share/nginx/html;
    }

    location /health {
        return 200 'alive';
        add_header Content-Type text/plain;
    }
}
like image 186
CGFoX Avatar answered Sep 24 '22 07:09

CGFoX