Setup nginx uptream for multiple php-fpm containers - recv() failed

721 Views Asked by At

I am new to nginx. I am trying to setup nginx with 2 different upstreams for my backend and my frontend apps (both are Symfony apps) Here is my docker-compose file

services:
  #PHP Back-End Service
  backend:
    build:
      context: .
      dockerfile: Dockerfile
    image: symfony-php-fpm:7.4
    container_name: backend
    volumes: 
      - ./backend/www:/var/www/backend.dev.local
      - ./z-backend.conf:/usr/local/etc/php-fpm.d/zz-docker.conf
    expose:
      - "9100"
...
  #PHP Front-End Service
  frontend:
    build:
      context: .
      dockerfile: Dockerfile
    image: symfony-php-fpm:7.4
    container_name: frontend
    volumes: 
      - ./frontend:/var/www/frontend.dev.local
      - ./z-frontend.conf:/usr/local/etc/php-fpm.d/zz-docker.conf
    expose:
      - "9200"
...  
  #Nginx Service
  webserver:
    image: nginx:1.8.1
    container_name: nginx
    restart: unless-stopped
    tty: true
    ports:
      - "80:80"
      - "443:443"
    volumes: 
      - ./backend/www:/var/www/backend.dev.local
      - ./frontend:/var/www/frontend.dev.local
      - ./conf/nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./conf/nginx/sites/:/etc/nginx/sites-available
      - ./conf/nginx/sites/:/etc/nginx/sites-enabled
      - ./conf/nginx/conf.d/:/etc/nginx/conf.d
      - ./logs:/var/log      
    depends_on: 
      - backend
      - frontend
    working_dir: /var/www

z-backend.conf :

[global]
daemonize = no

[www]
listen = 9100

z-frontend.conf

[global]
daemonize = no

[www]
listen = 9200

My nginx configuration

upstream backend-php-fpm {
    server 127.0.0.1:9100;
}
upstream frontend-php-fpm {
    server 127.0.0.1:9200;
}

server {
    listen 80;

    root /var/www/backend.dev.local/web;
    server_name backend.dev.local;
    index index.php index.html index.htm;
    location / {
        proxy_pass http://backend:9100;
         try_files $uri ./app.php$is_args$args;
    }
    location ~ \.php$ {
        try_files $uri /app.php =404;
        fastcgi_pass backend:9100;
        fastcgi_index app.php;
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_read_timeout 600;
        include fastcgi_params;
    }
}

server {
    listen 80;
    root /var/www/frontend.dev.local/public;
    server_name frontend.dev.local;
    index index.php index.html index.htm;
    location / {
        proxy_pass http://frontend:9200;
         try_files $uri ./index.php$is_args$args;
    }
    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_pass frontend:9200;
        fastcgi_index index.php;
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_read_timeout 600;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}

Everything is running and seems to be working. I can access both site and php-fpm is working. Except the assets cannot seem to be able to load for both sites.

[error] 8#0: *169 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 172.20.0.1, server: backend.dev.local, request: "GET /assets/js/plugins/pace/pace.min.js HTTP/1.1", upstream: "http://172.20.0.5:9100/assets/js/plugins/pace/pace.min.js", host: "backend.dev.local", referrer: "http://backend.dev.local/"

[error] 7#0: *192 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 172.20.0.1, server: frontend.dev.local, request: "GET /build/website.7e7abacb.js HTTP/1.1", upstream: "http://172.20.0.4:9200/build/website.7e7abacb.js", host: "frontend.dev.local", referrer: "http://frontend.dev.local/recettes"

Did I forget something or had a wrong parameter in nginx ?

1

There are 1 best solutions below

0
On

Found the problem. I removed the proxy_pass lines and everything worked fine.