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


3
  • Can you access those apps on port 9002 and 9003? Commented Jul 31, 2023 at 10:08
  • no i cant, may be i need nginx for each app not just nginx as load balancer Commented Jul 31, 2023 at 10:49
  • i mean there is a need for inginx image for each server and how i add it in docker compose Commented Jul 31, 2023 at 10:56

1 Answer 1

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 
Sign up to request clarification or add additional context in comments.

10 Comments

do you mean in each container or in nginx-lb container
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
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.
now it opens the nginx html file. not my apps. i changed my code as you mentioned
You mean the default nginx page with "Welcome to nginx"? On both port 9002 and 9003?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.