简体   繁体   中英

Nginx reverse proxy does unexpected load balancing

// Edit after posting the question
My issue was that all dockers were in the same network because i wanted to dockerise the proxy aswell and he needed to access all docker to proxy the request.
It had nothing to do with PROXY
So i dunno exactly why but the fact that they were in the same docker network even when using the exposed ports it was loadbalacing through all my apps

I'm trying to setup a reverse proxy for local development for my team. We have 3 Laravel Apps running well in docker mapped to different port on host

docker ps results :

a1741218e399 nginx "/docker-entrypoint.…"   5 seconds ago        Up 3 seconds        0.0.0.0:8087->80/tcp   qbo
175b5133fd82 nginx "/docker-entrypoint.…"   8 seconds ago        Up 7 seconds        0.0.0.0:8086->80/tcp   apiv2
56c485f51700 nginx "/docker-entrypoint.…"   41 seconds ago       Up 39 seconds       0.0.0.0:8085->80/tcp   apiv1

All the Hosts are in referenced in /etc/hosts

127.0.0.1 api.lan
127.0.0.1 api2.lan
127.0.0.1 qbo.lan

Nginx is installed on my machine with the folowing configuration

# Api v1
upstream api.lan {

  server localhost:8085;
}

server {

  server_name api.lan;
  listen 80;

  location / {

    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-NginX-Proxy true;

    proxy_pass http://api.lan;
    proxy_redirect off;
  }
}


# Qbo
upstream qbo.lan {

  server localhost:8087;
}

server {

  server_name qbo.lan;
  listen 80;

  location / {

    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-NginX-Proxy true;

    proxy_pass http://qbo.lan;
    proxy_redirect off;
  }
}

# Api v2
upstream api2.lan {

  server localhost:8086;
}

server {

  server_name api2.lan;
  listen 80;

  location / {

    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-NginX-Proxy true;

    proxy_pass http://api2.lan;
    proxy_redirect off;
  }
}

Here is my issue, when hitting anyone of the 3 domains it is load balancing between those 3 apps.

The strangest part is that beside those apps I also got my fronts apps v1 and v2 running well on different port with a similar configuration for proxy without any load balancing problem. If anyone has a suggestion to help me fix this problem

The issue were my dockers which were in the same network. Even if i exposed different ports it was load balacing between those. It had nothing to do with the Proxy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM