无法使用Docker-Compose连接到NGINX
我只需要将我的开发环境转移到新的Mac上,并在新机器上安装Docker。 我正在使用docker-compose来托pipenginx的本地wordpress。 所有的容器都开始了,但是nginx容器拒绝工作。 所以我无法到达localhost:8000下的网站
我在旧电脑上有相同的configuration,运行顺畅。 我错过了什么?
所以,这是我的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"
使用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
看来这些端口并没有真正设置正确。 比较旧计算机上的nginx容器和新安装的端口显示更清楚:
旧电脑:
新电脑:
你知道我做错了什么,或者我可以怎样设置权限端口吗?
编辑:
这是/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; } }
你正在试图在一个未定义的名字里面代理请求: backend
2可能性:
-
把你的
wordpress.conf
里的backend
变成php
。 正如你所看到的,你的php容器的名字是php
,所以backend
是未定义的。 -
更改所有的
php
到你的Dockerfile中的backend
。 要小心端口转发。
看看ivan对PHP_HOST_NAME的回答:
PHP_HOST_NAME: localhost:8000 => PHP_HOST_NAME: nginx:8000
第83行的/etc/nginx/conf.d/wordpress.conf
存在一个错误,其中fastcgi_pass backend
configuration引用了不存在的backend
服务。
查看nginx 文档 ,此configuration需要FastCGI服务器的IP地址或DNS名称和一个端口。 IOW,即使你把你的docker-compose.yml中的php
服务重命名为backend
,nginx仍然会失败,另一个nginx: [emerg] no port in upstream "backend" in /etc/nginx/conf.d/wordpress.conf:83
错误,因为端口号丢失。
由于这个wordpress.conf
wodby/wordpress-nginx
入了wodby/wordpress-nginx
映像,所以你必须根据这个映像创build你自己的Dockerfile,或者覆盖/etc/nginx/conf.d/wordpress.conf
文件。
要覆盖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; }
然后更新docker-compose.yaml中的nginx
服务定义,以使用您的版本的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
请注意,容器中的目标path是/etc/gotpl/wordpress.conf.tpl
因为这是容器的/docker-entrypoint.sh
脚本从/etc/nginx/conf.d
文件夹复制的源。
最后,正如我原来的回答指出的那样,不要忘记将docker-compose.yaml文件中的所有localhost
引用更改为正确的服务名称。
原文答案:
尝试将NGINX_SERVER_NAME
环境variables从localhost
更改为nginx
。
当您将应用程序作为Compose项目运行时,Compose将为您的应用程序设置一个默认的单一networking 。 该networking以您的Compose项目命名,您可以通过运行docker network ls
命令来查看该项目。 项目中的所有容器都join到该networking中,并且通过主机名(它们被设置为与容器名称相同)可以互相访问和发现。 您可以在文档中阅读有关Docker Compose Networking的更多信息。
在你的情况下,你的应用程序中的其他容器无法访问localhost
。
您将不得不对PHP_HOST_NAME
环境variables进行相同的更改。