在/etc/nginx/nginx.conf的上游“ php”中找不到Docker Azure主机

时间:2020-11-02 19:19:40

标签: azure docker nginx docker-compose nginx-config

我有一个问题,当我在本地运行Docker时,它可以工作,但是当我尝试在Azure上运行多容器应用程序时,会出现此错误:

2020/11/02 19:10:39 [emerg] 1#1: host not found in upstream "php" in /etc/nginx/nginx.conf:38
nginx: [emerg] host not found in upstream "php" in /etc/nginx/nginx.conf:38 

很显然是指fastcgi_pass php:9000;行,我尝试使用localhost,但这没用。有什么想法吗?

docker-compose.yml:

version: '3.1'

services:

  db:
    image: mysql:5.7
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=password
    ports:
      - "3306:3306"

  php:
    image: joelcastillo/php:1.1
    ports:
      - "9000:9000"
    restart: always

  web:
    image: joelcastillo/nginx:1.3
    ports:
      - "80:80"
    links:
      - php

PHP Dockerfile:

FROM php:7-fpm

# Use the default production configuration
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"

RUN docker-php-ext-install mysqli
RUN usermod -u 1000 www-data
RUN apt-get update && apt-get install -y libpng-dev libmagickwand-dev --no-install-recommends
RUN docker-php-ext-install opcache
RUN docker-php-ext-install gd
RUN pecl install imagick
RUN docker-php-ext-enable imagick 

Nginx Dockerfile:

FROM nginx

COPY ./ /code
COPY ./docker/nginx/nginx.conf /etc/nginx/nginx.conf

nginx.conf:

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}

http {

    server {

        listen         80 default_server;
        root           /code/public_html;
        index          index.php;

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

        # Forward images to prod site
        location ~ ^/app/uploads {
           try_files $uri @prod;
        }

        location @prod {
           rewrite ^/app/uploads/(.*)$  https://jee-o.com/app/uploads/$1 redirect;
        }

        location ~ \.php$ {

                proxy_read_timeout 3600;
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass php:9000;
                fastcgi_index index.php;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_param PATH_INFO $fastcgi_path_info;

            }

    }

    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

}

1 个答案:

答案 0 :(得分:1)

如果您使用的是docker-compose,则将通过网络创建所有服务,并可以通过该网络按容器名称(例如“ php”)进行通信。

如果使用其他某种机制(您的问题没有详细介绍如何配置Azure实例),则必须确保:

  • 您创建了一个可以将容器连接到的网络(例如docker network create my-network
  • 使用名称“ php”创建后端容器,并将其附加到您的网络(例如docker run --name php --network my-network <image>
  • 如果您已经启动了容器,则仍可以将其连接到网络:docker network connect my-network php