PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, September 2, 2022

[FIXED] How to make ngnix docker containers accessible through proxy?

 September 02, 2022     docker, docker-compose, nginx, nginx-reverse-proxy     No comments   

Issue

Let's say I have three docker containers with nginx. Their exposed port is mapped to 8080, 8181 and 8282 respectively. I want to configure the server on 8080 that it proxies /example01 and /example02 to the other two applications. This is my config file:

server {

    listen 80;
    server_name  localhost;

    location / {
        root    /usr/share/nginx/html/;
        index   index.html index.htm;
    }

    location /example01 {
        proxy_pass http://localhost:8181/;
    }

    location /example02 {
        proxy_pass http://localhost:8282/;
    }
}

So if I run the containers, each of the applications are accessible (http://localhost:8080, http://localhost:8181 and http://localhost:8282).

Now, I don't really get why http://localhost:8080/example01 and http://localhost:8080/example02 are not redirecting correctly. Instead I get a 502 bad gateway error. Does it have something to do with my exposed ports and the VIRTUAL_PORT?

Thanks in advance.


Solution

This is because of the container network scope. Those container's localhost's are inside each container respectively - and that's not where your ports are mapped to. Instead do:

$ ifconfig

on your host machine and find your local ip address and proxy the traffic to your host - that has the ports mapped.

conf:

server {

    listen 80;
    server_name  localhost;

    location / {
        root    /usr/share/nginx/html/;
        index   index.html index.htm;
    }

    location /example01 {
        proxy_pass http://192.168.1.2:8181/;
    }

    location /example02 {
        proxy_pass http://192.168.1.2:8282/;
    }
}

where 192.168.1.2 is your own machine local ip address.

Another way would be to link those containers and not proxy via localhost - but the alias you'd provide in the link definition. I can elaborate if you choose this method.

-- edit with the linking method --

In order to have your services linked, you need to use a docker tool docker-compose. Assuming you're familiar with what it is ( doc refs on the bottom ), you could write a compose file like this:

first-nginx:
   build: first-nginx/
   ports:
      - 8080:80
   links:
      - second-nginx
      - third-nginx

second-nginx:
   build: second-nginx/
   ports:
      - 8081:80

third-nginx:
   build: third-nginx/
   ports:
      - 8082:80

Placed in your project's root directory like this:

root
  - first-nginx
    + nginx.conf
    + Dockerfile
    + some-other.files
  - second-nginx
    + some.files
    + another-nginx.conf
    + Dockerfile
  - third-nginx
    + some-nginx.conf
    + Dockerfile
  + docker-compose.yml

And you'd configure the "main" nginx to utilize the created links like so:

conf:

server {

    listen 80;

    location / {
        root    /usr/share/nginx/html/;
        index   index.html index.htm;
    }

    location /example01 {
        proxy_pass http://second-nginx/;
    }

    location /example02 {
        proxy_pass http://third-nginx/;
    }
}

Be sure to ask if anything is unclear.

links ref

compose ref



Answered By - trust512
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing