DEV Community

Cover image for How to set up NGINX Docker Reverse Proxy?

How to set up NGINX Docker Reverse Proxy?

Sukhbir Sekhon on April 25, 2020

Introduction Imagine a time when you set up the local server and created a front-end application based on it. To access the API of an ap...
Collapse
 
alara_joel profile image
Alara Oluwatoyin Joel

Great write Sukhbir, trying to follow it but my situation is quite different.
I have a server that runs Nginx, and I am deploying a CMS (Xibo) on it.
Cms runs by default on port 80, but cannot run because Nginx is listening there.
So to run it I kill the Nginx process.
Note that the cms is running inside of a docker-compose container.

So I am supposed to change its port, then use a reverse proxy to bring it back on port 80, when this works on HTTP, then I can do the securing
Phew!!
You did help understand things better, so i will keep hacking away.

Collapse
 
davbaster profile image
davbaster • Edited

Hi,
I have completed this tutorial, and it helped me a lot, however I needed to do a couple of changes in order to work on my side.

First, I changed the name of the networks, since on my side they were called webservice1_default instead of site1_default. I found the name of the network by executing this docker command: "docker network ls"

dev-to-uploads.s3.amazonaws.com/i/...

Second, I had to found out which were my containers' ip, since the ones listed here are different than mine. I found the ips by inspecting the networks I found before. I used "docker network inspect NameOfYourNetwork" command.

dev-to-uploads.s3.amazonaws.com/i/...

Third, I had to modify my hosts file with the correct ips I found in the previous step.

dev-to-uploads.s3.amazonaws.com/i/...

Finally, I needed to do some modifications in docker-compose.yml file for the proxy, since the identitation was no correct. In the below image you can see the correct identation.

dev-to-uploads.s3.amazonaws.com/i/...

Thanks to the author for taking the time to do this tutorial.

Collapse
 
xkozs01 profile image
xkozs01

Can you please fix the indentation in the yml files? I cannot build the docker-compose.yml for proxy server (the last yml in the article)

Collapse
 
sukhbirsekhon profile image
Sukhbir Sekhon

Try copying the code using ALT instead of CTRL.

Collapse
 
xkozs01 profile image
xkozs01

Thanks for response, but how can I use ALT istead of CTRL? It doesn't work on my Windows notebook. I am pasting it to VS Code. When I format it like this

version: '2'
services:
  proxy:
    build: ./
    networks:
      - site1
      - site2
    ports:
      - "80:80"
      - "443:443"

  networks:
    site1:
      external:
        name: site1_default
    site2:
      external:
        name: site2_default
Enter fullscreen mode Exit fullscreen mode

It gives me an error

ERROR: The Compose file '.\docker-compose.yml' is invalid because:
Unsupported config option for services.networks: 'site2'
Enter fullscreen mode Exit fullscreen mode

What is wrong?

Thread Thread
 
xkozs01 profile image
xkozs01

OK, I got it, but it took me some time to figure it out

Webservice 1

version: "2"
services:
  app:
    image: nginx:1.9
    volumes:
      - .:/usr/share/nginx/html/
    expose:
      - "80"
Enter fullscreen mode Exit fullscreen mode

Webservice 2

version: "2"
services:
  app:
    image: nginx:1.9
    volumes:
      - .:/usr/share/nginx/html/
    expose:
      - "80"
Enter fullscreen mode Exit fullscreen mode

proxy

version: "2"
services:
  proxy:
    build: ./
    networks:
      - site1
      - site2
    ports:
      - "80:80"
      - "443:443"

networks:
  site1:
    external:
      name: site1_default
  site2:
    external:
      name: site2_default
Enter fullscreen mode Exit fullscreen mode
Collapse
 
raulsesaj profile image
RICARDO RAUL JACINTO MONTES

First at all, thanks !!! great tutorial, with some mistakes it's true, but it shows cooperation of all involved. I followed not only this blog, but the instructions from davbaster and it worked well.

Thanks again

Collapse
 
alver23 profile image
Alver Grisales

hi, the implementation i have error

host not found in upstream "site1_app_1" in /etc/nginx/conf.d/default.conf:14

host not found in upstream "site1_app_1" in /etc/nginx/conf.d/default.conf:14

Collapse
 
nicolasgoudard profile image
Nicolas G • Edited

Hello
This tutorial does not work, but I success with another
1- A part is missing ; The site1_default and site2_default networks should be created before,

2- The container names site1_app_1 and site2_app_1 are not defined, because autogenerated by docker : in my Docker version it generates webservice1_app_1 and webservice1_app_2. The best practice is to give a name to containers in the docker-compose.yml file, using the "container_name" keyword .
3- Cannot resolve the container names inside the Nginx container : so the proxy_pass directive dos not work, I think I must configure something in my Docker configuration, but it is not explained in this tutorial
4- Let's encrypt does not work generate the certificates because the server does not exist in the DNS ( pointer A missing)
5- site1.test and site2.test are bad declared in the /etc/hosts file . Normally you should declare in one line, like that : "172.31.30.78 site1.test site2.test"
Best regards

Collapse
 
sibelius profile image
Sibelius Seraphini

do you have a github repo for this?

Collapse
 
sukhbirsekhon profile image
Sukhbir Sekhon

Unfortunately, I don’t. Will keep in mind next time. Thanks!

Collapse
 
1nnikan1 profile image
1nnikan1

can anyone make a video about this please

Collapse
 
1nnikan1 profile image
1nnikan1

ERROR: In file './docker-compose.yml', service must be a mapping, not a NoneType.

can anyone help me with this please

Collapse
 
arvigeus profile image
Nikolay Stoynov

@1nnikan1 I know it's a year late, but if anyone stumbles upon this in the future: see comment above

Collapse
 
dameyerdave profile image
David Meyer

Very helpful article - thanks!
Do you already know about

Image description

smnrp.net