From 43bff91db2ba4ec68d76e601f7bc42cb7a506bc5 Mon Sep 17 00:00:00 2001 From: Elias Schneider Date: Thu, 29 Feb 2024 14:41:45 +0100 Subject: [PATCH] fix: replace Nginx with Caddy to fix "premature close" error while downloading larger files --- Caddyfile | 15 +++++++++++++++ Dockerfile | 17 ++++++++--------- README.md | 2 +- frontend/src/pages/api/[...all].tsx | 2 +- nginx/nginx.conf | 22 ---------------------- 5 files changed, 25 insertions(+), 33 deletions(-) create mode 100644 Caddyfile delete mode 100644 nginx/nginx.conf diff --git a/Caddyfile b/Caddyfile new file mode 100644 index 00000000..e266b804 --- /dev/null +++ b/Caddyfile @@ -0,0 +1,15 @@ +:3000 { + # Reverse proxy for /api + reverse_proxy /api/* http://localhost:8080 { + header_up X-Forwarded-Host {host}:{server_port} + header_up X-Forwarded-For {remote_host} + header_up X-Forwarded-Proto {scheme} + } + + # Reverse proxy for all other requests + reverse_proxy http://localhost:3333 { + header_up X-Forwarded-Host {host}:{server_port} + header_up X-Forwarded-For {remote_host} + header_up X-Forwarded-Proto {scheme} + } +} diff --git a/Dockerfile b/Dockerfile index 7b41a6f2..3e969dd1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -30,12 +30,12 @@ RUN npm run build && npm prune --production FROM node:20-alpine AS runner ENV NODE_ENV=docker -# Alpine specific dependencies -RUN apk update --no-cache -RUN apk upgrade --no-cache -RUN apk add --no-cache curl nginx +# Install Caddy +RUN apk update --no-cache \ + && apk upgrade --no-cache \ + && apk add --no-cache curl caddy -COPY ./nginx/nginx.conf /etc/nginx/nginx.conf +COPY ./Caddyfile /etc/caddy/Caddyfile WORKDIR /opt/app/frontend COPY --from=frontend-builder /opt/app/public ./public @@ -53,9 +53,8 @@ WORKDIR /opt/app EXPOSE 3000 -# Add a health check to ensure the container is healthy +# Health check remains unchanged HEALTHCHECK --interval=10s --timeout=3s CMD curl -f http://localhost:3000/api/health || exit 1 -# Application startup -# HOSTNAME=0.0.0.0 fixes https://github.com/vercel/next.js/issues/51684. It can be removed as soon as the issue is fixed -CMD cp -rn /tmp/img/* /opt/app/frontend/public/img && nginx && PORT=3333 HOSTNAME=0.0.0.0 node frontend/server.js & cd backend && npm run prod \ No newline at end of file +# Application startup updated for Caddy +CMD cp -rn /tmp/img/* /opt/app/frontend/public/img && caddy run --config /etc/caddy/Caddyfile & PORT=3333 HOSTNAME=0.0.0.0 node frontend/server.js & cd backend && npm run prod \ No newline at end of file diff --git a/README.md b/README.md index 25f07857..90aab9e5 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,7 @@ API_URL=http://localhost:8080 # Set the URL of the backend, default: http://loca pm2 start --name="pingvin-share-frontend" npm -- run start ``` -**Uploading Large Files**: By default, Pingvin Share uses a built-in reverse proxy to reduce the installation steps. However, this reverse proxy is not optimized for uploading large files. If you wish to upload larger files, you can either use the Docker installation or set up your own reverse proxy. An example configuration for Nginx can be found in `/nginx/nginx.conf`. +**Uploading Large Files**: By default, Pingvin Share uses a built-in reverse proxy to reduce the installation steps. However, this reverse proxy is not optimized for uploading large files. If you wish to upload larger files, you can either use the Docker installation or set up your own reverse proxy. An example configuration for Caddy can be found in `./Caddyfile`. The website is now listening on `http://localhost:3000`, have fun with Pingvin Share 🐧! diff --git a/frontend/src/pages/api/[...all].tsx b/frontend/src/pages/api/[...all].tsx index ff27d57b..df7a7c4d 100644 --- a/frontend/src/pages/api/[...all].tsx +++ b/frontend/src/pages/api/[...all].tsx @@ -12,7 +12,7 @@ export const config = { const { apiURL } = getConfig().serverRuntimeConfig; // A proxy to the API server only used in development. -// In production this route gets overridden by nginx. +// In production this route gets overridden by Caddy. export default (req: NextApiRequest, res: NextApiResponse) => { httpProxyMiddleware(req, res, { headers: { diff --git a/nginx/nginx.conf b/nginx/nginx.conf deleted file mode 100644 index 7134235c..00000000 --- a/nginx/nginx.conf +++ /dev/null @@ -1,22 +0,0 @@ -events {} - -http { - server { - listen 3000; - client_max_body_size 100M; - - location /api { - proxy_pass http://localhost:8080; - proxy_set_header X-Forwarded-Host $host:$server_port; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - proxy_set_header X-Forwarded-Proto $scheme; - } - - location / { - proxy_pass http://localhost:3333; - proxy_set_header X-Forwarded-Host $host:$server_port; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - proxy_set_header X-Forwarded-Proto $scheme; - } - } -}