php nginx config with docker bad getway

p8ekf7hl  于 2023-08-02  发布在  PHP
关注(0)|答案(1)|浏览(90)

终端错误:

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 [错误] 23#23:*3 connect()失败(111:连接被拒绝),同时连接到上游,客户端:192.168.21.1,服务器:localhost,请求:“GET / HTTP/1.1”,上行:“http://192.168.21.2:9003/“,主机:“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"


我的nginx配置:

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;
        }
    }
}


我的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

7kqas0il

7kqas0il1#

请看下面的帖子:How to correctly link php-fpm and Nginx Docker containers?
看起来你必须调整你的音量才能用nginx正确地服务它。所以使用- ./app1/html:/var/www/app1/html- ./app2/html:/var/www/app2/html应该可以。之后,您应该能够访问端口9002和9003上的应用程序。它也可能在端口84上工作,但请先检查其他端口。
我的意思是把你的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
    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

字符串

相关问题