Taibh Khalid
Taibh Khalid

Reputation: 1

nginx config with docker bad getway

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


Upvotes: 0

Views: 64

Answers (1)

Geilmaker
Geilmaker

Reputation: 533

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

Upvotes: 1

Related Questions