简体   繁体   English

无法使用 Docker-Compose 连接到 NGINX

[英]Can not connect to NGINX with Docker-Compose

I just had to tranfer my development enviroment to a new Mac and set up Docker on the new machine.我只需要将我的开发环境转移到新的 Mac 并在新机器上设置 Docker。 I am using docker-compose to host a local wordpress with nginx.我正在使用 docker-compose 通过 nginx 托管本地 wordpress。 All container are starting, but the nginx container refuses to work.所有容器都在启动,但 nginx 容器拒绝工作。 So I can't reach the site under localhost:8000所以我无法访问 localhost:8000 下的站点

I have the same configuration on my old computer and it is running smoothly.我在我的旧电脑上有相同的配置,它运行顺利。 Am I am missing something?我错过了什么吗?

So, this is my dockerfile:所以,这是我的 dockerfile:

version: "2"

services:
  mariadb:
    image: wodby/wordpress-mariadb
    environment:
      MYSQL_RANDOM_ROOT_PASSWORD: 1
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
    volumes:
      - ./docker-runtime/mariadb:/var/lib/mysql

  php:
    image: wodby/wordpress-php
    environment:
      PHP_SITE_NAME: dev
      PHP_HOST_NAME: localhost:8000
      PHP_SENDMAIL_PATH: /usr/sbin/sendmail -t -i -S mailhog:1025
      PHP_XDEBUG_ENABLED: 0 
    volumes:
      - ./:/var/www/html

  nginx:
    image: wodby/wordpress-nginx
    environment:
      NGINX_SERVER_NAME: localhost
      NGINX_UPSTREAM_NAME: php
    volumes_from:
      - php
    ports:
      - "8000:80"

  pma:
    image: phpmyadmin/phpmyadmin
    environment:
      PMA_HOST: mariadb
      PMA_USER: wordpress
      PMA_PASSWORD: wordpress
      PHP_UPLOAD_MAX_FILESIZE: 1G
      PHP_MAX_INPUT_VARS: 1G
    ports:
     - "8001:80"

  mailhog:
    image: mailhog/mailhog
    ports:
      - "8002:8025"

Using Kitematic I get the following error for nginx:使用 Kitematic 我收到以下 nginx 错误:

/docker-entrypoint.sh: running /docker-entrypoint-init.d/wordpress-nginx.sh
nginx: [emerg] no port in upstream "backend" in /etc/nginx/conf.d/wordpress.conf:83

It seems that the ports are not really set correctly.似乎端口没有真正正确设置。 Comparing the ports for the nginx container on my old computer with the new installation shows that more clearly:将旧计算机上的 nginx 容器的端口与新安装的端口进行比较可以更清楚地表明:

Old Computer:旧电脑: 在此处输入图片说明

New computer:新电脑: 在此处输入图片说明

Do you have any idea of what I have done wrong or how I can set up the rights ports?你知道我做错了什么或者我如何设置正确的端口吗?

EDIT:编辑:

This is the /etc/nginx/conf.d/wordpress.conf :这是/etc/nginx/conf.d/wordpress.conf

    server {
        server_name wordpress;
        listen 80;

        root /var/www/html/;
        index index.php;

        fastcgi_keep_conn on;
        fastcgi_index index.php;
        fastcgi_param QUERY_STRING $query_string;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

        add_header Cache-Control "store, must-revalidate, post-check=0, pre-check=0";

        location ~* ^/.well-known/ {
            allow all;
        }

        location = /favicon.ico {
            try_files $uri =204;
            log_not_found off;
            access_log off;
        }

        location = /robots.txt {
            allow all;
            log_not_found off;
            access_log off;
        }

        location = /readme.html {
            return 404;
        }

        location ~* ^.*(\.(?:git|svn|htaccess|txt|pot?))$ {
            return 404;
        }

        location ~ /\. {
            deny all;
        }

        location ~* /(?:uploads|files)/.*\.php$ {
            deny all;
        }

        location ~* \.flv$ {
            flv;
        }

        location ~* .*\.(?:m4a|mp4|mov)$ {
            mp4;
            mp4_buffer_size     1M;
            mp4_max_buffer_size 5M;
        }

        location ~* ^.+\.(?:ogg|pdf|pptx?)$ {
            expires max;
            tcp_nodelay off;
        }

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

        # Add trailing slash to */wp-admin requests.
        rewrite /wp-admin$ $scheme://$host$uri/ permanent;

        # Directives to send expires headers and turn off 404 error logging.
        location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|woff2|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
            access_log off; log_not_found off; expires max;
        }

        location ~ [^/]\.php(/|$) {
            fastcgi_split_path_info ^(.+?\.php)(/.*)$;
            if (!-f $document_root$fastcgi_script_name) {
                return 404;
            }

            include fastcgi.conf;
            fastcgi_index index.php;
            fastcgi_pass backend;
            track_uploads uploads 60s;
        }
    }

