Docker Nginx:How to Prevent "Host Not Found" Error
Development
Problem Description
When using Nginx as a reverse proxy in a Docker container, sometimes you need to start a service first, and then build an image of another service. At this time, if you configure proxy_pass
directly in nginx conf pointing to a service that has not been started, Nginx will fail to start and throw Host Not
Found exception.
The following is a basic configuration of nginx conf
With this configuration, if the some-service
service is not started, Nginx will fail to start.
Below is the adjusted nginx conf
Step 1: Set up the resolver
127.0.0.11 is the DNS service inside Docker, which can resolve the service name to an ipv4 address. This DNS will not change, so there is no problem with hardcoding. Set the cache validity time using valid, pay attention to set ipv6 to off, it seems that nginx will use ipv6 as default, and Docker DNS resolution is ipv4, so there will be a problem of resolution failure.
Step 2: Set the service address as a variable
Use set $something http://some-service:port
to set the variable, and then pass in proxy_pass
as a variable.
Result
With the above modification, Nginx will return an error page when encountering a service that is not started, but will not exit directly. After the service is started, if you visit the page again, it will be available without restarting Nginx.
Tags:
Previous
Next