Nginx – error 503 when using nginx-proxy container like a front-end server to access multiple container on the same host

dockerdocker-composenginxreverse-proxy

I want to be able to have access to multiple containers, on the same host, containing web applications.

When I want to access the host (by IP address) or the containers (e.g. by host_ip_adress/container1), I get for both a 503 error from nginx. What I want is to access my container1 by ip_addrress_host/container1.

The solution I found on internet was to set an nginx-proxy front-end server (source: https://blog.florianlopes.io/host-multiple-websites-on-single-host-docker/)

My docker-compose file :
docker-compose.yml

version: '2'
services:
  nginx-proxy:
    image: jwilder/nginx-proxy
    ports:
      - "80:80"
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock
  site_a:
   image: php:7.0-apache
   expose:
   - "80"
   environment:
   - VIRTUAL_HOST=container1
  volumes:
   - ./php:/var/www/html
 site_b:
   image: php:7.0-apache
   expose:
   - "80"
   environment:
   - VIRTUAL_HOST=container2
   volumes:
   - ./php:/var/www/html

I run it with the command :

docker-compose up

My entries in the /etc/hosts file :

127.0.1.1       container1
127.0.0.1       container2

The logs I see when I make a request from the outside :

nginx-proxy_1  | nginx.1    | 192.168.12.28 192.168.12.82 - - [25/Oct/2017:09:46:42 +0000] "GET /container1 HTTP/1.1" 503 615 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"
nginx-proxy_1  | nginx.1    | 192.168.12.28 192.168.12.82 - - [25/Oct/2017:09:46:42 +0000] "GET /favicon.ico HTTP/1.1" 503 615 "http://192.168.12.28/container1" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"

Thank you for your help, and sorry for my bad English! 🙂

Edit :

Here are my logs for the nginx-proxy container at the start :

forego     | starting dockergen.1 on port 5000
forego     | starting nginx.1 on port 5100
dockergen.1 | 2017/10/25 14:01:53 Generated '/etc/nginx/conf.d/default.conf' from 3 containers
dockergen.1 | 2017/10/25 14:01:53 Running 'nginx -s reload'
nginx.1    | 2017/10/25 14:01:54 [warn] 30#30: server name "192.168.12.28/container1" has suspicious symbols in /etc/nginx/conf.d/default.conf:60
nginx.1    | 2017/10/25 14:01:54 [warn] 30#30: server name "192.168.12.28/container2" has suspicious symbols in /etc/nginx/conf.d/default.conf:74
dockergen.1 | 2017/10/25 14:01:54 Watching docker events
dockergen.1 | 2017/10/25 14:01:54 Contents of /etc/nginx/conf.d/default.conf did not change. Skipping notification 'nginx -s reload'

Edit2 : I tried to "custumize" nginx-proxy with the configuration file give by Paweł Tatarczuk (https://serverfault.com/a/880384/441157)

Now, when I do a request llike http://192.168.12.28/container1 I have got this log :

nginx-proxy_1  | nginx.1    | 2017/10/26 08:46:19 [error] 41#41: *1 open() "/etc/nginx/html/container1" failed (2: No such file or directory), client: 192.168.12.82, server: 192.168.12.28, request: "GET /container1 HTTP/1.1", host: "192.168.12.28"

Edit 3 : Add the ? to the rewrite

nginx-proxy_1  | nginx.1    | 2017/10/26 09:11:00 [error] 31#31: *1 container1 could not be resolved (2: Server failure), client: 192.168.12.82, server: 192.168.12.28, request: "GET /container1 HTTP/1.1", host: "192.168.12.28"

Best Answer

What I want is to access my container1 by ip_addrress_host/container1.

Are you sure you want to access container with e.g. http://127.0.0.1/container1? Then jwilder/nginx-proxy is not the best approach.

Your proxy is listening locally on port 80, it will proxy request to container1 and container2 but it won't proxy paths /container1 and /container2.

Curl

curl -H "Host: container1" localhost

Browser

Open up http://container1


You can attach a custom config that will take care of proxying paths to make ip_address/container-name work:

  1. Add this to volumes: ./custom.conf:/etc/nginx/conf.d/custom.conf
  2. Create custom.conf next to docker-compose.yml:

    server {
        server_name 192.168.X.Y;
        listen 80;
    
        location ~ ^/([^/]+)(/.*)? {
            proxy_pass http://$1$2;
        }
    }
    
  3. Link containers

    nginx-proxy:
      ...
      links:
        - site_a:container1
        - site_b:container2
    

This is just for start, you should improve this to match your needs. It should work with http://192.168.X.Y/container1 or http://192.168.X.Y/container2.

Please note that there is a rewrite, so http://192.168.X.Y/container1/some/path is proxied to http://container1/some/path. I've assumed that you don't want requests with /container1 prefix to your destination conatiner.

Related Topic