nginx proxy_pass to a linked docker container nginx proxy_pass to a linked docker container nginx nginx

nginx proxy_pass to a linked docker container


Use an upstream block instead of the container name directly

upstream backend {    server container1;}server {    location ~ ^/some_url/(.*)$ {        proxy_pass http://backend/$1;    }}

This should allow normal name resolution to occur providing a way to easily use docker links with nginx.


I believe, Nginx is using its own DNS resolver implementation,

You could use embedded Docker DNS service, if enabled, check your container resolver:

cat /etc/resolv.conf

Should be:

nameserver 127.0.0.11

Use this IP as resolver:

server {    location ~ ^/some_url/(.*)$ {        resolver 127.0.0.11;        proxy_pass http://container1/$1;    } }

There is plenty of Docker image with such hack in the entrypoint:

https://github.com/jetbrains-infra/docker-nginx-resolver

entrypoint.sh:

...echo resolver $(awk 'BEGIN{ORS=" "} $1=="nameserver" {print $2}' /etc/resolv.conf) ";" > /etc/nginx/includes/resolver.conf...

nginx.conf:

http {    include       /etc/nginx/includes/resolver.conf;....


You should take a look at this answer about using /etc/hosts as your resolver: Using /etc/hosts as resolver for url rewriting

Basically, your dns or resolver does not use /etc/hosts to resolve names during a lookup, but you can work around this by installing dnsmasq and using 127.0.0.1 as your resolver. You can add 127.0.0.1 as the resolver directly in your nginx config:

server {    location ~ ^/some_url/(.*)$ {        resolver 127.0.0.1;        proxy_pass http://container1/$1;    }}