From f94c90456f4e6ffd8af0a9d9b04089989bc2865c Mon Sep 17 00:00:00 2001 From: shept Date: Wed, 29 Oct 2025 21:47:59 +0500 Subject: [PATCH] Add Docker setup for Nginx serving built Node app Introduce a multi-stage Dockerfile to build and serve a Node.js application using Nginx. Define a custom Nginx configuration for optimized static file handling and enable gzip compression. --- .docker/app/nginx/nginx.conf | 51 ++++++++++++++++++++++++++++++++++++ Dockerfile | 11 ++++++++ 2 files changed, 62 insertions(+) create mode 100644 .docker/app/nginx/nginx.conf create mode 100644 Dockerfile diff --git a/.docker/app/nginx/nginx.conf b/.docker/app/nginx/nginx.conf new file mode 100644 index 0000000..05c90cc --- /dev/null +++ b/.docker/app/nginx/nginx.conf @@ -0,0 +1,51 @@ +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; + + gzip on; + gzip_comp_level 6; + gzip_http_version 1.1; + gzip_proxied any; + gzip_min_length 256; + gzip_types text/plain text/css application/javascript application/x-javascript application/xml application/xml+rss application/atom+xml image/svg+xml; + + sendfile on; + tcp_nopush on; + tcp_nodelay on; + keepalive_timeout 65; + types_hash_max_size 2048; + + include /etc/nginx/sites-enabled/*; + + server { + listen 80; + server_name _; + + location / { + root /usr/share/nginx/html; + index index.html; + try_files $uri $uri/ /index.html; + } + + error_page 500 502 503 504 /50x.html; + location = /50x.html { + root /usr/share/nginx/html; + } + } +} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..42949b3 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,11 @@ +FROM node:20-alpine AS build +WORKDIR /app +COPY package*.json ./ +RUN npm install +COPY . . +RUN npm run build + +FROM nginx:stable +COPY --from=build /app/dist /usr/share/nginx/html +COPY .docker/app/nginx/nginx.conf /etc/nginx/nginx.conf +EXPOSE 80