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?
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.
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.
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.
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With