There is a bug in the /etc/nginx/conf.d/wordpress.conf on line 83 where the fastcgi_pass backend configuration refers to a backend service that doesn't exist.第 83 行的/etc/nginx/conf.d/wordpress.conf有一个错误,其中fastcgi_pass backend配置指的是不存在的backend服务。

Looking at the nginx docs , this configuration requires either the IP address or DNS name of a FastCGI server AND a port.查看 nginx 文档,此配置需要 FastCGI 服务器的 IP 地址或 DNS 名称端口。 IOW, even if you renamed your php service in your docker-compose.yml to backend , nginx will still fail with another nginx: [emerg] no port in upstream "backend" in /etc/nginx/conf.d/wordpress.conf:83 error, because the port number is missing. IOW,即使您将 docker-compose.yml 中的php服务重命名为backend ,nginx 仍然会因另一个nginx: [emerg] no port in upstream "backend" in /etc/nginx/conf.d/wordpress.conf:83失败nginx: [emerg] no port in upstream "backend" in /etc/nginx/conf.d/wordpress.conf:83错误,因为缺少端口号。

Since this wordpress.conf is baked into the wodby/wordpress-nginx image, either you have to create your own Dockerfile based on this image, or overwrite its /etc/nginx/conf.d/wordpress.conf file.由于此wordpress.conf已烘焙到wodby/wordpress-nginx映像中,因此您必须基于此映像创建自己的 Dockerfile,或覆盖其/etc/nginx/conf.d/wordpress.conf文件。

To overwrite the wordpress.conf file, obtain a copy of the original from the container, and change the fastcgi_pass section to php:80 :要覆盖wordpress.conf文件,请从容器中获取原始副本,并将fastcgi_pass部分更改为php:80

location ~ [^/]\.php(/|$) {
  fastcgi_split_path_info ^(.+?\.php)(/.*)$;
  if (!-f $document_root$fastcgi_script_name) {
    return 404;
  }

  include fastcgi.conf;
  fastcgi_index index.php;
  fastcgi_pass php:80;
  track_uploads uploads 60s;
}

Then update the nginx service definition in your docker-compose.yaml to use your version of the wordpress.conf using volumes as seen below.然后更新nginx在泊坞窗,compose.yaml服务定义使用您的版本wordpress.conf使用volumes ,如下图所示。

nginx:
  image: wodby/wordpress-nginx
  environment:
    NGINX_SERVER_NAME: nginx
    NGINX_UPSTREAM_NAME: php
  ports:
    - "8000:80"
  depends_on:
    - php
  volumes:
    - ./wordpress.conf:/etc/gotpl/wordpress.conf.tpl

Note that the target path in the container is /etc/gotpl/wordpress.conf.tpl because that's the source the container's /docker-entrypoint.sh script copies from into the /etc/nginx/conf.d folder.请注意,容器中的目标路径是/etc/gotpl/wordpress.conf.tpl因为这是容器的/docker-entrypoint.sh脚本复制到/etc/nginx/conf.d文件夹中的源。

Finally, as pointed out in my original answer, don't forget to change all references of localhost in your docker-compose.yaml file to the correct service name.最后,正如我在原始答案中指出的那样,不要忘记将 docker-compose.yaml 文件中localhost所有引用更改为正确的服务名称。

ORIGINAL ANSWER:原始答案:

Try change the NGINX_SERVER_NAME environment variable from localhost to nginx .尝试将NGINX_SERVER_NAME环境变量从localhost更改为nginx

When you run your application as a Compose project, Compose sets up a default single network for your application.当您将应用程序作为 Compose 项目运行时,Compose 会为您的应用程序设置一个默认的单一网络 This network is named after your Compose project, which you can view by running the docker network ls command.该网络以您的 Compose 项目命名,您可以通过运行docker network ls命令查看该项目。 All the containers in your project join that network and are both reachable and discoverable by each other via their hostnames, which are set to be identical to the containers name.项目中的所有容器都加入该网络,并且可以通过它们的主机名相互访问和发现,主机名设置为与容器名称相同。 You can read more about Docker Compose Networking in the docs .您可以在docs 中阅读有关 Docker Compose Networking 的更多信息。

In your case, localhost isn't reachable by other containers in your application.在您的情况下,您的应用程序中的其他容器无法访问localhost

You will have to make the same change to the PHP_HOST_NAME environment variable.您必须对PHP_HOST_NAME环境变量进行相同的更改。

A bit late to the party, but like you can see in the error log, the problem is in the line fastcgi_pass backend;聚会有点晚了,但是就像您在错误日志中看到的那样,问题出在fastcgi_pass backend;这一行fastcgi_pass backend; . .

backend is not a valid upstream. backend不是有效的上游。 In your case you should reference the php service ( php in your docker-compose file), and reference it like fastcgi_pass php:9000;在您的情况下,您应该引用 php 服务( docker-compose文件中的php ),并像fastcgi_pass php:9000;一样引用它fastcgi_pass php:9000; (I'm assuming the port you are using for the php fpm is 9000, but could be another, in which case you should change accordingly). (我假设您用于 php fpm 的端口是 9000,但可能是另一个,在这种情况下您应该相应地更改)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM