Jacopo Sciampi
Jacopo Sciampi

Reputation: 3432

Docker, nginx, nginx proxy not redirecting to the right container

I'm setting up some containers in my Ubuntu server. I've created two simple images this way:

Dockerfile: static-site

FROM nginx:alpine
COPY ./site/ /usr/share/nginx/html

Dockerfile: static-content

FROM nginx:alpine
COPY ./assets/ /usr/share/nginx/html

The Dockerfiles are in different location

Until here no problem at all. I've installed nginx-proxy and used the VIRTUAL_HOST to run them:

 docker run -d -p 80 -e VIRTUAL_HOST=mysite.com static-site
 docker run -d -p 80 -e VIRTUAL_HOST=static.mysite.com static-content

The result is that whatever address I put in the browser it always redirect me to mysite.com.

What am I doing wrong?

Also, I have a DNS record like this:

*.mysite.com    86400   A   0   xxx.xxx.xx.xx (the ip of mysite.com)

Could it be the problem?

Upvotes: 0

Views: 806

Answers (1)

Al-waleed Shihadeh
Al-waleed Shihadeh

Reputation: 2855

you cant bind two containers to the same port ("80"). Most probably that the second container is dead (you can verify this by running docker ps). or it is running with automatically assigned ports

docker ps --format " {{.Image}} ==> {{.Ports}}"
nginx ==> 0.0.0.0:32769->80/tcp
nginx ==> 0.0.0.0:32768->80/tcp

To fix this issue you either use different ports for the containers and configure your DNS to be linked to a load balancer (so you can configure the destination port) or you switch to use single Nginx with multiple server definitions.

Dockerfile:

FROM nginx:alpine
COPY ./site/ /usr/share/nginx/site_html
COPY ./assets/ /usr/share/nginx/assets_html
COPY ./site.conf /etc/nginx/conf.d/default.conf

Nginx Config:

server {
    listen 80;
    server_name  mysite.com;

    root /usr/share/nginx/site_html;
}

server {
    listen 80;
    server_name  static.mysite.com;

    root /usr/share/nginx/static_html;
}

Upvotes: 1

Related Questions