Score:0

Docker + Symfony + Nginx + PHP 8.1 bad gateway configure php for Nginx

cn flag

I am trying to access my symfony application via a docker compose setup

I have issues with nginx default.conf When trying to access the app I am getting bad gateway

docker logs show an error:

no resolver defined to resolve app, client: 193.32.126.216

so I tried putting 127.0.0.1:9000 instead of app:9000 and I get a new error:

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

I have this configuration:

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/

default.conf

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;
}
}

fd

Score:0
gr flag

Do you really need to specify your upstream name using the variable? This trick usually used when your backend can be unavailable or down when the nginx container starts; some more details can be found in this article. If it isn't your case, you can use the container name directly:

fastcgi_pass app:9000;

If it is really your case after all, you can define a resolver using internal docker-compose container-to-IP resolving system:

resolver 127.0.0.11;

or better move your upstream declaration to the separate upstream block and go on without and additional resolver needed to be defined:

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

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.