Nginx not starting when IPv6 is not enabled in hos

2019-08-20 02:00发布

问题:

I am currently configuring a software to be able to handle IPv6 addresses, I have also configure the Nginx docker image so that it can handle both IPv4 and IPv6 address. The problem that I encounter is that I am not sure if the host machine where the software is going to be deployed has IPv6 enabled or not.

The issue is that whenever the IPv6 is not enabled in the host machine it is causing the error below.

nginx: [emerg] socket() [::]:8080 failed (97: Address family not supported by protocol)
2018/01/23 10:53:08 [emerg] 1#1: socket() [::]:8080 failed (97: Address family not supported by protocol)

What I am trying to achieve is that even if the host machine is not IPv6 enabled, the nginx would ignore all IPv6 and simply go to the IPv4. One solution I found is by using the environment variable (envsubsts) that is discussed in the nginx docker repository and use the docker-compose file to set the port and address. However this solution will need a manual changes in docker-compose file depending if IPv6 is enabled or not.

Any help or suggestion will do. THANKS!

Here is the part of my nginx configuration that handles IPv6 and IPv4.

server {

    listen [::]:8080 ssl ipv6only=off;
    server_name localhost;

    ssl on;
    ssl_certificate    /etc/nginx/cert.pem;
    ssl_certificate_key    /etc/nginx/key.pem;

    ...
    ...

    }
}

I have also tried separating the IPv4 and IPv6 but it is still causing the issue.

listen 8080 ssl;
listen [::]:8080 ssl ipv6only=on;

回答1:

The solution that I created is to create two different nginx configuration files.

(1) IPv6 and IPv4 compatible and

(2) IPv4 only

Then I override the Nginx image's default Entrypoint with a shell script that would first run the Nginx using IPv6 and IPv4 configuration, then if the run failed then it would then start Nginx using the IPv4 only configuration file. This way it doesn't matter whether the host machine is IPv6 enabled or not because it would gracefully downgrade to IPv4 only configuration.