Magento:在 docker 上配置 nginx 清漆时收到错误

Magento : Recieving errors when config nginx varnish on docker

我收到 nginx 错误:

[emerg] host not found in upstream "varnish" in /etc/nginx/conf.d/default.conf:53

清漆日志中的错误

Backend host '"web"' could not be resolved to an IP address: Temporary failure in name resolution (Sorry if that error message is gibberish.) ('/etc/varnish/default.vcl' Line 9 Pos 13) .host = "web";

它似乎有一个依赖循环,使得docker无法同时启动varnish和nginx。

有人对此有任何想法吗?

文件docker-compose.yml

web:
image: magento/magento-cloud-docker-nginx:latest
ports:
  - 80:80
  - 443:443
  - 8080:8080
links:
  - fpm
  - fpm_xdebug
  - db
volumes_from:
  - appdata
volumes:
  - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
  - ./nginx/ssl:/etc/nginx/ssl
env_file:
  - ./global.env
varnish:
build: ./varnish
depends_on:
  - web
links:
  - web
ports:
  - 6082:6082
  - 6081:6081
volumes:
  - ./varnish/etc/varnish/default.vcl:/etc/varnish/default.vcl
  - ./varnish/etc/default/varnish:/etc/default/varnish

我的配置default.conf

upstream fastcgi_backend {
# use tcp connection
  server  fpm:9000;
}
server {
  listen 80;
  server_name local.defaultsite.com;
  return 301 https://$server_name$request_uri;
}

server {
  listen 8080;
  server_name local.defaultsite.com;
  # set $MAGE_ROOT /app;
  set $MAGE_ROOT /home/webapps/public_html;
  # include /app/nginx.conf.sample;
  include /home/webapps/public_html/nginx.conf.sample;
}

server {
  listen 443 ssl http2 default_server;
  listen [::]:443 ssl http2 default_server;
  ssl_certificate /etc/nginx/ssl/cert.pem;
  ssl_certificate_key /etc/nginx/ssl/key.pem;
  set $MAGE_ROOT /home/webapps/public_html;
  set $MAGE_DEBUG_SHOW_ARGS 0;
  set $my_fastcgi_pass "fastcgi_backend";
  if ($cookie_XDEBUG_SESSION) {
    set $my_fastcgi_pass "fastcgi_backend_xdebug";
  }
  server_name local.defaultsite.com;
  location / {
   proxy_pass http://varnish:6081;
   proxy_set_header Host               $http_host;
   proxy_set_header X-Forwarded-Host   $http_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  https;
   proxy_set_header X-Forwarded-Port   443;
   proxy_buffer_size                   128k;
   proxy_buffers                       4 256k;
   proxy_busy_buffers_size             256k;
   fastcgi_buffer_size                 32k;
   fastcgi_buffers                     4 32k;
  }
 }

清漆default.vcl

backend default {
.host = "web";
.port = "8080";
.first_byte_timeout = 600s;
}

问题始于您的 Nginx 配置使用以下 FastCGI 上游配置:

upstream fastcgi_backend {
# use tcp connection
  server  fpm:9000;
}

主机名是 fpm,可能指的是 PHP-FPM Docker 容器。此容器似乎不是您 docker-compose.yml 设置的一部分。

可能是您的 web 容器无法从中恢复并出现故障(请参阅 docker-compose 输出日志)。

Varnish 依赖 web 主机名来代理其数据,但由于它不再解析,Varnish 也会抛出错误。

看看 https://hub.docker.com/_/php?tab=tags&page=1&name=fpm:它列出了官方 PHP 图片的所有可用的基于 FPM 的图片。你可以选择 php:7.4-fpm.