1

I'm setting up Nginx using Docker's service discovery. My service name is webAdmin.

Relevant section of the current Nginx config reads

resolver 127.0.0.11 valid=10s;  # Docker DNS server
if (!-f $request_filename) {
    set $upstream_admin_server webAdmin:8000;
    proxy_pass http://$upstream_admin_server;
    break;
}

When visiting the appropriate server, Nginx returns a 404. The logs reveal that Nginx is attempting to resolve a lower case version of my service name.

2019/08/26 21:53:46 [error] 3756#3756: *1569 webadmin could not be resolved (3: Host not found), client: 10.0.0.29, server: admin.mysite.com, request: "GET /favicon.ico HTTP/1.1", host: "admin.mysite.com"

When I avoid using a variable the config reads

resolver 127.0.0.11 valid=10s;  # Docker DNS server
if (!-f $request_filename) {
    proxy_pass http://webAdmin:8000;
    break;
}

Nginx is then able to resolve the service name and correctly route my request.

I attempted to use quotes, single and double but neither seem to have any effect. The Nginx docs for set don't seem to offer any clues.

Why is my variable being converted to lower case?

derek
  • 61
  • 4

2 Answers2

1

When Nginx attempts to resolve a name it actually forces the name to lower case. Source can be found here.

I assume this decision was made with the knowledge that DNS names are supposed to be "case insensitive". But it results in inconsistent behavior between an explicitly declared resolver and the default resolver.

For now it seems that the best option is to avoid the use of capitalization in service names. (ie. webAdmin -> web_admin)

Thanks to Adiii for the guidance!

derek
  • 61
  • 4
0

Its working fine with me using nginx:alpine. I test it using the below configuration and changing the value for LocalhOst.

server {
    listen       80;
    server_name  localhost;
location / {
       set $upstream_admin_server LocalhOst:80;
       proxy_set_header   X-Forwarded-For $remote_addr;
       proxy_set_header   Host $http_host;
       proxy_pass         http://$upstream_admin_server/index.html;

 }
location /index.html {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

You can test it below command.

docker run --rm --name my-custom-nginx-container -p 80:80 -v $PWD/nginx.conf:/etc/nginx/conf.d/default.conf -it nginx:alpine

enter image description here

Adiii
  • 54,482
  • 7
  • 145
  • 148
  • While doing some more digging, I noticed that I can achieve the same results as you when I comment out my resolver. `no resolver defined to resolve webAdmin`. Perhaps the resolver is doing the coercion? – derek Aug 28 '19 at 16:25
  • Yes maybe, I thought its comment. :D – Adiii Aug 28 '19 at 16:29
  • you do not need to define Docker DNS server – Adiii Aug 28 '19 at 17:23
  • 1
    Could you expand on not needing to define the Docker DNS server? I was under the impression that if I used a variable in the `proxy_pass` statement, it was [necessary to define a resolver](https://stackoverflow.com/a/22259088/7129097). I could get away with not using a variable but that would require me to reload Nginx any time I added or removed a service. – derek Aug 28 '19 at 17:45