Docker + Symfony + Nginx + PHP 8.1 错误网关为 Nginx 配置 php

Docker + Symfony + Nginx + PHP 8.1 错误网关为 Nginx 配置 php

我正在尝试通过 docker compose 设置访问我的 symfony 应用程序

我在使用 nginx 时遇到问题,default.conf尝试访问应用程序时遇到网关错误

docker logs 显示错误:

no resolver defined to resolve app, client: 193.32.126.216

所以我尝试用 代替127.0.0.1:9000app:9000出现了新的错误:

connect() failed (111: Connection refused) while connecting to upstream

我有这样的配置:

version: "3"
services:

nginx:
  container_name: nginx
  image: "${NGINX_IMAGE}"
  build: build/nginx
  restart: always
  env_file: .env
  ports:
    # - "8000:443"
    - "80:80"
    - "443:443"
  volumes:
    - "${APP_HOST_DIR}/public:/var/www/app/public:ro"
    - "${APP_HOST_LETSENCRYPT}:${APP_CONTAINER_LETSENCRYPT}"
    - "${APP_HOST_NGINX_CONF}:${APP_CONTAINER_NGINX_CONF}"
  volumes_from:
    - app
  networks:
    - central_mr
  depends_on:
    - app

app:
  container_name: app
  image: "${APP_IMAGE}"
  restart: always
  build: build/app
  env_file: .env
  networks:
    - central_mr
  volumes:
    - "${APP_HOST_DIR}:${APP_CONTAINER_DIR}"


  networks:
    central_mr:

.env

# PROD
MODE=prod

# IMAGES
NGINX_IMAGE=mr/nginx:prod
MARIADB_VERSION=latest
APP_IMAGE=mr/app:prod

# APP
APP_HOST_DIR=./app
APP_CONTAINER_DIR=/var/www/app/

# MARIADB
MARIADB_DATA_DIR=/var/lib/mysql
MARIADB_LOG_DIR=/var/logs/mysql
SQL_INIT=./build/database/prod

MYSQL_DATABASE=app
MYSQL_USER=app
MYSQL_PASSWORD=password
MYSQL_ROOT_PASSWORD=password

APP_HOST_NGINX_CONF=./volume/etc/nginx/prod/default.conf
APP_CONTAINER_NGINX_CONF=/etc/nginx/conf.d/default.conf

# NGINX
APP_HOST_NGINX_CONF=./volume/etc/nginx/prod/default.conf
APP_CONTAINER_NGINX_CONF=/etc/nginx/conf.d/default.conf

# SSL
APP_HOST_LETSENCRYPT=/etc/letsencrypt/
APP_CONTAINER_LETSENCRYPT=/etc/letsencrypt/

默认配置文件

server {

listen [::]:443 ssl; 
listen 443 ssl; 
ssl_certificate /etc/letsencrypt/live/mywebsite.com/fullchain.pem; 
ssl_certificate_key /etc/letsencrypt/live/mywebsite.com/privkey.pem; 
include /etc/letsencrypt/options-ssl-nginx.conf; 
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; 
root /var/www/app/public/;
index index.php;
server_name mywebsite.com; 


location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    set $upstream app:9000;
    fastcgi_pass $upstream; 
    fastcgi_index index.php;
}
}

自由度

答案1

你真的需要使用变量指定上游名称吗?此技巧通常在 nginx 容器启动时后端不可用或关闭时使用;更多详细信息可参见如果不是这种情况,您可以直接使用容器名称:

fastcgi_pass app:9000;

如果这确实是您的情况,您可以使用内部 docker-compose 容器到 IP 解析系统定义一个解析器:

resolver 127.0.0.11;

或者最好将您的上游声明移至单独的上游块并继续,而resolver无需定义其他内容:

upstream webapp {
    server app:9000;
}
server {
    ...
    location ~ \.php$ {
        try_files $uri =404;
        set $upstream webapp;
        fastcgi_pass $upstream; 
    }
}

相关内容