scorewinner
scorewinner

Reputation: 11

docker compose nginx proxy not redirecting

I have an Ubuntu server and have built an nginx proxy in one docker container and an nginx webserver in an other container and they are both in the same network and can see each other, but the proxy doesn't redirect to the webserver and I always get this error when accessing my website:

proxy      | 2018/09/05 15:30:27 [alert] 8#8: 1024 worker_connections are not enough
proxy      | 2018/09/05 15:30:27 [error] 8#8: *4086 upstream prematurely closed connection while reading response header from upstream, client: 172.18.0.1, server: , request: "GET / HTTP/1.0", upstream: "http://XX.XX.XX.XX:80/", host: "test.com"

Here are my files:

docker-compose.yml

version: '3'
networks:
    webnet:

services:
  proxy:
    build: ./proxy
    container_name: proxy
    networks:
      - webnet
    ports:
      - 80:80

  website:
    container_name: website
    build: ./nginx
    volumes:
      - ./config/default.conf:/etc/nginx/conf.d/
    networks:
      - webnet

Dockerfile proxy

FROM nginx
RUN rm /etc/nginx/nginx.conf

COPY proxy.conf /etc/nginx/nginx.conf

## proxy.conf

events {
  worker_connections 1024;
}

http {
        server {
                listen 80;
                server_name website;

                location / {
                        proxy_pass http://website;
                }
        }
}
```

Dockerfile nginx webserver

FROM nginx

RUN rm /usr/share/nginx/html/*
COPY test.com /usr/share/nginx/html

## nginx webserver default.conf
server {
    server_name test.com www.test.com;
    root /usr/share/nginx/html;
    index index.php;
}

Upvotes: 0

Views: 2101

Answers (2)

bobmarksie
bobmarksie

Reputation: 3636

Another way to do this is to use a pre-packaged NginX http to https docker image e.g. https://hub.docker.com/r/foxylion/nginx-https-redirect/

A minimal nginx which redirects all requests to the https version.

If for example, you have a micro-service running on secure port 443 then you can use it to redirect all 80 (http traffic) to 443 (https traffic) as follows in your docker-compose.yml file: -

services:

  my-micro-service:
    image: my-micro-service
    container_name: my-micro-service
    ports:
      - "443:443"

  # Redirect http traffic to https
  nginx-redirect:
    image: foxylion/nginx-https-redirect
    ports:
      - "80:80"

As you can see, it's very straightforward and easy.

Upvotes: 0

scorewinner
scorewinner

Reputation: 11

I just realised, I did redirect my proxy container to itself, fixed it now ^^

Dockerfile proxy

server {
  listen 80;
  server_name proxy;

  location / {
      proxy_pass http://website/
  }
}

Dockerfile nginx webserver

server {
    server_name website;
    root /usr/share/nginx/html;
    index index.php;
}

Upvotes: 1

Related Questions