Здравствуйте. Только начал знакомится с nginx и docker. Сделал простенький сайт с фронтом на реакте и бэкендом на ноде. Добавил ssl сертификаты для бэка. Чтобы мочь запускать через https. Все работало.
Решил сделать production сборку. Посмотрел ролики, почитал самые основы. И запилил как мог докерфайлы для бэка, для фронта, nginx.conf для фронта, docker-compose для всего проекта и nginx.conf для всего проекта.
Выглядят они вот так:
докерфайл бэкенда:
# server/Dockerfile
# Use the official Node.js image as the base image
FROM node:18-alpine
# Set the working directory
WORKDIR /app
# Copy package.json and package-lock.json
COPY package*.json ./
# Install dependencies
RUN npm install
# Copy the rest of the application code
COPY . .
# Expose port 433
EXPOSE 8443
# Start the Express server
# CMD ["node", "index.ts"]
CMD ["npx", "ts-node-dev", "--respawn","--transpile-only", "--exit-child", "-r", "tsconfig-paths/register", "./src/index.ts"]
Докерфайл фронта:
FROM node:alpine as build
WORKDIR /app
COPY package.json /app/package.json
RUN npm install
COPY . /app
RUN npm run build
FROM nginx:stable-alpine
COPY --from=build /app/dist /usr/share/nginx/html
COPY --from=build /app/nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 3000
CMD ["nginx", "-g", "daemon off;"]
Nginx.conf фронта:
server {
listen 3000;
location / {
root /usr/share/nginx.html;
index index.html index.htm;
try_files $uri $uri/ /index.html =404;
}
include /etc/nginx/extra-conf.d/*.conf;
}
Nginx.conf всего проекта:
user root;
worker_processes 1;
events {
worker_connections 1024;
}
http {
server {
listen 80;
server_name localhost;
# Перенаправление HTTP на HTTPS
return 301 https://localhost;
}
server {
listen 443 ssl;
server_name localhost;
ssl_certificate_key /etc/nginx/https_certs/localhost-key.pem;
ssl_certificate /etc/nginx/https_certs/localhost.pem;
location / {
proxy_pass http://localhost:3000/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location /api/ {
proxy_pass http://localhost:8443/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
}
И docker-compose всего проекта:
networks:
dev:
services:
nginx:
image: nginx:stable-alpine
ports:
- "443:443"
volumes:
- "./nginx.conf:/etc/nginx/nginx.conf"
- "C:/https_certs/localhost-key.pem:/etc/nginx/https_certs/localhost-key.pem"
- "C:/https_certs/localhost.pem:/etc/nginx/https_certs/localhost.pem"
depends_on:
- backend
- frontend
frontend:
build:
context: ./front
dockerfile: Dockerfile
depends_on:
- backend
networks:
- dev
backend:
build:
context: ./back
dockerfile: Dockerfile
networks:
- dev
env_file:
- /back/.env.production
volumes:
- "C:/https_certs/localhost-key.pem:/etc/nginx/https_certs/localhost-key.pem"
- "C:/https_certs/localhost.pem:/etc/nginx/https_certs/localhost.pem"
Сервер бэкенда запускается на порту 8443 (локально я его запускал на 443 для ssl). Сейчас на этом порту nginx.
( соотв. в переменных прод. окружения на бэкенде прописан этот порт 8443)
И в переменных окружения на фронте соотв. прописано что ходить надо также на этот порт 8443
В итоге докер запускается без ошибок, но при открытии
https://localhost имею вот такаю ошибку:
Прошу кто разбирается дать ответ или подсказку куда копать. Спасибо