Home > Back-end >  Where is the bug in docker-compose?
Where is the bug in docker-compose?

Time:05-15

I run a simple project (docker-compose and nginx), but it don't work and I do not know why. I include the entire code of the project so as not to miss anything.

My project includes:

  • docker-compose.yaml
  • data
    • nginx.conf
  • website
    • index.php

docker-compose.yaml:

version: '3.7'

services:
  nginx-proxy:
    image: nginx:stable-alpine
    container_name: nginx-proxy
    networks:
       - network
    ports:
      - 80:80
      - 443:443
    volumes:
      - ./data/nginx.conf:/etc/nginx/conf.d/default.conf
  website:
    image: php:7.4-fpm
    container_name: website
    volumes:
      - ./website:/var/www/html
    expose:
      - "3000"
    networks:
       - network
networks:
  networks:
    driver: bridge

index.php:

   <html>
    <body>
       Body of site
    </body>
   </html>

nginx.conf:

upstream site {
  server website:3000;
}

server {
  listen 80;
  listen [::]:80;
  server_name .test.ru;

  return 301 https://$server_name$request_uri;
}

server {
  listen 443 ssl http2;
  listen [::]:443 ssl http2;
  server_name test.ru www.test.ru;

  location / {
    proxy_pass http://website:3000;
  }
}

I don't understand how to solve it. The logs (command "docker-compose logs nginx-proxy") show only requests from the client. That is, requests reach, but the page is not loaded.

But I need my static page to open. Also I can upload the project if nginx.conf:

server {
  return  301 http://google.com;
}

Please help me.

CodePudding user response:

Unfortunately, You can't use proxy_pass with the php-fpm docker image because it doesn't provide an http server but it implements fastcgi protocol instead (if you need more about FastCGI Process Manager).

You can try to replace your nginx.conf by :

upstream site {
  server website:3000;
}

server {
  listen 80;
  listen [::]:80;
  server_name test.ru;

  return 301 https://$server_name$request_uri;
}

server {
  listen 443 ssl http2;
  listen [::]:443 ssl http2;
  server_name test.ru www.test.ru;
  root /var/www/html;  // You need to define where is your static files directory

  location / {
    fastcgi_split_path_info ^(. \.php)(/. )$;
    fastcgi_pass site; // You can use your upstream here
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  }
}
  • Related