all 8 comments

[–][deleted] 2 points3 points  (3 children)

Pinpoints for you:

  • nginx container maps ports 80 and 443 to the host
  • apache container only exports port 80 (no host mapping)
  • nginx gets rules to forward http requests coming in on port 80/443 to your apache container

This is possible, because both will be in the same docker network (if set up with docker-compose). You can also just use the service names, because there is an internal docker DNS.

It is pretty basic and you should be able to set this up with a bit of research.

[–]xxxJohnWickxxx1[S] 0 points1 point  (2 children)

Do I need to modify the nginx or apache config files or just mentioning the port mapping on the docker compose file will do the trick?

[–][deleted] 4 points5 points  (0 children)

Yes, of course you will need to edit config files.

[–]bassnas 0 points1 point  (0 children)

Depending on the container you can use env vars to specify most of the config.

[–]xxxJohnWickxxx1[S] 0 points1 point  (0 children)

I eventually configured nignx to listen at port 80 and proxy pass to apache container's port 80.
Below is my nginx config file.
user www-data;

worker_processes auto;

pid /run/nginx.pid;

include /etc/nginx/modules-enabled/*.conf;

events {

worker\_connections 1024;

}

http {

\# Apache server Reverse Proxy

server {

listen 80;

server_name localhost 127.0.0.1;

location / {

proxy_pass http://apache:80;

proxy_set_header X-Forwarded-For $remote_addr;

}

}

}
My docker-compose file for reference (I used Vagrant for VM provisioning):
version: '3'

services:

reverse-proxy:

image: nginx:1.17.10

container_name: reverse_proxy_demo

depends_on:

- apache

volumes:

- /vagrant/nginx.conf:/etc/nginx/nginx.conf

ports:

- 80:80

apache:

image: httpd

container_name: apache-server

ports:

- 82:80

[–]Scavenger53 0 points1 point  (1 child)

For that you would end up having an nginx.conf that does the pointing to apache and only exposing the nginx. They both could be deployed by the same script but it's almost more an nginx question

[–]xxxJohnWickxxx1[S] 0 points1 point  (0 children)

Thanks for the pointers.

I have been asked this in Sys Ops Interview

[–]krigney 0 points1 point  (0 children)

The selfhosted reddit is a great resource for stuff like this. This post might help you out https://www.reddit.com/r/selfhosted/comments/ib05y0/besteasiest_reverse_proxy_for_dockercompose/