Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get IP Address Upstream #164

Closed
mjaverto opened this issue May 11, 2015 · 6 comments
Closed

Get IP Address Upstream #164

mjaverto opened this issue May 11, 2015 · 6 comments

Comments

@mjaverto
Copy link

Great project, I hope you can help me with a small piece and I can contribute back for others in our scenario.

We are running self-contained spring boot jars that spin up a Tomcat server on 8080 per container. We need to reverse proxy back to the respective container based on NGINX locations. This project seems to be perfect for what we need but the only issue I run into is getting the container address, {{ $address.IP }}.

The error I get is on the upstream line:

2015/05/11 23:32:09 [emerg] 15#15: unexpected "{" in /etc/nginx/conf.d/my-custom.conf:29

as seen here when DOCKER_CONTAINER is an env correctly set for the container.

{{ range $host, $containers := groupByMulti $ "Env.DOCKER_CONTAINER" "," }}

    upstream {{ $host }} {
        {{ range $container := $containers }}
            {{ range $address := $container.Addresses }}
                # {{$container.Name}}
                server {{ $address.IP }}:8080;
            {{ end }}
        {{ end }}
    }

{{ end }}

I've tried editing many different ways and it seems like {{ $host }} is returning the env variable but I can't seem to get

        {{ range $container := $containers }}
            {{ range $address := $container.Addresses }}
                # {{$container.Name}}
                server {{ $address.IP }}:8080;
            {{ end }}
        {{ end }}

working even if I remove the upstream {{ $host }} {} block.

Essentially I'm trying to get the upstream to look like this:

upstream my-service-name {

    server 172.17.0.7:8080;

}

And so it all makes sense, I have our locations hardcoded which works because the proxy pass is the same as Env.DOCKER_CONTAINER

location /api/v1/sessions {

        proxy_pass         http://my-service-name;
        proxy_redirect     off;
        proxy_pass_request_headers      on;
        proxy_set_header   Host $host;
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Host $server_name;
        proxy_set_header   Origin $http_origin;
}

Thanks in advance.
Docker version 1.6.0, build bdbc177

@mjaverto mjaverto changed the title Get IP Upstream Address Get IP Address Upstream May 11, 2015
@mjaverto
Copy link
Author

I actually think I see smaller problem now is related to:

nginx-proxy/docker-gen#79

for

    {{ range $container := $containers }}
        {{ range $address := $container.Addresses }}
            # {{$container.Name}}
            server {{ $address.IP }}:8080;
        {{ end }}
    {{ end }}

but I'm still trying to understand why I have the error with '{'

2015/05/11 23:32:09 [emerg] 15#15: unexpected "{" in /etc/nginx/conf.d/my-custom.conf:29

when running:

docker run --name=nginx-proxy --privileged=true -d -p 80:80 -v /home/mjaverto/dockerapps/nginx/nginx-proxy/nginx-dynamic.tmpl:/etc/nginx/conf.d/my-custom.conf:ro -v /var/run/docker.sock:/tmp/docker.sock jwilder/nginx-proxy

As when I run docker-gen locally it generates the file correctly.

@md5
Copy link
Contributor

md5 commented May 12, 2015

The problem is that the files under conf.d are read directly by Nginx. They are not processed as templates. There is an open issue requesting the ability to include custom templates in #71. I believe that changes would be needed in docker-gen to support it.

As for nginx-proxy/docker-gen#79, it's not related, though it is something you'd probably want to use for your use case if custom templates actually were supported.

@md5
Copy link
Contributor

md5 commented May 12, 2015

The discussion starting at this comment is also relevant: #106 (comment)

@mjaverto
Copy link
Author

@md5 thanks for clarifying and the help. I was a little confused by that file in the root of the repo but I see now it works with Docker-gen.

@mjaverto
Copy link
Author

So with fresh eyes this morning I figured out my problems for anyone else who may find this.

  1. the template file I was passing into the container on start should have been mounted to /app/nginx.tmpl rather than into the nginx conf /etc/nginx/conf.d/my_proxy.conf dir.
  2. I had to expose some ports on my micro service containers so even though they could accept connections on 8080 without any ports exposed to the host machine, I still had to expose a few ports just to have docker-gen pick up their addresses.

My new docker run command is

docker run --name=nginx-proxy -d -p 80:80 -v /home/user/dockerapps/nginx/nginx-proxy/nginx-dynamic.tmpl:/app/nginx.tmpl -v /var/run/docker.sock:/tmp/docker.sock jwilder/nginx-proxy

This is now working as needed for us and we are able to have multiple nginx locations reverse proxied to the one or many containers.

@md5
Copy link
Contributor

md5 commented May 12, 2015

@mjaverto Regarding your second point, I just opened a PR to always expose the container's address: nginx-proxy/docker-gen#86

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants