使用docker-compose在关闭的docker容器上为前端react和后端php运行nginx服务器

时间:2020-05-27 05:38:57

标签: php docker ubuntu nginx docker-compose

我正在尝试使用相同的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

0 个答案:

没有答案