简体   繁体   中英

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. I am using docker-compose to host a local wordpress with nginx. All container are starting, but the nginx container refuses to work. So I can't reach the site under 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:

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:

/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:

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 :

    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.

Looking at the nginx docs , this configuration requires either the IP address or DNS name of a FastCGI server AND a port. 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.

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.

To overwrite the wordpress.conf file, obtain a copy of the original from the container, and change the fastcgi_pass section to 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:
  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.

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.

ORIGINAL ANSWER:

Try change the NGINX_SERVER_NAME environment variable from localhost to nginx .

When you run your application as a Compose project, Compose sets up a default single network for your application. This network is named after your Compose project, which you can view by running the docker network ls command. 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 .

In your case, localhost isn't reachable by other containers in your application.

You will have to make the same change to the PHP_HOST_NAME environment variable.

A bit late to the party, but like you can see in the error log, the problem is in the line fastcgi_pass backend; .

backend is not a valid upstream. In your case you should reference the php service ( php in your docker-compose file), and reference it like 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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