首页 > 解决方案 > 为前端 react 和后端 php 运行 nginx 服务器,两者都在使用 docker-compose 的 docker 容器上

问题描述

我正在尝试使用相同的 docker-compose 文件以及为它们使用单​​独的容器来运行我的前端和后端。

前端似乎运行良好。我的问题是我的后端抛出 502(通过端口 8080 和 /api) 注意:我已经检查了 php 文件是否正确复制到容器中。

后端 Dockerfile:

FROM php:7.4-fpm
RUN docker-php-ext-install pdo_mysql mysqli

FROM nginx:latest
ADD ./nginx.conf /etc/nginx/nginx.conf
RUN mkdir logs 
RUN cd ./logs && touch error.log && touch access.log

EXPOSE 80 9000

后端 nginx.conf 文件:

user nginx;
worker_processes  1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
  worker_connections 1024;
}
http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    log_format main '$remote_addr - $remote_user [$time_local]'
                  '"$request" $status $body_bytes_sent'
                  '"$http_referer" "$http_user_agent"'
                  '"$http_x_forwarded_for"';
        access_log /var/log/nginx/access.log main;


        server {
            listen 80;
            server_name 35.174.39.18;
            root /var/www/html;
            index index.html index.php;

            charset utf-8;
            error_log /logs/error.log;
            access_log /logs/access.log;

             location / {
                try_files $uri $uri/ /index.php?$query_string;

            }
            location ~ \.php {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
                fastcgi_param SCRIPT_NAME $fastcgi_script_name;
                fastcgi_index index.php;
                fastcgi_pass php:9000;
            }
         }
}

前端 Dockerfile:

FROM node:latest AS build
WORKDIR /app

COPY . /app/

RUN npm install --silent
RUN npm install react-scripts@3.0.1 -g --silent

RUN npm run-script build

前端 Nginx Dockerfile:

FROM nginx:alpine
# Add the Nginx configuration file
ADD ./nginx.conf /etc/nginx/nginx.conf
COPY --from=ubuntu_front-end /app/build /usr/share/nginx/html
RUN mkdir logs 
RUN cd ./logs && touch error.log && touch access.log
EXPOSE 80
#CMD ["nginx","-g","daemon off;"]

前端的 nginx.conf 文件

user nginx;
worker_processes  1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
  worker_connections 1024;
}
http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    log_format main '$remote_addr - $remote_user [$time_local]'
                  '"$request" $status $body_bytes_sent'
                  '"$http_referer" "$http_user_agent"'
                  '"$http_x_forwarded_for"';
    access_log /var/log/nginx/access.log main;

    upstream backend {
            server 35.174.39.18:8080;
    }
        server {

                listen 80;
                server_name 35.174.39.18;

                charset utf-8;
                root   /usr/share/nginx/html;
                index  index.php index.html index.htm;

                location / { 
                try_files $uri $uri/ /index.html;

                }

                location /api {
                    proxy_pass http://backend;
                }

                # Any route containing a file extension (e.g. /devicesfile.js)
                location ~ ^.+\..+$ {
                try_files $uri =404;
                }

                error_page   500 502 503 504  /50x.html;
                location = /50x.html {
                root   /usr/share/nginx/html;
                }

        }
}

最后,这是我的 docker-compose.yml 文件:

version: "3"
services:

   front-end:
       container_name: frontend
       build: ./front-end

   nginx-frontend:
     build: ./nginx
     container_name: nginx-frontend
     ports:
       - 80:80
     depends_on:
      - nginx-backend
     command: nginx -g 'daemon off';


   nginx-backend:
     build: ./slim-backend
     hostname: backend
     container_name: nginx-backend
     ports:
       - 8080:80
     volumes:
       - app:/var/www
       - public:/var/www/html


   php:
     image: php:7.4-fpm
     volumes:
       - app:/var/www
       - public:/var/www/html
     command: "true" 
     expose:
       - 9000   

volumes:
   public:
      driver: local
      driver_opts:
         o: bind
         type: none
         device: /home/ubuntu/slim-backend/app/public
   app:
      driver: local
      driver_opts:
         o: bind
         type: none
         device: /home/ubuntu/slim-backend/app

标签: phpdockerubuntunginxdocker-compose

解决方案


推荐阅读