0

the error in terminal:

2023/07/31 09:30:16 [error] 23#23: *3 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.21.1, server: localhost, request: "GET / HTTP/1.1", upstream: "http://192.168.21.3:9002/", host: "192.168.21.4:84"

2023/07/31 09:30:16 [error] 23#23: *3 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.21.1, server: localhost, request: "GET / HTTP/1.1", upstream: "http://192.168.21.2:9003/", host: "192.168.21.4:84"

192.168.21.1 - - [31/Jul/2023:09:30:16 +0000] "GET / HTTP/1.1" 502 559 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36"
2023/07/31 09:30:16 [error] 23#23: *3 no live upstreams while connecting to upstream, client: 192.168.21.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", upstream: "http://php-apps/favicon.ico", host: "192.168.21.4:84", referrer: "http://192.168.21.4:84/"
192.168.21.1 - - [31/Jul/2023:09:30:16 +0000] "GET /favicon.ico HTTP/1.1" 502 559 "http://192.168.21.4:84/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36"

my nginx config :


worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    access_log /var/log/nginx/access.log;

    # Load balancer configuration
    upstream php-apps {
        server php-app1:9002;
        server php-app2:9003;
    }

    server {
        listen 84;
        server_name localhost;

        location / {
            proxy_pass http://php-apps;
            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;
        }
    }
}

app1.conf :

http {

    server {
        listen 9002;
        server_name php-app1;
        root /var/www/app1/html;

        index index.php;

        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }

        location ~ \.php$ {
            fastcgi_pass php-app1:9000;
            fastcgi_index index.php;
            fastcgi_param REQUEST_METHOD $request_method;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    }

}

--------

app2.conf/

http {

    server {
        listen 9003;
        server_name php-app2;
        root /var/www/app2/html;

        index index.php;

        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }

        location ~ \.php$ {
            fastcgi_pass php-app2:9000;
            fastcgi_index index.php;
            fastcgi_param REQUEST_METHOD $request_method;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    }
}

my docker-compose :


version: '3'

services:
  nginx-lb:
    build: 
      context: ./nginx
    ports:
      - "84:84"
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./nginx/app1-config/app1.conf:/etc/nginx/conf.d/app1.conf
      - ./nginx/app2-config/app2.conf:/etc/nginx/conf.d/app2.conf
    depends_on:
      - php-app1
      - php-app2
    networks:
      my-test-network:
#        ipv4_address: 192.168.21.2
  php-app1:
    image: php:fpm
    volumes:
      - ./app1/html:/var/www/html
    ports:
      - "9002:9000"
    networks:
      my-test-network:
#       ipv4_address: 192.168.21.3
  php-app2:
    image: php:fpm
    volumes:
      - ./app2/html:/var/www/html
    ports:
      - "9003:9000"
    networks:
      my-test-network:
#        ipv4_address: 192.168.21.4
networks:
  my-test-network:
    driver: bridge
#    ipam:
#      config:
#       - subnet: 192.168.21.0/24


enter image description here


UnderDog
  • 305
  • 1
  • 4
  • 14

1 Answers1

1

Please have a look at the following post: How to correctly link php-fpm and Nginx Docker containers?

Seems like you have to adjust your volumes in order to serve it correctly with nginx. So using - ./app1/html:/var/www/app1/html and - ./app2/html:/var/www/app2/html should work. Afterwards you should be able to access your applications on port 9002 and 9003. It might also work on port 84 already, but please check the other ports first.


I mean changing your docker-compose to look like this:

version: '3'

services:
  nginx-lb:
    build: 
      context: ./nginx
    ports:
      - "84:84"
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./nginx/app1-config/app1.conf:/etc/nginx/conf.d/app1.conf
      - ./nginx/app2-config/app2.conf:/etc/nginx/conf.d/app2.conf
    depends_on:
      - php-app1
      - php-app2
    networks:
      my-test-network:
#        ipv4_address: 192.168.21.2
  php-app1:
    image: php:fpm
    container_name: php-app1
    volumes:
      - ./app1/html:/var/www/app1/html
    ports:
      - "9002:9000"
    networks:
      my-test-network:
#       ipv4_address: 192.168.21.3
  php-app2:
    image: php:fpm
    container_name: php-app2
    volumes:
      - ./app2/html:/var/www/app2/html
    ports:
      - "9003:9000"
    networks:
      my-test-network:
#        ipv4_address: 192.168.21.4
networks:
  my-test-network:
    driver: bridge
#    ipam:
#      config:
#       - subnet: 192.168.21.0/24
Geilmaker
  • 471
  • 1
  • 7
  • do you mean in each container or in nginx-lb container – Taibh Khalid Jul 31 '23 at 11:44
  • `2023/07/31 11:58:22 [warn] 1#1: conflicting server name "php-app2" on 0.0.0.0:9003, ignored test2-nginx-1 | nginx: [warn] conflicting server name "php-app2" on 0.0.0.0:9003, ignored` – Taibh Khalid Jul 31 '23 at 11:59
  • I edited my answer. But you indeed also need to change the server_name in you app1.conf and app2.conf. You should use `server_name localhost` or `server_name default_server` or `server_name _` depending on how you want to access it. If it's all running on your local computer you should use `server_name localhost`. – Geilmaker Jul 31 '23 at 12:53
  • now it opens the nginx html file. not my apps. i changed my code as you mentioned – Taibh Khalid Jul 31 '23 at 12:59
  • You mean the default nginx page with "Welcome to nginx"? On both port 9002 and 9003? – Geilmaker Jul 31 '23 at 13:01
  • on 84 it opens the default nginx page when i do host-ip:9002 or 9003 get me an error – Taibh Khalid Jul 31 '23 at 13:15
  • Can you tell which error? On one side in browser and also in nginx logs? – Geilmaker Jul 31 '23 at 14:46
  • Maybe you should also add the container name to your docker compose file like I updated above. – Geilmaker Jul 31 '23 at 15:57
  • i do it, name the containers but the load balance not working. it just open the default nginx page or one of my app. but it does not balancing my two apps – Taibh Khalid Aug 01 '23 at 13:27
  • So what is working currently? You can access your app on port 9002 and 9003 and also access it on port 84 but not load balanced (always requesting the same app)? – Geilmaker Aug 02 '23 at 09:42