I'm confused by how I should manage reverse proxy (nginx) with multiple independent webapps on the same host. I know that i can use https://github.com/jwilder/nginx-proxy and configure VIRTUAL_HOST per app but then I wont be able to have nginx visible as a service in every app docker-compose.yml.
I want to do it this way because I want to clearly define all services needed to run app in production and replicate it easy in development.
In other words: I have two webapps that I need to run on the same host and I want to define nginx as a service dependency in docker-compose.yml in both apps but share that service with both as only one nginx can forward port 80.
It's ok to have multiple processes, but to get the most benefit out of Docker, avoid one container being responsible for multiple aspects of your overall application. You can connect multiple containers using user-defined networks and shared volumes.
Maintaining Content and Configuration Files on the Docker Host. Any change made to the files in the local directories /var/www and /var/nginx/conf on the Docker host are reflected in the directories /usr/share/nginx/html and /etc/nginx in the container.
Dockerfile:
FROM ubuntu:14.04
MAINTAINER Test ([email protected])
# install nginx
RUN apt-get update -y
RUN apt-get install -y python-software-properties
RUN apt-get install -y software-properties-common
RUN add-apt-repository -y ppa:nginx/stable
RUN apt-get update -y
RUN apt-get install -y nginx
# deamon mode off
RUN echo "\ndaemon off;" >> /etc/nginx/nginx.conf
RUN chown -R www-data:www-data /var/lib/nginx
# volume
VOLUME ["/etc/nginx/sites-enabled", "/etc/nginx/certs", "/var/log/nginx"]
# expose ports
EXPOSE 80 443
# add nginx conf
ADD nginx.conf /etc/nginx/conf.d/default.conf
WORKDIR /etc/nginx
CMD ["nginx"]
nginx.conf:
server {
listen 80;
server_name test1.com www.test1.com;
location / {
proxy_pass http://web1:81/;
}
}
server {
listen 80;
server_name test2.com www.test2.com;
location / {
proxy_pass http://web1:82/;
}
}
** where web1 and web2 - container names
docker-compose.yml:
version: "2"
services:
web1:
image: your_image
container_name: web1
ports:
- 81:80
web2:
image: your_image
container_name: web2
ports:
- 82:80
nginx:
build: .
container_name: nginx
ports:
- 80:80
- 443:443
links:
- web1
- web2
How to run:
docker-compose up -d
When you call test1.com - nginx forwards your request to container web1:81, when test2.com - to container web2:82
P.S.: your question was about NGINX-reverse-proxy. But better and easier do this with TRAEFIK https://traefik.io
You should also be able to open two ports in the same container
services:
web:
image: your_image
container_name: web
ports:
- 8080:80
- 8081:81
And the add new config file for second app in nginx site-enabled (or conf.d) that will listen 81 port.
First App
server {
listen 80;
server_name localhost;
root /app/first;
}
Second App
server {
listen 81;
server_name localhost;
root /app/second;
}
So that:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With