1

I'm trying to set up a nginx proxy inside docker that will listen for web traffic and then based on virtualhosts forward traffic to differerat jenkins instances also running inside docker containers. To orchestrate this I use docker-compose.

I have managed to:

  • Start up jenkins instances and talk to them directly
  • Start nginx and configure it to act as a proxy for a webserver running on another host

But when I try to let the nginx server route the traffic inside docker I cant get it to work, just getting 502-Bag Gateway errors and the folowing in the nginx logs:

[error] 6#6: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.17.0.1, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8081/", host: "localhost:8080"

Relevant parts of my docker-compose.yml:

httpd:
  build: httpd
  ports:
      - "8080:8080"
  links:
      - "server_jenkins"
      - "clients_jenkins"
  restart: always

server_jenkins:
  image: jenkins
  ports:
      - "8081:8080"
  restart: always

clients_jenkins:
  image: jenkins
  ports:
      - "8082:8080"
  restart: always

The httpd/Dockerfile looks like this:

FROM nginx
RUN rm -rf /etc/nginx
COPY config /etc/nginx/

And relevant parts of nginx.conf looks like this (only proxying one jenkins instance):

http {
    server {
        listen       8080;
        server_name server.build.example.com;
        root /tmp;

        location / {
            proxy_pass   http://127.0.0.1:8081;
        }
    }
    server {
        listen       8080;
        server_name clients.build.example.com;
        root /tmp;

        location / {
            proxy_pass   http://127.0.0.1:8082;
        }
    }
}

I have tried the following variants of the first proxy_pass url (and some other that I don't remember):

When running with --x-networking I changed the link statements to container_name in docker-compose.yml

Versions:

  • docker: 1.9.1, build a34a1d5
  • docker-compose: 1.5.2, build 7240ff3
  • OS: Debian 8 (running inside Virtualbox)
2
  • Please provide clear error messages. "[...] I cant get it to work.[...]" isn't sufficient at all. – gf_ Feb 3 '16 at 16:30
  • Well I get 502-Bad Gateway from the nginx server when I go to localhost:8080 – Mr Shark Feb 4 '16 at 8:21
0

I am deleting my previous answer as I was answering something completely different. I found your problem, its the http://127.0.0.1 and ports which is wrong, 127.0.0.1 inside nginx's conf means the httpd container itself in this case not the host.

If you re-define that to server_jenkins and clients_jenkins and also suffix the 8080 port for both of those, then it should work.

server {
    listen       8080;
    server_name server.build.example.com;
    root /tmp;

    location / {
        proxy_pass   http://server_jenkins:8080;
    }
}
server {
    listen       8080;
    server_name clients.build.example.com;
    root /tmp;

    location / {
        proxy_pass   http://clients_jenkins:8080;
    }
}

Also, I noticed that the http directive causes errors, but as you mentioned the file you have provided can be just a snippet.

1
  • Somewhat, the actual server configs will bee more complex (I'll update the orig-post) – Mr Shark Feb 4 '16 at 8:15
0

Found the problem. I was confused over how portmapping works.

The working config: docker-compose.yml:

httpd:
  build: httpd
  ports:
      - "8080:8080"
  links:
      - "server_jenkins"
      - "clients_jenkins"
  restart: always

server_jenkins:
  image: jenkins
  restart: always

clients_jenkins:
  image: jenkins
  restart: always

nginx.conf:

http {
    server {
        listen       8080;
        server_name server.build.example.com;
        root /tmp;

        location / {
            proxy_pass   http://server_jenkins:8080;
        }
    }
    server {
        listen       8080;
        server_name clients.build.example.com;
        root /tmp;

        location / {
            proxy_pass   http://client_jenkins:8080;
        }
    }
}

Ie. There are no need for mapping the ports.

1
  • 1
    no need for an upstream{} block? – gdbj May 4 '17 at 20:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.