Home > OS >  WSL2 Nginx PHP FPM failed (111: Connection refused) while connecting to upstream, client: 172.23.0
WSL2 Nginx PHP FPM failed (111: Connection refused) while connecting to upstream, client: 172.23.0

Time:05-30

Every time that I'm trying to access nginx via localhost:8000 / 127.0.0.1:8000 I catch this error:

nginx_1 | 2022/05/29 13:28:57 [error] 32#32: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.22.0.1, server: , request: "GET /favicon.ico HTTP/1.1", upstream: "fastcgi://172.22.0.5:9001", host: "localhost:8000", referrer: "http://localhost:8000/"

A day before this docker config worked

I created the github repo for the for easier code review

What I'm already tried:

  1. Change FPM/Nginx ports
  2. Restart WSL/Docker/PC
  3. New symfony project
  4. Add php && nginx containers at the same network

docker-compose:

version: '3.7'
services:
  database:
    image: postgres:11-alpine
    environment:
      POSTGRES_USER: root
      POSTGRES_PASSWORD: symfony
      POSTGRES_DB: main
    ports: 
      - 15432:5432
  php:
    build: ./docker/php
    ports: ['9001:9000']
    volumes: ['./symfony/:/var/www/symfony:cached']
    depends_on:
      - database
  nginx:
    build: ./docker/nginx
    ports: ['8000:80']
    volumes: ['./symfony/:/var/www/symfony:cached']
  adminer:
    image: adminer
    restart: always
    links:
      - database
    ports:
      - 8081:8080

symfony.conf:

    upstream php-upstream {
        server php:9001;
    }
    
    server {
       listen 80;
       root /var/www/symfony/public;
       
       fastcgi_buffers 16 16k;
       fastcgi_buffer_size 32k;
    
       location / {
          try_files $uri /index.php$is_args$args;
       }
    
       location ~ ^/. \.php(/|$) {
          fastcgi_pass php-upstream;
          fastcgi_split_path_info ^(. \.php)(/.*)$;
          include fastcgi_params;
          fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
          fastcgi_param DOCUMENT_ROOT $realpath_root;
          internal;
       }
    
       location ~ \.php$ {
           return 404;
       }

   error_log /var/log/nginx/error.log;
   access_log /var/log/nginx/access.log;
}

nginx.conf:

user nobody;
worker_processes auto;
pid /run/nginx.pid;

events {
  worker_connections  4000;
  multi_accept on;
  use epoll;
}

http {
  server_tokens off;
  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;
  keepalive_timeout 30;
  types_hash_max_size 2048;
  include /etc/nginx/mime.types;
  default_type application/octet-stream;
  access_log off;
  error_log off;
  gzip on;
  gzip_min_length 10240;
  gzip_comp_level 1;
  gzip_vary on;
  gzip_disable msie6;
  gzip_proxied expired no-cache no-store private auth;
  gzip_types
      text/css
      text/javascript
      text/xml
      text/plain
      text/x-component
      application/javascript
      application/x-javascript
      application/json
      application/xml
      application/rss xml
      application/atom xml
      font/truetype
      font/opentype
      application/vnd.ms-fontobject
      image/svg xml;
  include /etc/nginx/conf.d/*.conf;
  include /etc/nginx/sites-enabled/*;
  reset_timedout_connection on;
  open_file_cache max=200000 inactive=20s;
  open_file_cache_valid 30s;
  open_file_cache_min_uses 2;
  open_file_cache_errors on;
  client_body_temp_path /tmp 1 2;
  client_body_buffer_size 256k;
  client_body_in_file_only off;
}

daemon off;

CodePudding user response:

You are trying to use the port mapped on your host 9001 to connect to a container using its services name.

You have two options:

  1. Use the port that the container is listening on 9000 in your nginx upstream. php:9000

  2. Forward the upstream to the host with host.docker.internal:9001.

  3. Bonus: use a Unix Socket, but that is a different ball game.

When services share a network and have to communicate with each other, I would usually use the "internal" port. This way, you keep the network traffic within that network. So "solution 1" would be the best approach if you are using TCP/IP to connect. Plus, if you don't need to connect from outside the Docker network, you don't have to map the port on your host.

So... this should work:

upstream php-upstream {
    server php:9000;
}

server {
   listen 80;
   root /var/www/symfony/public;
   
   fastcgi_buffers 16 16k;
   fastcgi_buffer_size 32k;

   location / {
      try_files $uri /index.php$is_args$args;
   }

   location ~ ^/. \.php(/|$) {
      fastcgi_pass php-upstream;
      fastcgi_split_path_info ^(. \.php)(/.*)$;
      include fastcgi_params;
      fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
      fastcgi_param DOCUMENT_ROOT $realpath_root;
      internal;
   }

   location ~ \.php$ {
       return 404;
   }

   error_log /var/log/nginx/error.log;
   access_log /var/log/nginx/access.log;
}
  • Related